Skip to content

Commit cc46446

Browse files
committed
Work through regressions #3168
1 parent 0ae7801 commit cc46446

File tree

8 files changed

+90
-64
lines changed

8 files changed

+90
-64
lines changed

src/core/admin.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -451,11 +451,11 @@ class AffiliationAdmin(admin.ModelAdmin):
451451
'pk',
452452
'title',
453453
'department',
454-
'organization__ror_display_for',
455-
'organization__custom_label_for',
456-
'organization__label_for',
457-
'organization__alias_for',
458-
'organization__acronym_for'
454+
'organization__ror_display__value',
455+
'organization__custom_label__value',
456+
'organization__labels__value',
457+
'organization__aliases__value',
458+
'organization__acronyms__value',
459459
'account__first_name',
460460
'account__last_name',
461461
'account__email',

src/core/models.py

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,6 @@ def snapshot_self(self, article, force_update=True):
566566
'middle_name': self.middle_name,
567567
'last_name': self.last_name,
568568
'name_suffix': self.suffix,
569-
'institution': self.institution,
570-
'department': self.department,
571569
'display_email': True if self == article.correspondence_author else False,
572570
}
573571

@@ -576,6 +574,8 @@ def snapshot_self(self, article, force_update=True):
576574
if frozen_author and force_update:
577575
for k, v in frozen_dict.items():
578576
setattr(frozen_author, k, v)
577+
frozen_author.institution = self.institution
578+
frozen_author.department = self.department
579579
frozen_author.save()
580580

581581
else:
@@ -589,11 +589,15 @@ def snapshot_self(self, article, force_update=True):
589589
defaults={'order': order_integer}
590590
)
591591

592-
submission_models.FrozenAuthor.objects.get_or_create(
592+
frozen_author, created = submission_models.FrozenAuthor.objects.get_or_create(
593593
author=self,
594594
article=article,
595595
defaults=dict(order=order_object.order, **frozen_dict)
596596
)
597+
if created:
598+
frozen_author.institution = self.institution
599+
frozen_author.department = self.department
600+
frozen_author.save()
597601

598602
def frozen_author(self, article):
599603
try:
@@ -2074,40 +2078,39 @@ def naive_get_or_create(
20742078
value,
20752079
account=None,
20762080
frozen_author=None,
2081+
preprint_author=None,
20772082
):
20782083
"""
20792084
Backwards-compatible API for finding a matching organization name.
2080-
Intended for use in batch importers where ROR data is not available.
2085+
Intended for use in batch importers where ROR data is not available
2086+
in the data being imported.
20812087
Does not support ROR ids, ROR name types, or location data.
20822088
"""
2083-
no_exact_match = (
2084-
cls.DoesNotExist or cls.MultipleObjectsReturned
2085-
)
2086-
20872089
# Is there a single exact match in the
20882090
# canonical name data from ROR (e.g. labels)?
20892091
try:
20902092
organization = cls.objects.get(labels__value=value)
20912093
created = False
2092-
except no_exact_match:
2094+
except cls.DoesNotExist or cls.MultipleObjectsReturned:
20932095
# Or maybe one in the past or alternate
20942096
# name data from ROR (e.g. aliases)?
20952097
try:
20962098
organization = cls.objects.get(aliases__value=value)
20972099
created = False
2098-
except no_exact_match:
2100+
except cls.DoesNotExist or cls.MultipleObjectsReturned:
20992101
# Or maybe an organization has already been
21002102
# entered without a ROR for this
21012103
# account / frozen author / preprint author?
21022104
try:
21032105
organization = cls.objects.get(
21042106
affiliation__account=account,
21052107
affiliation__frozen_author=frozen_author,
2108+
affiliation__preprint_author=preprint_author,
21062109
ror__exact='',
21072110
)
21082111
created = False
2109-
except no_exact_match:
2110-
# Otherwise, create a naive, disconnected record
2112+
except cls.DoesNotExist or cls.MultipleObjectsReturned:
2113+
# Otherwise, create a naive, disconnected record.
21112114
organization = cls.objects.create()
21122115
created = True
21132116

@@ -2350,17 +2353,24 @@ def naive_get_or_create(
23502353
value,
23512354
account=account,
23522355
frozen_author=frozen_author,
2353-
)
2354-
2355-
# Create or update the actual affiliation
2356-
affiliation, created = Affiliation.objects.get_or_create(
2357-
account=account,
2358-
frozen_author=frozen_author,
23592356
preprint_author=preprint_author,
2360-
organization=organization,
23612357
)
2362-
affiliation.is_primary = True
2363-
affiliation.save()
2358+
2359+
# Create or update the actual affiliation if the associated
2360+
# account / frozen author / preprint author has been saved already
2361+
try:
2362+
affiliation, created = Affiliation.objects.get_or_create(
2363+
account=account,
2364+
frozen_author=frozen_author,
2365+
preprint_author=preprint_author,
2366+
organization=organization,
2367+
)
2368+
affiliation.is_primary = True
2369+
affiliation.save()
2370+
except ValueError:
2371+
logger.warn('The affiliation could not be created.')
2372+
affiliation = None
2373+
created = False
23642374
return affiliation, created
23652375

23662376
@classmethod
@@ -2375,13 +2385,18 @@ def naive_set_primary_department(
23752385
Intended for use in batch importers where ROR data is not available.
23762386
Does not support RORs or multiple affiliations.
23772387
"""
2378-
affiliation, _created = Affiliation.objects.get_or_create(
2379-
account=account,
2380-
frozen_author=frozen_author,
2381-
is_primary=True,
2382-
)
2383-
affiliation.department = value
2384-
affiliation.save()
2388+
# Create or update an affiliation if the associated
2389+
# account / frozen author / preprint author has been saved already
2390+
try:
2391+
affiliation, _created = Affiliation.objects.get_or_create(
2392+
account=account,
2393+
frozen_author=frozen_author,
2394+
is_primary=True,
2395+
)
2396+
affiliation.department = value
2397+
affiliation.save()
2398+
except ValueError:
2399+
logger.warn('The department could not be set.')
23852400

23862401
@classmethod
23872402
def naive_set_primary_country(

src/core/tests/test_app.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ def test_orcid_registration(self, record_mock):
206206
self.assertEqual(response.status_code, 200)
207207
self.assertContains(response, "Campbell")
208208
self.assertContains(response, "Kasey")
209-
self.assertContains(response, "Elk Valley University")
210209
self.assertContains(response, "[email protected]")
211210
self.assertNotContains(response, "Register with ORCiD")
212211
self.assertContains(response, "http://sandbox.orcid.org/0000-0000-0000-0000")

src/core/views.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -329,11 +329,6 @@ def register(request):
329329
initial["last_name"] = orcid_details.get("last_name", "")
330330
if orcid_details.get("emails"):
331331
initial["email"] = orcid_details["emails"][0]
332-
if orcid_details.get("affiliation"):
333-
initial['institution'] = orcid_details['affiliation']
334-
if orcid_details.get("country"):
335-
if models.Country.objects.filter(code=orcid_details['country']).exists():
336-
initial["country"] = models.Country.objects.get(code=orcid_details['country'])
337332

338333
form = forms.RegistrationForm(
339334
journal=request.journal,
@@ -355,6 +350,10 @@ def register(request):
355350
if form.is_valid():
356351
if token_obj:
357352
new_user = form.save()
353+
if new_user.orcid:
354+
orcid_details = orcid.get_orcid_record_details(token_obj.orcid)
355+
if orcid_details.get("affiliation"):
356+
new_user.institution = orcid_details['affiliation']
358357
token_obj.delete()
359358
# If the email matches the user email on ORCID, log them in
360359
if new_user.email == initial.get("email"):

src/repository/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ def repository_search(request, search_term=None):
377377
Q(account__first_name__in=split_search_term) |
378378
Q(account__middle_name__in=split_search_term) |
379379
Q(account__last_name__in=split_search_term) |
380-
Q(account__institution__icontains=search_term)
380+
Q(account__affiliation__organization__labels__value__icontains=search_term)
381381
)
382382
&
383383
(

src/review/logic.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -824,24 +824,22 @@ def process_reviewer_csv(path, request, article, form):
824824
reader = csv.DictReader(csv_file)
825825
reviewers = []
826826
for row in reader:
827-
try:
828-
country = core_models.Country.objects.get(code=row.get('country'))
829-
except core_models.Country.DoesNotExist:
830-
country = None
831-
832827
reviewer, created = core_models.Account.objects.get_or_create(
833828
email=row.get('email_address'),
834829
defaults={
835830
'salutation': row.get('salutation', ''),
836831
'first_name': row.get('firstname', ''),
837832
'middle_name': row.get('middlename', ''),
838833
'last_name': row.get('lastname', ''),
839-
'department': row.get('department', ''),
840-
'institution': row.get('institution', ''),
841-
'country': country,
842834
'is_active': True,
843835
}
844836
)
837+
if row.get('department', ''):
838+
reviewer.department = row.get('department', '')
839+
reviewer.save()
840+
if row.get('institution', ''):
841+
reviewer.institution = row.get('institution', '')
842+
reviewer.save()
845843

846844
try:
847845
review_interests = row.get('interests')

src/submission/tests.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,28 +66,32 @@ def create_journal():
6666

6767
return journal_one
6868

69-
@staticmethod
70-
def create_authors():
69+
@classmethod
70+
def create_authors(cls):
7171
author_1_data = {
72+
'email': '[email protected]',
7273
'is_active': True,
7374
'password': 'this_is_a_password',
7475
'salutation': 'Prof.',
7576
'first_name': 'Martin',
77+
'middle_name': '',
7678
'last_name': 'Eve',
7779
'department': 'English & Humanities',
7880
'institution': 'Birkbeck, University of London',
7981
}
8082
author_2_data = {
83+
'email': '[email protected]',
8184
'is_active': True,
8285
'password': 'this_is_a_password',
8386
'salutation': 'Sr.',
8487
'first_name': 'Mauro',
88+
'middle_name': '',
8589
'last_name': 'Sanchez',
8690
'department': 'English & Humanities',
8791
'institution': 'Birkbeck, University of London',
8892
}
89-
author_1 = Account.objects.create(email="[email protected]", **author_1_data)
90-
author_2 = Account.objects.create(email="[email protected]", **author_2_data)
93+
author_1 = helpers.create_author(cls.journal_one, **author_1_data)
94+
author_2 = helpers.create_author(cls.journal_one, **author_2_data)
9195

9296
return author_1, author_2
9397

@@ -105,12 +109,15 @@ def create_sections(self):
105109
journal=self.journal_one,
106110
)
107111

112+
@classmethod
113+
def setUpTestData(cls):
114+
cls.journal_one = cls.create_journal()
115+
108116
def setUp(self):
109117
"""
110118
Setup the test environment.
111119
:return: None
112120
"""
113-
self.journal_one = self.create_journal()
114121
self.editor = helpers.create_editor(self.journal_one)
115122
self.press = helpers.create_press()
116123
self.create_sections()
@@ -273,7 +280,9 @@ def test_snapshot_author_metadata_override(self):
273280

274281
article.snapshot_authors()
275282
new_department = "New department"
276-
article.frozen_authors().update(department=new_department)
283+
for frozen in article.frozen_authors():
284+
frozen.department = new_department
285+
frozen.save()
277286
article.snapshot_authors(force_update=True)
278287
frozen = article.frozen_authors().all()[0]
279288

@@ -576,15 +585,12 @@ def test_author_form_harmful_inputs(self):
576585
"middle_name",
577586
"name_prefix",
578587
"suffix",
579-
"institution",
580-
"department",
581588
}):
582589
form = forms.AuthorForm(
583590
{
584591
'first_name': 'Andy',
585592
'last_name': 'Byers',
586593
'biography': 'Andy',
587-
'institution': 'Birkbeck, University of London',
588594
'email': f'andy{i}@janeway.systems',
589595
'orcid': 'https://orcid.org/0000-0003-2126-266X',
590596
**{attr: harmful_string},
@@ -783,37 +789,44 @@ def create_journal():
783789

784790
return journal_one
785791

786-
@staticmethod
787-
def create_authors():
792+
@classmethod
793+
def create_authors(cls):
788794
author_1_data = {
795+
'email': '[email protected]',
789796
'is_active': True,
790797
'password': 'this_is_a_password',
791798
'salutation': 'Prof.',
792799
'first_name': 'Martin',
800+
'middle_name': '',
793801
'last_name': 'Eve',
794802
'department': 'English & Humanities',
795803
'institution': 'Birkbeck, University of London',
796804
}
797805
author_2_data = {
806+
'email': '[email protected]',
798807
'is_active': True,
799808
'password': 'this_is_a_password',
800809
'salutation': 'Sr.',
801810
'first_name': 'Mauro',
811+
'middle_name': '',
802812
'last_name': 'Sanchez',
803813
'department': 'English & Humanities',
804814
'institution': 'Birkbeck, University of London',
805815
}
806-
author_1 = Account.objects.create(email="[email protected]", **author_1_data)
807-
author_2 = Account.objects.create(email="[email protected]", **author_1_data)
816+
author_1 = helpers.create_author(cls.journal_one, **author_1_data)
817+
author_2 = helpers.create_author(cls.journal_one, **author_2_data)
808818

809819
return author_1, author_2
810820

821+
@classmethod
822+
def setUpTestData(cls):
823+
cls.journal_one = cls.create_journal()
824+
811825
def setUp(self):
812826
"""
813827
Setup the test environment.
814828
:return: None
815829
"""
816-
self.journal_one = self.create_journal()
817830
self.editor = helpers.create_editor(self.journal_one)
818831
self.press = helpers.create_press()
819832

src/utils/testing/helpers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ def create_user(username, roles=None, journal=None, **attrs):
6262
journal=journal
6363
)
6464

65+
user.save()
6566
for attr, value in attrs.items():
6667
setattr(user, attr, value)
6768

@@ -179,8 +180,6 @@ def create_author(journal, **kwargs):
179180
"first_name": "Author",
180181
"middle_name": "A",
181182
"last_name": "User",
182-
"institution": "Author institution",
183-
"department": "Author Department",
184183
"biography": "Author test biography"
185184
}
186185
attrs.update(kwargs)
@@ -190,6 +189,8 @@ def create_author(journal, **kwargs):
190189
journal=journal,
191190
**attrs,
192191
)
192+
author.institution = "Author institution"
193+
author.department = "Author Department"
193194
author.is_active = True
194195
author.save()
195196
return author
@@ -353,6 +354,7 @@ def create_preprint(repository, author, subject, title='This is a Test Preprint'
353354
account=author,
354355
order=1,
355356
)
357+
preprint_author.save()
356358
preprint_author.affiliation = 'Made Up University'
357359
preprint_author.save()
358360
return preprint

0 commit comments

Comments
 (0)