From f85b083637de1a31720612e6ba361eac1a3b66a7 Mon Sep 17 00:00:00 2001 From: ivermac Date: Thu, 30 Mar 2017 10:04:15 +0300 Subject: [PATCH 1/2] Clear cache data when moving forms btn projects This avoids duplication of forms on source and destination projects for the time the data is still in cache. --- onadata/apps/api/tools.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/onadata/apps/api/tools.py b/onadata/apps/api/tools.py index 51ee3412b9..afd72ce12e 100644 --- a/onadata/apps/api/tools.py +++ b/onadata/apps/api/tools.py @@ -39,6 +39,7 @@ from onadata.apps.viewer.models.export import Export from onadata.apps.viewer.models.parsed_instance import datetime_from_str from onadata.libs.utils.api_export_tools import custom_response_handler +from onadata.libs.utils.cache_tools import safe_delete, PROJ_FORMS_CACHE from onadata.libs.utils.logger_tools import publish_form from onadata.libs.utils.logger_tools import response_with_mimetype_and_name from onadata.libs.utils.project_utils import set_project_perms_to_xform @@ -336,6 +337,7 @@ def id_string_exists_in_account(): if 'formid' in request.data: xform = get_object_or_404(XForm, pk=request.data.get('formid')) + safe_delete('{}{}'.format(PROJ_FORMS_CACHE, xform.project.pk)) if not ManagerRole.user_has_role(request.user, xform): raise exceptions.PermissionDenied(_( "{} has no manager/owner role to the form {}". format( From fd12348705a2793498ffff8adf480d17e38b3dcb Mon Sep 17 00:00:00 2001 From: ivermac Date: Thu, 30 Mar 2017 10:08:48 +0300 Subject: [PATCH 2/2] check cache is cleared after form transfer --- .../tests/viewsets/test_project_viewset.py | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/onadata/apps/api/tests/viewsets/test_project_viewset.py b/onadata/apps/api/tests/viewsets/test_project_viewset.py index 43ab59a760..339fb4b646 100644 --- a/onadata/apps/api/tests/viewsets/test_project_viewset.py +++ b/onadata/apps/api/tests/viewsets/test_project_viewset.py @@ -537,6 +537,14 @@ def test_allow_form_transfer_if_org_is_owned_by_user( self, mock_send_mail): # create bob's project and publish a form to it self._publish_xls_form_to_project() + bobs_project = self.project + + view = ProjectViewSet.as_view({ + 'get': 'retrieve' + }) + # access bob's project initially to cache the forms list + request = self.factory.get('/', **self.extra) + view(request, pk=bobs_project.pk) # create an organization with a project self._org_create() @@ -545,16 +553,28 @@ def test_allow_form_transfer_if_org_is_owned_by_user( 'owner': 'http://testserver/api/v1/users/denoinc', 'public': False }) + org_project = self.project + + self.assertNotEqual(bobs_project.id, org_project.id) # try transfering bob's form to an organization project he created view = ProjectViewSet.as_view({ 'post': 'forms', + 'get': 'retrieve' }) post_data = {'formid': self.xform.id} request = self.factory.post('/', data=post_data, **self.extra) response = view(request, pk=self.project.id) + self.assertEqual(response.status_code, 201) + # test that cached forms of a source project are cleared. Bob had one + # forms initially and now it's been moved to the org project. + request = self.factory.get('/', **self.extra) + response = view(request, pk=bobs_project.pk) + bobs_results = response.data + self.assertListEqual(bobs_results.get('forms'), []) + @patch('onadata.apps.api.viewsets.project_viewset.send_mail') def test_handle_integrity_error_on_form_transfer(self, mock_send_mail): # create bob's project and publish a form to it