From 022e4620ac4469df5f25114959b39e1ead4aff13 Mon Sep 17 00:00:00 2001
From: Piotr Gawron <piotr.gawron@uni.lu>
Date: Thu, 11 Mar 2021 14:32:57 +0100
Subject: [PATCH] api call to list of subject types is computed based on data
 from db

---
 smash/web/api_views/subject.py            |  9 +++++----
 smash/web/tests/api_views/test_subject.py | 11 ++++++++++-
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/smash/web/api_views/subject.py b/smash/web/api_views/subject.py
index fe2e7e6a..f5c777cc 100644
--- a/smash/web/api_views/subject.py
+++ b/smash/web/api_views/subject.py
@@ -10,8 +10,8 @@ from django.urls import reverse
 from web.api_views.serialization_utils import str_to_yes_no_null, bool_to_yes_no, flying_team_to_str, location_to_str, \
     add_column, serialize_date, serialize_datetime, get_filters_for_data_table_request
 from web.models import ConfigurationItem, StudySubject, Visit, Appointment, Subject, SubjectColumns, StudyColumns, \
-    Study, ContactAttempt
-from web.models.constants import SUBJECT_TYPE_CHOICES, GLOBAL_STUDY_ID, VISIT_SHOW_VISIT_NUMBER_FROM_ZERO, \
+    Study, ContactAttempt, SubjectType
+from web.models.constants import GLOBAL_STUDY_ID, VISIT_SHOW_VISIT_NUMBER_FROM_ZERO, \
     CUSTOM_FIELD_TYPE_TEXT, CUSTOM_FIELD_TYPE_BOOLEAN, CUSTOM_FIELD_TYPE_INTEGER, CUSTOM_FIELD_TYPE_DOUBLE, \
     CUSTOM_FIELD_TYPE_DATE, CUSTOM_FIELD_TYPE_SELECT_LIST, CUSTOM_FIELD_TYPE_FILE
 from web.models.custom_data.custom_study_subject_field import get_study_subject_field_id, CustomStudySubjectField
@@ -461,8 +461,9 @@ def subjects(request, subject_list_type):
 
 # noinspection PyUnusedLocal
 def types(request):
-    data = [{"id": subject_type_id, "name": subject_type_name} for subject_type_id, subject_type_name in
-            list(SUBJECT_TYPE_CHOICES.items())]
+    data = []
+    for subject_type in SubjectType.objects.all():
+        data.append({"id": subject_type.id, "name": subject_type.name})
     return JsonResponse({
         "types": data
     })
diff --git a/smash/web/tests/api_views/test_subject.py b/smash/web/tests/api_views/test_subject.py
index 3f2b213c..97943bb1 100644
--- a/smash/web/tests/api_views/test_subject.py
+++ b/smash/web/tests/api_views/test_subject.py
@@ -10,7 +10,7 @@ from six import ensure_str
 
 from web.api_views.subject import get_subjects_order, get_subjects_filtered, serialize_subject, get_subject_columns
 from web.importer.warning_counter import MsgCounterHandler
-from web.models import StudySubject, Appointment, Study, Worker, SubjectColumns, StudyColumns
+from web.models import StudySubject, Appointment, Study, Worker, SubjectColumns, StudyColumns, SubjectType
 from web.models.constants import GLOBAL_STUDY_ID, CUSTOM_FIELD_TYPE_TEXT, CUSTOM_FIELD_TYPE_BOOLEAN, \
     CUSTOM_FIELD_TYPE_INTEGER, CUSTOM_FIELD_TYPE_DOUBLE, \
     CUSTOM_FIELD_TYPE_DATE, CUSTOM_FIELD_TYPE_SELECT_LIST, CUSTOM_FIELD_TYPE_FILE
@@ -60,6 +60,15 @@ class TestSubjectApi(LoggedInWithWorkerTestCase):
 
         self.assertTrue(city_name in cities)
 
+    def test_subject_types(self):
+        response = self.client.get(reverse('web.api.subject_types'))
+        self.assertEqual(response.status_code, 200)
+
+        types = json.loads(response.content)['types']
+
+        for subject_type in types:
+            self.assertIsNotNone(SubjectType.objects.get(pk=subject_type['id']))
+
     def test_get_columns(self):
         response = self.client.get(
             reverse('web.api.subjects.columns', kwargs={'subject_list_type': SUBJECT_LIST_GENERIC}))
-- 
GitLab