Skip to content

Commit 7fac40a

Browse files
committed
feat #3168 Fix logic that manages affiliations
1 parent 56f41e1 commit 7fac40a

File tree

4 files changed

+64
-15
lines changed

4 files changed

+64
-15
lines changed

src/core/models.py

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,18 @@ def is_preprint_editor(self, request):
581581
def is_reader(self, request):
582582
return self.check_role(request.journal, 'reader', staff_override=False)
583583

584+
def snapshot_affiliations(self, frozen_author):
585+
"""
586+
Delete any outdated affiliations on the frozen author and then
587+
assign copies of account affiliations to the frozen author.
588+
"""
589+
frozen_author.affiliations.delete()
590+
for affiliation in self.affiliations:
591+
affiliation.pk = None
592+
affiliation.account = None
593+
affiliation.frozen_author = frozen_author
594+
affiliation.save()
595+
584596
def snapshot_self(self, article, force_update=True):
585597
frozen_dict = {
586598
'name_prefix': self.name_prefix,
@@ -596,9 +608,8 @@ def snapshot_self(self, article, force_update=True):
596608
if frozen_author and force_update:
597609
for k, v in frozen_dict.items():
598610
setattr(frozen_author, k, v)
599-
frozen_author.institution = self.institution
600-
frozen_author.department = self.department
601611
frozen_author.save()
612+
self.snapshot_affiliations(frozen_author)
602613

603614
else:
604615
try:
@@ -617,9 +628,7 @@ def snapshot_self(self, article, force_update=True):
617628
defaults=dict(order=order_object.order, **frozen_dict)
618629
)
619630
if created:
620-
frozen_author.institution = self.institution
621-
frozen_author.department = self.department
622-
frozen_author.save()
631+
self.snapshot_affiliations(frozen_author)
623632

624633
def frozen_author(self, article):
625634
try:
@@ -2087,6 +2096,13 @@ def location(self):
20872096
"""
20882097
return self.locations.first() if self.locations else None
20892098

2099+
@property
2100+
def country(self):
2101+
"""
2102+
Return the country of the first location.
2103+
"""
2104+
return self.location.country if self.location else None
2105+
20902106
@property
20912107
def names(self):
20922108
"""

src/review/logic.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -834,12 +834,12 @@ def process_reviewer_csv(path, request, article, form):
834834
'is_active': True,
835835
}
836836
)
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()
837+
if row.get('institution', '') or row.get('department', ''):
838+
core_models.Affiliation.naive_get_or_create(
839+
institution=row.get('institution', ''),
840+
department=row.get('department', ''),
841+
account=reviewer,
842+
)
843843

844844
try:
845845
review_interests = row.get('interests')

src/submission/models.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,12 @@ def affiliation(self, obj=False, date=None):
21652165
date=date,
21662166
)
21672167

2168+
@property
2169+
def affiliations(self):
2170+
return core_models.Affiliation.objects.filter(
2171+
frozen_author=self
2172+
).order_by('-is_primary')
2173+
21682174
@property
21692175
def is_correspondence_author(self):
21702176
# early return if no email address available

src/utils/testing/helpers.py

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,28 @@ def create_peer_reviewer(journal, **kwargs):
173173
return reviewer
174174

175175

176+
def create_affiliation(
177+
institution='',
178+
department='',
179+
account=None,
180+
frozen_author=None,
181+
preprint_author=None,
182+
):
183+
organization = core_models.Organization.objects.create()
184+
core_models.OrganizationName.objects.create(
185+
value=institution,
186+
custom_label_for=organization,
187+
)
188+
affiliation = core_models.Affiliation.objects.create(
189+
organization=organization,
190+
department=department,
191+
account=account,
192+
frozen_author=frozen_author,
193+
preprint_author=preprint_author,
194+
)
195+
return affiliation
196+
197+
176198
def create_author(journal, **kwargs):
177199
roles = kwargs.pop('roles', ['author'])
178200
email = kwargs.pop('email', "[email protected]")
@@ -189,10 +211,13 @@ def create_author(journal, **kwargs):
189211
journal=journal,
190212
**attrs,
191213
)
192-
author.institution = "Author institution"
193-
author.department = "Author Department"
194214
author.is_active = True
195215
author.save()
216+
create_affiliation(
217+
institution="Author institution",
218+
department="Author Department",
219+
account=author,
220+
)
196221
return author
197222

198223

@@ -355,8 +380,10 @@ def create_preprint(repository, author, subject, title='This is a Test Preprint'
355380
order=1,
356381
)
357382
preprint_author.save()
358-
preprint_author.affiliation = 'Made Up University'
359-
preprint_author.save()
383+
create_affiliation(
384+
institution="Made Up University",
385+
preprint_author=preprint_author,
386+
)
360387
return preprint
361388

362389

0 commit comments

Comments
 (0)