Skip to content

Commit

Permalink
Merge pull request #1200
Browse files Browse the repository at this point in the history
use_master when running set_project_perms_to_xform_async
  • Loading branch information
ukanga authored Jan 9, 2018
2 parents 8d89606 + ec88ffe commit 5b45a3d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
18 changes: 18 additions & 0 deletions onadata/libs/tests/utils/test_project_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from mock import patch

from django.test.utils import override_settings

from onadata.apps.logger.models import Project
from onadata.apps.main.tests.test_base import TestBase
from onadata.libs.permissions import DataEntryRole
from onadata.libs.utils.project_utils import set_project_perms_to_xform
from onadata.libs.utils.project_utils import set_project_perms_to_xform_async


class TestProjectUtils(TestBase):
Expand Down Expand Up @@ -33,3 +38,16 @@ def test_set_project_perms_to_xform(self):

# Alice should have no data entry role to transfered form
self.assertFalse(DataEntryRole.user_has_role(alice, self.xform))

@override_settings(CELERY_ALWAYS_EAGER=True)
@patch('onadata.libs.utils.project_utils.set_project_perms_to_xform')
def test_set_project_perms_to_xform_async(self, mock):
"""
Test that the set_project_perms_to_xform_async task actually calls
the set_project_perms_to_xform function
"""
set_project_perms_to_xform_async(self.xform.pk, self.project.pk)
self.assertTrue(mock.called)
args, kwargs = mock.call_args_list[0]
self.assertEqual(args[0], self.xform)
self.assertEqual(args[1], self.project)
39 changes: 23 additions & 16 deletions onadata/libs/utils/project_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,29 @@ def set_project_perms_to_xform(xform, project):
role.add(user, xform)


@task
def set_project_perms_to_xform_async(xform_id, project_id):
try:
xform = XForm.objects.get(id=xform_id)
project = Project.objects.get(id=project_id)
except (Project.DoesNotExist, XForm.DoesNotExist):
pass
else:
@task(bind=True, max_retries=3)
def set_project_perms_to_xform_async(self, xform_id, project_id):

def _set_project_perms():
try:
if len(getattr(settings, 'SLAVE_DATABASES', [])):
from multidb.pinning import use_master
with use_master:
set_project_perms_to_xform(xform, project)
else:
set_project_perms_to_xform(xform, project)
except Exception as e:
xform = XForm.objects.get(id=xform_id)
project = Project.objects.get(id=project_id)
except (Project.DoesNotExist, XForm.DoesNotExist) as e:
msg = '%s: Setting project %d permissions to form %d failed.' % (
type(e), project_id, xform_id)
type(e), project_id, xform_id)
report_exception(msg, e, sys.exc_info())
self.retry(countdown=60, exc=e)
else:
set_project_perms_to_xform(xform, project)

try:
if len(getattr(settings, 'SLAVE_DATABASES', [])):
from multidb.pinning import use_master
with use_master:
_set_project_perms()
else:
_set_project_perms()
except Exception as e:
msg = '%s: Setting project %d permissions to form %d failed.' % (
type(e), project_id, xform_id)
report_exception(msg, e, sys.exc_info())

0 comments on commit 5b45a3d

Please sign in to comment.