Skip to content

Commit

Permalink
Merge pull request #543 from onaio/529-Add-download-form-as-XLSForm
Browse files Browse the repository at this point in the history
DW: Added download form as an xls functionality
  • Loading branch information
pld committed Sep 12, 2014
2 parents efff1e3 + 668db3d commit 8d3a5d2
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
2 changes: 2 additions & 0 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ def test_form_format(self):
response = view(request, pk=formid, format='xml')
self.assertEqual(response.status_code, 200)
response_doc = minidom.parseString(response.data)
response = view(request, pk=formid, format='xls')
self.assertEqual(response.status_code, 200)

xml_path = os.path.join(
settings.PROJECT_ROOT, "apps", "main", "tests", "fixtures",
Expand Down
19 changes: 16 additions & 3 deletions onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ def _get_owner(request):


def response_for_format(data, format=None):
formatted_data = data.xml if format == 'xml' else json.loads(data.json)
if format == 'xml':
formatted_data = data.xml
elif format == 'xls':
formatted_data = data.xls
else:
formatted_data = json.loads(data.json)
return Response(formatted_data)


Expand Down Expand Up @@ -339,7 +344,7 @@ class XFormViewSet(AnonymousUserPublicFormsMixin, LabelsMixin, ModelViewSet):
> ...
> }, ...]
## Get `JSON` | `XML` Form Representation
## Get `JSON` | `XML` | `XLS` Form Representation
<pre class="prettyprint">
<b>GET</b> /api/v1/forms/<code>{pk}</code>/form.\
<code>{format}</code></pre>
Expand Down Expand Up @@ -381,6 +386,14 @@ class XFormViewSet(AnonymousUserPublicFormsMixin, LabelsMixin, ModelViewSet):
> </h:body>
> </h:html>
> XLS Example
>
> curl -X GET https://ona.io/api/v1/forms/28058/form.xls
> Response
>
> Xls file downloaded
## Get list of forms with specific tag(s)
Use the `tags` query parameter to filter the list of forms, `tags` should be a
Expand Down Expand Up @@ -591,7 +604,7 @@ def create(self, request, *args, **kwargs):
@action(methods=['GET'])
def form(self, request, format='json', **kwargs):
form = self.get_object()
if format not in ['json', 'xml']:
if format not in ['json', 'xml', 'xls']:
return HttpResponseBadRequest('400 BAD REQUEST',
mimetype='application/json',
status=400)
Expand Down

0 comments on commit 8d3a5d2

Please sign in to comment.