Skip to content

Commit

Permalink
last_submission_time for merged datasets at the forms endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ukanga committed Aug 7, 2017
1 parent 31b331d commit 8692573
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
15 changes: 14 additions & 1 deletion onadata/apps/api/tests/viewsets/test_merged_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def _create_merged_dataset(self, geo=False):
view = MergedXFormViewSet.as_view({
'post': 'create',
})
# pylint: disable=attribute-defined-outside-init
self.project = get_user_default_project(self.user)
xform1 = self._publish_md(MD, self.user, id_string='a')
xform2 = self._publish_md(MD, self.user, id_string='b')
Expand Down Expand Up @@ -224,6 +225,9 @@ def test_merged_datasets_data(self):
detail_view = MergedXFormViewSet.as_view({
'get': 'retrieve',
})
xform_detail_view = XFormViewSet.as_view({
'get': 'retrieve',
})

response = view(request, pk=merged_dataset['id'])
self.assertEqual(response.status_code, 200)
Expand Down Expand Up @@ -264,7 +268,7 @@ def test_merged_datasets_data(self):
expected_fruit = ['orange', 'mango']
self.assertEqual(fruit, expected_fruit)

# check num_of_submissions
# check num_of_submissions /merged-datasets/[pk]
response = detail_view(request, pk=merged_dataset['id'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['num_of_submissions'], 2)
Expand All @@ -273,6 +277,15 @@ def test_merged_datasets_data(self):
self.assertEqual(response.data['last_submission_time'],
last_submission.date_created)

# check num_of_submissions /forms/[pk]
response = xform_detail_view(request, pk=merged_dataset['id'])
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data['num_of_submissions'], 2)

# check last_submission_time
self.assertEqual(response.data['last_submission_time'],
last_submission.date_created)

def test_md_data_viewset(self):
"""Test retrieving data of a merged dataset at the /data endpoint"""
merged_dataset = self._create_merged_dataset()
Expand Down
22 changes: 22 additions & 0 deletions onadata/libs/serializers/xform_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,26 @@ def get_num_of_submissions(self, obj):
cache.set(key, count)
return count

def get_last_submission_time(self, obj):
"""Return datetime of last submission
If a form is a merged dataset then it is picked from the list of forms
attached to that merged dataset.
"""
if 'last_submission_time' not in self.fields:
return None

if obj.is_merged_dataset:
values = [
x.last_submission_time
for x in obj.mergedxform.xforms.only('last_submission_time')
if x.last_submission_time
]
if values:
return sorted(values, reverse=True)[0]

return obj.last_submission_time


class XFormBaseSerializer(XFormMixin, serializers.HyperlinkedModelSerializer):
formid = serializers.ReadOnlyField(source='id')
Expand All @@ -202,6 +222,7 @@ class XFormBaseSerializer(XFormMixin, serializers.HyperlinkedModelSerializer):
enketo_url = serializers.SerializerMethodField()
enketo_preview_url = serializers.SerializerMethodField()
num_of_submissions = serializers.SerializerMethodField()
last_submission_time = serializers.SerializerMethodField()
data_views = serializers.SerializerMethodField()

class Meta:
Expand Down Expand Up @@ -239,6 +260,7 @@ class XFormSerializer(XFormMixin, serializers.HyperlinkedModelSerializer):
enketo_url = serializers.SerializerMethodField()
enketo_preview_url = serializers.SerializerMethodField()
num_of_submissions = serializers.SerializerMethodField()
last_submission_time = serializers.SerializerMethodField()
form_versions = serializers.SerializerMethodField()
data_views = serializers.SerializerMethodField()

Expand Down

0 comments on commit 8692573

Please sign in to comment.