diff --git a/greg/api/serializers/consent.py b/greg/api/serializers/consent.py index 2d1eadf6dad2599d1e188199d956d73060bb753b..ceed2dbea6aa74806ddafd9c7674070f17871aa0 100644 --- a/greg/api/serializers/consent.py +++ b/greg/api/serializers/consent.py @@ -6,7 +6,9 @@ from greg.models import Consent class ConsentSerializerBrief(serializers.ModelSerializer): type = ConsentTypeSerializerBrief(read_only=True) + choice = serializers.CharField(read_only=True, source="choice.value") class Meta: model = Consent - fields = ["type", "consent_given_at"] + queryset = Consent.objects.all().select_related('choice') + fields = ["type", "consent_given_at", "choice"] diff --git a/greg/api/serializers/consent_type.py b/greg/api/serializers/consent_type.py index 407777f6026e7ae0602988889657b2e764d2b71c..fefe1f19caca36bb086422bd2c2cb77371d05781 100644 --- a/greg/api/serializers/consent_type.py +++ b/greg/api/serializers/consent_type.py @@ -33,8 +33,4 @@ class ConsentTypeSerializer(ModelSerializer): class ConsentTypeSerializerBrief(ModelSerializer): class Meta: model = ConsentType - fields = [ - "identifier", - "valid_from", - "user_allowed_to_change", - ] + fields = ["identifier", "mandatory"] diff --git a/greg/management/commands/populate_test_data.py b/greg/management/commands/populate_test_data.py index 72f6d1275f7e67c81b33f72f33afb4114371821f..f978003624786d70ed7c9daf69864f8d5965ef81 100644 --- a/greg/management/commands/populate_test_data.py +++ b/greg/management/commands/populate_test_data.py @@ -22,11 +22,10 @@ one of them has denied the other one. """ import datetime -from django.core.management.base import CommandError from django.db import connection from django.conf import settings -from django.core.management.base import BaseCommand +from django.core.management.base import BaseCommand, CommandError from django.utils import timezone from greg.models import ( @@ -327,15 +326,27 @@ class DatabasePopulation: invitation=invitation, expire=timezone.now() - datetime.timedelta(days=32), ) + mandatory_consent_type = ConsentType.objects.get( + identifier=CONSENT_IDENT_MANDATORY + ) Consent.objects.create( person=adam, - type=ConsentType.objects.get(identifier=CONSENT_IDENT_MANDATORY), + type=mandatory_consent_type, consent_given_at=datetime.date.today() - datetime.timedelta(days=10), + choice=ConsentChoice.objects.get( + consent_type=mandatory_consent_type, value="yes" + ), + ) + optional_consent_type = ConsentType.objects.get( + identifier=CONSENT_IDENT_OPTIONAL ) Consent.objects.create( person=adam, - type=ConsentType.objects.get(identifier=CONSENT_IDENT_OPTIONAL), + type=optional_consent_type, consent_given_at=datetime.date.today() - datetime.timedelta(days=10), + choice=ConsentChoice.objects.get( + consent_type=optional_consent_type, value="no" + ), ) def _add_expired_person(self): @@ -383,10 +394,16 @@ class DatabasePopulation: invitation=invitation, expire=timezone.now() - datetime.timedelta(days=204), ) + mandatory_consent_type = ConsentType.objects.get( + identifier=CONSENT_IDENT_MANDATORY + ) Consent.objects.create( person=esther, - type=ConsentType.objects.get(identifier=CONSENT_IDENT_MANDATORY), - consent_given_at=datetime.date.today() - datetime.timedelta(days=206), + type=mandatory_consent_type, + consent_given_at=datetime.date.today() - datetime.timedelta(days=10), + choice=ConsentChoice.objects.get( + consent_type=mandatory_consent_type, value="yes" + ), ) def populate_database(self): diff --git a/greg/models.py b/greg/models.py index dd7bb127fe4ef390ad072f90cc09765ca5a1af2f..4729a325d55e149e23f759375241bd00625e56b0 100644 --- a/greg/models.py +++ b/greg/models.py @@ -373,7 +373,9 @@ class ConsentChoice(BaseModel): ) def __str__(self): - return "{} ({})".format(str(self.text_en or self.text_nb), self.value) + return "{}: {} ({})".format( + str(self.consent_type), str(self.text_en or self.text_nb), self.value + ) def __repr__(self): return "{}(id={!r}, consent_type={!r} value={!r}, text_en={!r}, text_nb={!r}, text_nn={!r})".format( diff --git a/greg/tests/api/test_person.py b/greg/tests/api/test_person.py index e39bd2c28c57ab25612650c800993642c91f323c..0adf5c8b199b78981894c39fc468d5b7cac6f4ae 100644 --- a/greg/tests/api/test_person.py +++ b/greg/tests/api/test_person.py @@ -8,6 +8,7 @@ from rest_framework.status import HTTP_200_OK from rest_framework.test import APIClient from greg.models import ( + Consent, Identity, Sponsor, RoleType, @@ -60,13 +61,25 @@ def role_data_guest( @pytest.mark.django_db -def test_get_person(client, person_foo): +def test_get_person(client, person_foo, consent_type_foo): + Consent.objects.create( + person=person_foo, + type=consent_type_foo, + choice=consent_type_foo.choices.get(value="yes"), + ) resp = client.get(reverse("v1:person-detail", kwargs={"id": person_foo.id})) assert resp.status_code == HTTP_200_OK data = resp.json() assert data.get("id") == person_foo.id assert data.get("first_name") == person_foo.first_name assert data.get("last_name") == person_foo.last_name + assert data.get("consents") == [ + { + "consent_given_at": None, + "type": {"identifier": "foo", "mandatory": False}, + "choice": "yes", + } + ] @pytest.mark.django_db