diff --git a/docs/profiles.rst b/docs/profiles.rst index 882c6d5f7a..2b9a42507e 100644 --- a/docs/profiles.rst +++ b/docs/profiles.rst @@ -240,9 +240,14 @@ Get the total number of monthly submissions This gets the total number of submissions made in a month to a specific user's forms. The result is a count of the submissions to both private and public forms. + +If there are no private forms then only the number of submissions to the public forms is returned, and vice versa. +If there are no submissions, then an empty dictionary is returned. + Use the month and year as query parameters to get the total number of submissions for a specific month. If no query parameters are used, the result is the number of submissions of the current month. -If one query parameter is used, then the other is assumed to be the current month or year. +If only month is used, then the year is assumed to be the current year. And if only year is passed, then the month is +assumed to be the current year. Example diff --git a/onadata/apps/api/tests/viewsets/test_user_profile_viewset.py b/onadata/apps/api/tests/viewsets/test_user_profile_viewset.py index e4747edd5f..ecbc287306 100644 --- a/onadata/apps/api/tests/viewsets/test_user_profile_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_user_profile_viewset.py @@ -852,7 +852,7 @@ def test_get_monthly_submissions(self): response = view(request, user=self.user.username) self.assertEquals(response.status_code, 200) self.assertFalse(self.xform.shared) - self.assertEquals(response.data, [{'private': count1}]) + self.assertEquals(response.data, {'private': count1}) # publish another form, make submission and make it public self._publish_form_with_hxl_support() @@ -867,8 +867,7 @@ def test_get_monthly_submissions(self): request = self.factory.get('/', **self.extra) response = view(request, user=self.user.username) self.assertEquals(response.status_code, 200) - self.assertEquals(response.data, [{'private': count1, - 'public': count2}]) + self.assertEquals(response.data, {'private': count1, 'public': count2}) def test_get_monthly_submissions_with_year_and_month_params(self): """ @@ -892,7 +891,7 @@ def test_get_monthly_submissions_with_year_and_month_params(self): response = view(request, user=self.user.username) self.assertEquals(response.status_code, 200) self.assertFalse(self.xform.shared) - self.assertEquals(response.data, [{'private': count}]) + self.assertEquals(response.data, {'private': count}) def test_monthly_submissions_with_month_param(self): """ @@ -914,7 +913,7 @@ def test_monthly_submissions_with_month_param(self): response = view(request, user=self.user.username) self.assertEquals(response.status_code, 200) self.assertFalse(self.xform.shared) - self.assertEquals(response.data, [{'private': count}]) + self.assertEquals(response.data, {'private': count}) def test_monthly_submissions_with_year_param(self): """ @@ -942,4 +941,4 @@ def test_monthly_submissions_with_year_param(self): response = view(request, user=self.user.username) self.assertEquals(response.status_code, 200) self.assertFalse(self.xform.shared) - self.assertEquals(response.data, [{'private': count}]) + self.assertEquals(response.data, {'private': count}) diff --git a/onadata/apps/api/viewsets/user_profile_viewset.py b/onadata/apps/api/viewsets/user_profile_viewset.py index 3fafb4db9b..98e7325a24 100644 --- a/onadata/apps/api/viewsets/user_profile_viewset.py +++ b/onadata/apps/api/viewsets/user_profile_viewset.py @@ -9,6 +9,7 @@ from past.builtins import basestring # pylint: disable=redefined-builtin from django.conf import settings +from django.core.validators import ValidationError from django.db.models import Count from rest_framework import serializers, status @@ -177,11 +178,20 @@ def monthly_submissions(self, request, *args, **kwargs): month_param = self.request.query_params.get('month', None) year_param = self.request.query_params.get('year', None) + # check if parameters are valid + if month_param: + if not month_param.isdigit() or \ + int(month_param) not in range(1, 13): + raise ValidationError(u'Invalid month provided as parameter') + if year_param: + if not year_param.isdigit() or len(year_param) != 4: + raise ValidationError(u'Invalid year provided as parameter') + # Use query parameter values for month and year # if none, use the current month and year now = datetime.datetime.now() - month = month_param if month_param else str(now.month) - year = year_param if year_param else str(now.year) + month = month_param if month_param else now.month + year = year_param if year_param else now.year instance_count = Instance.objects.filter( xform__user=profile.user, xform__deleted_at__isnull=True, @@ -189,4 +199,4 @@ def monthly_submissions(self, request, *args, **kwargs): 'xform__shared').annotate(num_instances=Count('id')) serializer = MonthlySubmissionsSerializer(instance_count, many=True) - return Response(serializer.data) + return Response(serializer.data[0]) diff --git a/onadata/libs/serializers/monthly_submissions_serializer.py b/onadata/libs/serializers/monthly_submissions_serializer.py index 39b30d0a5e..2fa779f690 100644 --- a/onadata/libs/serializers/monthly_submissions_serializer.py +++ b/onadata/libs/serializers/monthly_submissions_serializer.py @@ -13,7 +13,6 @@ def to_representation(self, data): for i in result: label = 'public' if i['xform__shared'] else 'private' result_dictionary[label] = i['num_instances'] - return [result_dictionary]