Skip to content

Commit

Permalink
Merge pull request #978 from onaio/clear-cache-when-moving-forms
Browse files Browse the repository at this point in the history
Clear PROJ_FORMS_CACHE when moving forms
  • Loading branch information
ukanga authored Mar 30, 2017
2 parents f7936cf + fd12348 commit 2c05587
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions onadata/apps/api/tests/viewsets/test_project_viewset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
Expand Down
2 changes: 2 additions & 0 deletions onadata/apps/api/tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 2c05587

Please sign in to comment.