From d53d3320107b4331327b15ded78fa5d05d455a3c Mon Sep 17 00:00:00 2001 From: Kelvin Muchiri Date: Wed, 28 Feb 2024 11:52:43 +0300 Subject: [PATCH] add fields to forms exposed on endpoint /api/v1/projects --- .../tests/viewsets/test_project_viewset.py | 70 +++++++++++++++++-- .../libs/serializers/project_serializer.py | 42 ++++++++++- 2 files changed, 107 insertions(+), 5 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_project_viewset.py b/onadata/apps/api/tests/viewsets/test_project_viewset.py index 1b96ca546c..5bde00ca2c 100644 --- a/onadata/apps/api/tests/viewsets/test_project_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_project_viewset.py @@ -142,16 +142,78 @@ def test_publish_xlsform_using_url_upload(self, mock_requests): 1, ) + @override_settings(TIME_ZONE="UTC") def test_projects_list(self): - self._project_create() + self._publish_xls_form_to_project() + self.project.refresh_from_db() request = self.factory.get("/", **self.extra) request.user = self.user response = self.view(request) self.assertNotEqual(response.get("Cache-Control"), None) self.assertEqual(response.status_code, 200) - serializer = BaseProjectSerializer(self.project, context={"request": request}) - - self.assertEqual(response.data, [serializer.data]) + expected_data = [ + OrderedDict( + [ + ("url", f"http://testserver/api/v1/projects/{self.project.pk}"), + ("projectid", self.project.pk), + ("owner", "http://testserver/api/v1/users/bob"), + ("created_by", "http://testserver/api/v1/users/bob"), + ( + "metadata", + { + "category": "governance", + "location": "Naivasha, Kenya", + "description": "Some description", + }, + ), + ("starred", False), + ( + "users", + [ + { + "is_org": False, + "metadata": {}, + "first_name": "Bob", + "last_name": "erama", + "user": "bob", + "role": "owner", + } + ], + ), + ( + "forms", + [ + OrderedDict( + [ + ("name", "transportation_2011_07_25"), + ("formid", self.xform.pk), + ("id_string", "transportation_2011_07_25"), + ("is_merged_dataset", False), + ("contributes_entities_to", None), + ("consumes_entities_from", []), + ] + ) + ], + ), + ("public", False), + ("tags", []), + ("num_datasets", 1), + ("last_submission_date", None), + ("teams", []), + ("name", "demo"), + ( + "date_created", + self.project.date_created.isoformat().replace("+00:00", "Z"), + ), + ( + "date_modified", + self.project.date_modified.isoformat().replace("+00:00", "Z"), + ), + ("deleted_at", None), + ] + ) + ] + self.assertEqual(response.data, expected_data) self.assertIn("created_by", list(response.data[0])) def test_projects_list_with_pagination(self): diff --git a/onadata/libs/serializers/project_serializer.py b/onadata/libs/serializers/project_serializer.py index 0f86597f7c..833f5b43f8 100644 --- a/onadata/libs/serializers/project_serializer.py +++ b/onadata/libs/serializers/project_serializer.py @@ -218,11 +218,51 @@ class BaseProjectXFormSerializer(serializers.HyperlinkedModelSerializer): formid = serializers.ReadOnlyField(source="id") name = serializers.ReadOnlyField(source="title") + contributes_entities_to = serializers.SerializerMethodField() + consumes_entities_from = serializers.SerializerMethodField() + + def get_contributes_entities_to(self, obj: XForm): + """Return the EntityList that the form contributes Entities to""" + registration_form = obj.registration_forms.first() + + if registration_form is None: + return None + + return { + "id": registration_form.entity_list.pk, + "name": registration_form.entity_list.name, + "is_active": registration_form.is_active, + } + + def get_consumes_entities_from(self, obj: XForm): + """Return the EntityLIst that the form consumes Entities""" + queryset = obj.follow_up_forms.all() + + if not queryset: + return [] + + return list( + map( + lambda follow_up_form: { + "id": follow_up_form.entity_list.pk, + "name": follow_up_form.entity_list.name, + "is_active": follow_up_form.is_active, + }, + queryset, + ) + ) # pylint: disable=too-few-public-methods,missing-class-docstring class Meta: model = XForm - fields = ("name", "formid", "id_string", "is_merged_dataset") + fields = ( + "name", + "formid", + "id_string", + "is_merged_dataset", + "contributes_entities_to", + "consumes_entities_from", + ) # pylint: disable=too-few-public-methods