From e7d11b0dcc3b9e2b2d6adaad60d4997b85880258 Mon Sep 17 00:00:00 2001 From: Andreas Tolfsen <andretol@usit.uio.no> Date: Thu, 19 Aug 2021 15:03:39 +0200 Subject: [PATCH] greg.models: fix PersonIdentities link to Person A Person may have multiple associated PersonIdentities. The object relation link between these data structures was previously introduced as "person", but is renamed "identities" with this patch. This changes the access pattern from: person.person.all() -> QuerySet[PersonIdentity] To: person.identities.all() -> QuerySet[PersonIdentity] Fixes: GREG-20 --- greg/api/filters.py | 2 +- greg/models.py | 2 +- greg/tests/models/test_person.py | 42 +++++++++++++++++++++++++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/greg/api/filters.py b/greg/api/filters.py index 86465b1d..c7af546c 100644 --- a/greg/api/filters.py +++ b/greg/api/filters.py @@ -21,7 +21,7 @@ class PersonRoleFilter(FilterSet): class PersonFilter(FilterSet): verified = BooleanFilter( - field_name="person__verified_by_id", lookup_expr="isnull", exclude=True + field_name="identities__verified_by_id", lookup_expr="isnull", exclude=True ) class Meta: diff --git a/greg/models.py b/greg/models.py index 065104b7..f50a6e03 100644 --- a/greg/models.py +++ b/greg/models.py @@ -153,7 +153,7 @@ class PersonIdentity(BaseModel): MANUAL = "MANUAL" person = models.ForeignKey( - "Person", on_delete=models.CASCADE, related_name="person" + "Person", on_delete=models.CASCADE, related_name="identities" ) type = models.CharField(max_length=16, choices=IdentityType.choices) source = models.CharField(max_length=256) diff --git a/greg/tests/models/test_person.py b/greg/tests/models/test_person.py index f4aeba58..f2417f18 100644 --- a/greg/tests/models/test_person.py +++ b/greg/tests/models/test_person.py @@ -1,3 +1,7 @@ +from datetime import ( + datetime, + timedelta, +) from functools import partial import pytest @@ -5,6 +9,7 @@ import pytest from greg.models import ( OrganizationalUnit, Person, + PersonIdentity, PersonRole, Role, Sponsor, @@ -30,7 +35,7 @@ def role_bar() -> Role: @pytest.fixture -def person(role_foo, role_bar) -> Person: +def person(role_foo: Role, role_bar: Role) -> Person: person = Person.objects.create( first_name="Test", last_name="Tester", @@ -56,9 +61,44 @@ def person(role_foo, role_bar) -> Person: return person +@pytest.fixture +def a_year_ago() -> datetime: + return datetime.now() - timedelta(days=365) + + +@pytest.fixture +def feide_id(a_year_ago: datetime) -> PersonIdentity: + return PersonIdentity( + type=PersonIdentity.IdentityType.FEIDE_ID, + verified_when=a_year_ago, + ) + + +@pytest.fixture +def passport(a_year_ago: datetime) -> PersonIdentity: + return PersonIdentity( + type=PersonIdentity.IdentityType.PASSPORT, + verified_when=a_year_ago, + ) + + +@pytest.fixture +def feide_verified(person: Person, feide_id: PersonIdentity) -> Person: + person.identities.add(feide_id, bulk=False) + return person + + @pytest.mark.django_db def test_add_multiple_roles_to_person(person, role_foo, role_bar): person_roles = person.roles.all() assert len(person_roles) == 2 assert role_foo in person_roles assert role_bar in person_roles + + +@pytest.mark.django_db +def test_identities(person: Person, feide_id: PersonIdentity, passport: PersonIdentity): + person.identities.add(feide_id, bulk=False) + assert list(person.identities.all()) == [feide_id] + person.identities.add(passport, bulk=False) + assert list(person.identities.all()) == [feide_id, passport] -- GitLab