Skip to content

Commit

Permalink
Merge pull request #694 from onaio/690_return_json_object_in_xls_report
Browse files Browse the repository at this point in the history
Checks for a external export template
  • Loading branch information
ivermac committed Nov 18, 2014
2 parents dceb169 + b62efa2 commit 182cbd2
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 10 deletions.
4 changes: 4 additions & 0 deletions onadata/apps/api/tests/viewsets/test_xform_viewset.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# coding=utf-8
import json
import os
import re
import requests
Expand Down Expand Up @@ -624,6 +625,9 @@ def test_external_export_error(self):
format='xls')

self.assertEqual(response.status_code, 400)
data = json.loads(response.data)
self.assertTrue(data.get('error')
.startswith("J2X client could not generate report."))

def test_csv_import(self):
self._publish_xls_form_to_project()
Expand Down
28 changes: 23 additions & 5 deletions onadata/apps/api/viewsets/xform_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ def external_export_response(export):
else:
http_status = status.HTTP_400_BAD_REQUEST

return Response(export, http_status)
return Response(json.dumps(export), http_status,
content_type="application/json")


def log_export(request, xform, export_type):
Expand Down Expand Up @@ -571,15 +572,32 @@ class XFormViewSet(AnonymousUserPublicFormsMixin, LabelsMixin, ModelViewSet):
- `pk` - is the form unique identifier
- `format` - is the data export format i.e csv, xls, csvzip, savzip
Params for the custom xls report
- `meta` - the metadata id containing the template url
- `token` - the template url
<pre class="prettyprint">
<b>GET</b> /api/v1/exports/{pk}.{format}</code>
<b>GET</b> /api/v1/forms/{pk}.{format}</code>
</pre>
> Example
>
> curl -X GET https://ona.io/api/v1/exports/28058.xls
> curl -X GET https://ona.io/api/v1/forms/28058.xls
> binary file export of the format specied is returned as the response for the
> download.
> Binary file export of the format specified is returned as the response for
>the download.
>
> Response
>
> HTTP 200 OK
> Example 2 Custom XLS reports (beta)
>
> curl -X GET https://ona.io/api/v1/forms/28058.xls?meta=12121
> or
> curl -X GET https://ona.io/api/v1/forms/28058.xls?token={url}
>
> XLS file is downloaded
>
> Response
>
Expand Down
22 changes: 19 additions & 3 deletions onadata/apps/viewer/tests/test_export_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from onadata.apps.main.tests.test_base import TestBase
from onadata.apps.viewer.models.export import Export
from onadata.apps.main.models.meta_data import MetaData
from onadata.apps.viewer.views import export_list


Expand Down Expand Up @@ -95,10 +96,12 @@ def test_external_export_list(self):
kwargs = {'username': self.user.username,
'id_string': self.xform.id_string,
'export_type': Export.EXTERNAL_EXPORT}
server = \
'http://localhost:8080/xls/23fa4c38c0054748a984ffd89021a295'
server = 'http://localhost:8080/xls/23fa4c38c0054748a984ffd89021a295'
data_value = 'template 1 |{0}'.format(server)
meta = MetaData.external_export(self.xform, data_value)

custom_params = {
'token': server,
'meta': meta.id,
}
url = reverse(export_list, kwargs=kwargs)
count = len(Export.objects.all())
Expand All @@ -107,6 +110,19 @@ def test_external_export_list(self):
count1 = len(Export.objects.all())
self.assertEquals(count+1, count1)

def test_external_export_list_no_template(self):
kwargs = {'username': self.user.username,
'id_string': self.xform.id_string,
'export_type': Export.EXTERNAL_EXPORT}

url = reverse(export_list, kwargs=kwargs)
count = len(Export.objects.all())
response = self.client.get(url)
self.assertEqual(response.status_code, 403)
self.assertEquals(response.content, u'No XLS Template set.')
count1 = len(Export.objects.all())
self.assertEquals(count, count1)


class TestDataExportURL(TestBase):

Expand Down
22 changes: 20 additions & 2 deletions onadata/apps/viewer/tests/test_exports.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
create_export, export_progress, export_download
from onadata.apps.viewer.xls_writer import XlsWriter
from onadata.apps.viewer.models.export import Export
from onadata.apps.main.models.meta_data import MetaData
from onadata.apps.viewer.models.parsed_instance import ParsedInstance
from onadata.apps.logger.models import Instance
from onadata.apps.viewer.tasks import create_xls_export
Expand Down Expand Up @@ -1121,9 +1122,9 @@ def test_create_external_export_url(self):
self._publish_transportation_form()
self._submit_transport_instance()
num_exports = Export.objects.count()
from onadata.apps.main.models.meta_data import MetaData

server = 'http://localhost:8080/xls/23fa4c38c0054748a984ffd89021a295'
data_value = 'template 1 |' + server
data_value = 'template 1 |{0}'.format(server)
meta = MetaData.external_export(self.xform, data_value)

custom_params = {
Expand All @@ -1139,3 +1140,20 @@ def test_create_external_export_url(self):
response = self.client.post(create_export_url, custom_params)
self.assertEqual(response.status_code, 302)
self.assertEqual(Export.objects.count(), num_exports + 1)

def test_create_external_export_without_template(self):
self._publish_transportation_form()
self._submit_transport_instance()
num_exports = Export.objects.count()

# create export
create_export_url = reverse(create_export, kwargs={
'username': self.user.username,
'id_string': self.xform.id_string,
'export_type': Export.EXTERNAL_EXPORT
})

response = self.client.post(create_export_url)
self.assertEqual(response.status_code, 403)
self.assertEquals(response.content, u'No XLS Template set.')
self.assertEqual(Export.objects.count(), num_exports)
10 changes: 10 additions & 0 deletions onadata/apps/viewer/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@ def create_export(request, username, id_string, export_type):
if not has_permission(xform, owner, request):
return HttpResponseForbidden(_(u'Not shared.'))

if export_type == Export.EXTERNAL_EXPORT:
# check for template before trying to generate a report
if not MetaData.external_export(xform=xform):
return HttpResponseForbidden(_(u'No XLS Template set.'))

query = request.POST.get("query")
force_xlsx = request.POST.get('xls') != 'true'

Expand Down Expand Up @@ -380,6 +385,11 @@ def export_list(request, username, id_string, export_type):
xform = get_object_or_404(XForm, id_string__iexact=id_string, user=owner)
if not has_permission(xform, owner, request):
return HttpResponseForbidden(_(u'Not shared.'))

if export_type == Export.EXTERNAL_EXPORT:
# check for template before trying to generate a report
if not MetaData.external_export(xform=xform):
return HttpResponseForbidden(_(u'No XLS Template set.'))
# Get meta and token
export_token = request.GET.get('token')
export_meta = request.GET.get('meta')
Expand Down

0 comments on commit 182cbd2

Please sign in to comment.