Skip to content

Commit

Permalink
- Make documentation clearer
Browse files Browse the repository at this point in the history
- Add validation for the query parameters in the monthly submissions endpoint
- Return first item in the list, which is the dictionary with the total number of submissions
- Change the tests to check for a dictionary instead of a list of a dictionary
  • Loading branch information
Wambere committed May 29, 2018
1 parent f0aa70f commit 9b44213
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 11 deletions.
7 changes: 6 additions & 1 deletion docs/profiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 5 additions & 6 deletions onadata/apps/api/tests/viewsets/test_user_profile_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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):
"""
Expand Down Expand Up @@ -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})
16 changes: 13 additions & 3 deletions onadata/apps/api/viewsets/user_profile_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -177,16 +178,25 @@ 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,
date_created__year=year, date_created__month=month).values(
'xform__shared').annotate(num_instances=Count('id'))

serializer = MonthlySubmissionsSerializer(instance_count, many=True)
return Response(serializer.data)
return Response(serializer.data[0])
1 change: 0 additions & 1 deletion onadata/libs/serializers/monthly_submissions_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]


Expand Down

0 comments on commit 9b44213

Please sign in to comment.