Allow passing a function itself to the setting ROSETTA_ACCESS_CONTROL_FUNCTION.

This commit is contained in:
Alexander van Ratingen 2019-10-12 00:22:59 +02:00 committed by Alexander van Ratingn
parent 3c249d344e
commit 19442f8166
2 changed files with 19 additions and 7 deletions

View file

@ -15,13 +15,18 @@ def get_access_control_function():
Return a predicate for determining if a user can
access the Rosetta views
"""
fn_path = getattr(settings, 'ROSETTA_ACCESS_CONTROL_FUNCTION', None)
if fn_path is None:
access_function = getattr(settings, 'ROSETTA_ACCESS_CONTROL_FUNCTION', None)
if access_function is None:
return is_superuser_staff_or_in_translators_group
# Dynamically load a permissions function
perm_module, perm_func = fn_path.rsplit('.', 1)
perm_module = importlib.import_module(perm_module)
return getattr(perm_module, perm_func)
elif isinstance(access_function, str):
# Dynamically load a permissions function
perm_module, perm_func = access_function.rsplit('.', 1)
perm_module = importlib.import_module(perm_module)
return getattr(perm_module, perm_func)
elif callable(access_function):
return access_function
else:
raise TypeError(access_function)
# Default access control test

View file

@ -543,11 +543,18 @@ class RosettaTestCase(TestCase):
response = self.client.get(self.project_file_list_url)
self.assertEqual(200, response.status_code)
# Now replace access control, and check we get redirected
# Now replace access control with a function reference,
# and check we get redirected
with self.settings(ROSETTA_ACCESS_CONTROL_FUNCTION='rosetta.tests.no_access'):
response = self.client.get(self.project_file_list_url)
self.assertEqual(302, response.status_code)
# Now replace access control with a function itself,
# and check we get redirected
with self.settings(ROSETTA_ACCESS_CONTROL_FUNCTION=lambda user: False):
response = self.client.get(self.project_file_list_url)
self.assertEqual(302, response.status_code)
def test_26_urlconf_accept_dots_and_underscores(self):
resolver_match = resolve('/rosetta/files/all/fr_FR.utf8/0/')
self.assertEqual(resolver_match.url_name, 'rosetta-form')