From bd5e216842bb8056759c0e28b7b95e0c1f742f61 Mon Sep 17 00:00:00 2001
From: Jonas Braathen <jonas.braathen@usit.uio.no>
Date: Thu, 9 Dec 2021 19:38:45 +0100
Subject: [PATCH] Improvements to consent choices

- Add test data for consent choices
- Show consent choices made by persons
- Add consent type to ConsentChoice.__str__ to tell them apart in the admin interface
---
 greg/api/serializers/consent.py               |  4 ++-
 greg/api/serializers/consent_type.py          |  6 +---
 .../management/commands/populate_test_data.py | 29 +++++++++++++++----
 greg/models.py                                |  4 ++-
 greg/tests/api/test_person.py                 | 15 +++++++++-
 5 files changed, 44 insertions(+), 14 deletions(-)

diff --git a/greg/api/serializers/consent.py b/greg/api/serializers/consent.py
index 2d1eadf6..ceed2dbe 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 407777f6..fefe1f19 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 72f6d127..f9780036 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 dd7bb127..4729a325 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 e39bd2c2..0adf5c8b 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
-- 
GitLab