Skip to content

Commit

Permalink
add fields to forms exposed on endpoint /api/v1/projects
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvin-muchiri committed Feb 28, 2024
1 parent c474f9e commit d53d332
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
70 changes: 66 additions & 4 deletions onadata/apps/api/tests/viewsets/test_project_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
42 changes: 41 additions & 1 deletion onadata/libs/serializers/project_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d53d332

Please sign in to comment.