From 723d5b4d0d1c4e3626ec5707d44e0e592a0bad67 Mon Sep 17 00:00:00 2001 From: Dickson Ukang'a Date: Tue, 28 Mar 2017 15:10:12 +0300 Subject: [PATCH 1/3] raise ParseError on parial_update for forms when XLSFormError exception is raised --- onadata/apps/api/tests/viewsets/test_xform_viewset.py | 9 ++++++++- onadata/apps/api/viewsets/xform_viewset.py | 7 +++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/onadata/apps/api/tests/viewsets/test_xform_viewset.py b/onadata/apps/api/tests/viewsets/test_xform_viewset.py index 6316bdce02..6d1869581f 100644 --- a/onadata/apps/api/tests/viewsets/test_xform_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_xform_viewset.py @@ -1389,7 +1389,7 @@ def test_partial_update(self): view = XFormViewSet.as_view({ 'patch': 'partial_update' }) - title = u'مرحب' + title = u'Hello & World!' description = 'DESCRIPTION' data = {'public': True, 'description': description, 'title': title, 'downloadable': True} @@ -1398,6 +1398,13 @@ def test_partial_update(self): request = self.factory.patch('/', data=data, **self.extra) response = view(request, pk=self.xform.id) + self.assertEqual(response.status_code, 400) + + title = u'Hello and World!' + data['title'] = title + request = self.factory.patch('/', data=data, **self.extra) + response = view(request, pk=self.xform.id) + self.assertEqual(response.status_code, 200) self.xform.reload() self.assertTrue(self.xform.downloadable) diff --git a/onadata/apps/api/viewsets/xform_viewset.py b/onadata/apps/api/viewsets/xform_viewset.py index 4e74224196..3a968b9598 100644 --- a/onadata/apps/api/viewsets/xform_viewset.py +++ b/onadata/apps/api/viewsets/xform_viewset.py @@ -579,8 +579,11 @@ def partial_update(self, request, *args, **kwargs): 'text_xls_form']) & set(request.data.keys()): return _try_update_xlsform(request, self.object, owner) - return super(XFormViewSet, self).partial_update(request, *args, - **kwargs) + try: + return super(XFormViewSet, self).partial_update(request, *args, + **kwargs) + except XLSFormError as e: + raise ParseError(str(e)) @detail_route(methods=['DELETE', 'GET']) def delete_async(self, request, *args, **kwargs): From 6dee588bb092c2cff92fcef4e8dc83f56d7fe67c Mon Sep 17 00:00:00 2001 From: Dickson Ukang'a Date: Tue, 28 Mar 2017 18:28:38 +0300 Subject: [PATCH 2/3] initialize local variable xform to None in XFormManifestSerializer.get_hash --- onadata/libs/serializers/xform_serializer.py | 1 + 1 file changed, 1 insertion(+) diff --git a/onadata/libs/serializers/xform_serializer.py b/onadata/libs/serializers/xform_serializer.py index df8a865f09..e088c2cceb 100644 --- a/onadata/libs/serializers/xform_serializer.py +++ b/onadata/libs/serializers/xform_serializer.py @@ -358,6 +358,7 @@ def get_hash(self, obj): if len(parts) > 2: dataset_type = parts[0] pk = parts[1] + xform = None if dataset_type == 'xform': xform = XForm.objects.filter(pk=pk)\ .only('last_submission_time').first() From bda2ab02aae741411ef74d0f46680a160b622575 Mon Sep 17 00:00:00 2001 From: Dickson Ukang'a Date: Tue, 28 Mar 2017 18:35:51 +0300 Subject: [PATCH 3/3] add a check that role is truthy before assignment --- onadata/libs/utils/project_utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/onadata/libs/utils/project_utils.py b/onadata/libs/utils/project_utils.py index feaa161f86..cfc264d177 100644 --- a/onadata/libs/utils/project_utils.py +++ b/onadata/libs/utils/project_utils.py @@ -35,7 +35,8 @@ def set_project_perms_to_xform(xform, project): role_name = perm['role'] role = ROLES.get(role_name) - if user != xform.created_by: - role.add(user, xform) - else: + if user == xform.created_by: OwnerRole.add(user, xform) + else: + if role: + role.add(user, xform)