Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add count parameter to media endpoint #1665

Merged
merged 9 commits into from
Aug 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion docs/media.rst
Original file line number Diff line number Diff line change
Expand Up @@ -185,4 +185,24 @@ Response

http://api.ona.io/api/v1/media/1.jpg


Retrieve attachment count for a form
------------------------------------
Returns the total number of attachments for a form

.. raw:: html

<pre class="prettyprint">GET /api/v1/media/count?xform=<code>{xform_id}</code></pre>

Example
^^^^^^^
::


curl -X GET https://api.ona.io/api/v1/media/count?xform=1

Response
^^^^^^^^
::


{"count": 1}
11 changes: 11 additions & 0 deletions onadata/apps/api/tests/viewsets/test_attachment_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def setUp(self):
self.list_view = AttachmentViewSet.as_view({
'get': 'list'
})
self.count_view = AttachmentViewSet.as_view({
'get': 'count'
})

self._publish_xls_form_to_project()

Expand Down Expand Up @@ -371,3 +374,11 @@ def test_direct_image_link_uppercase(self):
self.assertEqual(response.status_code, 200)
self.assertTrue(isinstance(response.data, basestring))
self.assertEqual(response.data, attachment_url(self.attachment))

def test_total_count(self):
self._submit_transport_instance_w_attachment()
xform_id = self.attachment.instance.xform.id
request = self.factory.get(
'/count', data={"xform": xform_id}, **self.extra)
response = self.count_view(request)
self.assertEqual(response.data['count'], 1)
9 changes: 9 additions & 0 deletions onadata/apps/api/viewsets/attachment_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.conf import settings
from rest_framework import renderers
from rest_framework import viewsets
from rest_framework.decorators import action
from rest_framework.exceptions import ParseError
from rest_framework.response import Response

Expand Down Expand Up @@ -83,6 +84,14 @@ def retrieve(self, request, *args, **kwargs):

return Response(serializer.data)

@action(methods=['GET'], detail=False)
def count(self, request, *args, **kwargs):
data = {
"count": self.filter_queryset(self.get_queryset()).count()
}

return Response(data=data)

def list(self, request, *args, **kwargs):
if request.user.is_anonymous:
xform = request.query_params.get('xform')
Expand Down