Skip to content

Commit

Permalink
- Add documentation for new endpoint
Browse files Browse the repository at this point in the history
- Change query to be more efficient
- Optimize code in serializer
  • Loading branch information
Wambere committed May 28, 2018
1 parent 5b8a45c commit 650216b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 15 deletions.
43 changes: 43 additions & 0 deletions docs/profiles.rst
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,46 @@ Response
::

HTTP 200 OK

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.
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.


Example
^^^^^^^

::
curl -X GET https://api.ona.io/api/v1/profiles/demouser/monthly_submissions

Response
^^^^^^^^

::
[
{
"public": 41,
"private": 185
}
]

Example
^^^^^^^

::
curl -X GET https://api.ona.io/api/v1/profiles/demouser/monthly_submissions?month=5&year=2018

Response
^^^^^^^^

::
[
{
"public": 240
}
]
10 changes: 4 additions & 6 deletions onadata/apps/api/viewsets/user_profile_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,9 @@ def monthly_submissions(self, request, *args, **kwargs):
year = year_param if year_param else str(now.year)

instance_count = Instance.objects.filter(
xform__user=profile.user).filter(
xform__deleted_at__isnull=True).filter(
date_created__year=year).filter(
date_created__month=month).values(
'xform__shared').annotate(
num_instances=Count('id'))
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)
12 changes: 3 additions & 9 deletions onadata/libs/serializers/monthly_submissions_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ class MonthlySubmissionsListSerializer(serializers.ListSerializer):
def to_representation(self, data):
result = super(MonthlySubmissionsListSerializer,
self).to_representation(data)
states = [True, False]
result_dictionary = {}
for i in result:
label = 'public' if i['xform__shared'] else 'private'
result_dictionary[label] = i['num_instances']

def get_result():
for state in states:
for i in result:
if i['xform__shared'] == state:
label = 'public' if state else 'private'
result_dictionary[label] = i['num_instances']

get_result()
return [result_dictionary]


Expand Down

0 comments on commit 650216b

Please sign in to comment.