Skip to content

Commit

Permalink
Display primary affiliation; add primary if first #3168
Browse files Browse the repository at this point in the history
  • Loading branch information
joemull committed Dec 13, 2024
1 parent 4cbd974 commit bbbe9f3
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
15 changes: 14 additions & 1 deletion src/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2258,7 +2258,7 @@ class Affiliation(models.Model):
)

class Meta:
ordering = ['-pk']
ordering = ['is_primary', '-pk']

def title_department(self):
elements = [
Expand All @@ -2284,6 +2284,18 @@ def is_current(self):
return False
return True

@classmethod
def set_primary_if_first(cls, obj):
other_affiliations = cls.objects.filter(
account=obj.account,
frozen_author=obj.frozen_author,
preprint_author=obj.preprint_author,
).exclude(
pk=obj.pk
).exists()
if not other_affiliations:
obj.is_primary = True

@classmethod
def keep_is_primary_unique(cls, obj):
if obj.is_primary:
Expand Down Expand Up @@ -2426,6 +2438,7 @@ def naive_set_primary_country(
affiliation.organization.locations.add(country_location)

def save(self, *args, **kwargs):
self.set_primary_if_first(self)
self.keep_is_primary_unique(self)
super().save(*args, **kwargs)

Expand Down
18 changes: 18 additions & 0 deletions src/core/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,3 +787,21 @@ def test_organization_location(self):
self.organization_bbk.location,
self.location_london,
)

def test_set_primary_if_first_true(self):
first_affiliation, _created = models.Affiliation.objects.get_or_create(
account=self.t_s_eliot,
organization=self.organization_bbk,
)
self.assertTrue(first_affiliation.is_primary)

def test_set_primary_if_first_false(self):
_first_affiliation, _created = models.Affiliation.objects.get_or_create(
account=self.t_s_eliot,
organization=self.organization_bbk,
)
second_affiliation, _created = models.Affiliation.objects.get_or_create(
account=self.t_s_eliot,
organization=self.organization_rae,
)
self.assertFalse(second_affiliation.is_primary)
7 changes: 6 additions & 1 deletion src/templates/admin/core/affiliation_display.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% load static %}

<div>
<div class="max-w-32">
{% if affiliation.title_department %}
<span>{{ affiliation.title_department }},</span>
{% endif %}
Expand All @@ -17,4 +17,9 @@
{% else %}
{{ affiliation.organization }}
{% endif %}
{% if affiliation.is_primary %}
<span class="label secondary">
Primary
</span>
{% endif %}
</div>

0 comments on commit bbbe9f3

Please sign in to comment.