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 error_message field to the Export serializer #2094

Merged
merged 3 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 14 additions & 3 deletions docs/forms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -308,14 +308,24 @@ Response
"job_status": "SUCCESS",
"task_id": "54b7159b-3b53-4e3c-b2a7-a5ed51adcfe9",
"type": "xls",
"xform": "http://api.ona.io/api/v1/forms/1"
"xform": "http://api.ona.io/api/v1/forms/1",
"error_message": ""
},
{
"id": 2,
"job_status": "PENDING",
"task_id": "54b7159b-3b53-4e3c-b2a7-a5ed51adcde9",
"type": "xls",
"xform": "http://api.ona.io/api/v1/forms/17"
"xform": "http://api.ona.io/api/v1/forms/17",
"error_message": ""
},
{
"id": 3,
"job_status": "FAILED",
"task_id": "54b7159b-3b53-4e3c-b2a7-a5ed51adcfe9",
"type": "xls",
"xform": "http://api.ona.io/api/v1/forms/20",
"error_message": "Something unexpected happened"
}]

Get a list of exports on a form
Expand All @@ -341,7 +351,8 @@ Response
"job_status": "SUCCESS",
"task_id": "54b7159b-3b53-4e3c-b2a7-a5ed51adcfe9",
"type": "xls",
"xform": "http://api.ona.io/api/v1/forms/1"
"xform": "http://api.ona.io/api/v1/forms/1",
"error_message": ""
}]

Export form data asynchronously
Expand Down
26 changes: 26 additions & 0 deletions onadata/apps/api/tests/viewsets/test_export_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,3 +496,29 @@ def test_export_retrieval_authentication(self):
request = self.factory.get('/export', **extra)
response = self.view(request, pk=export.pk)
self.assertEqual(response.status_code, 200)

def test_export_failure_reason_returned(self):
"""
Test that the reason an export failed is returned on the API
"""
self._create_user_and_login()
self._publish_transportation_form()
export = Export.objects.create(
xform=self.xform,
internal_status=Export.FAILED,
error_message="Something unexpected happened")

extra = {
'HTTP_AUTHORIZATION': f'Token {self.user.auth_token.key}',
}

view = ExportViewSet.as_view({'get': 'list'})
request = self.factory.get(
'/export', {'xform': self.xform.pk}, **extra)
force_authenticate(request)
response = view(request)
self.assertEqual(response.status_code, 200)
self.assertIn('error_message', response.data[0].keys())
self.assertEqual(
response.data[0]['error_message'],
'Something unexpected happened')
3 changes: 2 additions & 1 deletion onadata/libs/serializers/export_serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class ExportSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = Export
fields = ('id', 'job_status', 'type', 'task_id', 'xform',
'date_created', 'filename', 'options', 'export_url')
'date_created', 'filename', 'options', 'export_url',
'error_message')

def get_job_status(self, obj):
return status_msg.get(obj.internal_status)
Expand Down