From 628305bddec9e4dfd17085542c9bac215b93cf48 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Fri, 28 May 2021 11:06:45 +0300 Subject: [PATCH 1/3] Add `error_message` field to the ExportSerializer --- .../api/tests/viewsets/test_export_viewset.py | 26 +++++++++++++++++++ onadata/libs/serializers/export_serializer.py | 3 ++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/onadata/apps/api/tests/viewsets/test_export_viewset.py b/onadata/apps/api/tests/viewsets/test_export_viewset.py index 48c5e640c1..748bdee551 100644 --- a/onadata/apps/api/tests/viewsets/test_export_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_export_viewset.py @@ -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') diff --git a/onadata/libs/serializers/export_serializer.py b/onadata/libs/serializers/export_serializer.py index 4e1c6aea3d..c10eab1029 100644 --- a/onadata/libs/serializers/export_serializer.py +++ b/onadata/libs/serializers/export_serializer.py @@ -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) From fd50e2bc3f3cd2463f653f9ef49a261b1787e475 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Fri, 28 May 2021 11:09:47 +0300 Subject: [PATCH 2/3] Update export functionality documentation --- docs/forms.rst | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/docs/forms.rst b/docs/forms.rst index 45245eab80..e59cb0e8c2 100644 --- a/docs/forms.rst +++ b/docs/forms.rst @@ -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 @@ -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 From 03551c7ad072704dd56a19fb2c26bec5b55e6150 Mon Sep 17 00:00:00 2001 From: Davis Raymond Muro Date: Fri, 28 May 2021 12:22:01 +0300 Subject: [PATCH 3/3] Fix failing test --- onadata/apps/api/tests/viewsets/test_export_viewset.py | 2 +- onadata/libs/tests/serializers/test_export_serializer.py | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_export_viewset.py b/onadata/apps/api/tests/viewsets/test_export_viewset.py index 748bdee551..afc905bfcb 100644 --- a/onadata/apps/api/tests/viewsets/test_export_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_export_viewset.py @@ -503,7 +503,7 @@ def test_export_failure_reason_returned(self): """ self._create_user_and_login() self._publish_transportation_form() - export = Export.objects.create( + Export.objects.create( xform=self.xform, internal_status=Export.FAILED, error_message="Something unexpected happened") diff --git a/onadata/libs/tests/serializers/test_export_serializer.py b/onadata/libs/tests/serializers/test_export_serializer.py index b3a2f8d0f1..7a60809f62 100644 --- a/onadata/libs/tests/serializers/test_export_serializer.py +++ b/onadata/libs/tests/serializers/test_export_serializer.py @@ -12,7 +12,7 @@ class TestExportSerializer(TestAbstractViewSet): - def test_project_serializer(self): + def test_export_serializer(self): request = APIRequestFactory().get('/') self._publish_xls_form_to_project() temp_dir = settings.MEDIA_ROOT @@ -34,7 +34,8 @@ def test_project_serializer(self): self.assertEqual(list(serializer.data), ['id', 'job_status', 'type', 'task_id', 'xform', 'date_created', 'filename', - 'options', 'export_url']) + 'options', 'export_url', + 'error_message']) self.assertEqual( serializer.data.get('export_url'), 'http://testserver/api/v1/export/%s.csv' % export[0].id