# Coverage for /Users/marco/Code/django-rosetta/rosetta/access.py : 66.67%

## 42 statements 29 run 13 missing 0 excluded 3 partial

Hot-keys on this page

r m x p toggle line displays

j k next/prev highlighted chunk

0 (zero) top of page

1 (one) first highlighted chunk

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | from django.conf import settings from rosetta.conf import settings as rosetta_settings from django.core.exceptions import ImproperlyConfigured try: import importlib except ImportError: from django.utils import importlib def can_translate(user): return get_access_control_function()(user) 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: 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) # Default access control test def is_superuser_staff_or_in_translators_group(user): if not getattr(settings, 'ROSETTA_REQUIRES_AUTH', True): return True try: 33 ↛ 34 line 33 didn't jump to line 34, because the condition on line 33 was never true if not user.is_authenticated: return False elif user.is_superuser and user.is_staff: return True else: return user.groups.filter(name='translators').exists() except AttributeError: if not hasattr(user, 'is_authenticated') or not hasattr(user, 'is_superuser') or not hasattr(user, 'groups'): raise ImproperlyConfigured('If you are using custom User Models you must implement a custom authentication method for Rosetta. See ROSETTA_ACCESS_CONTROL_FUNCTION here: https://django-rosetta.readthedocs.org/en/latest/settings.html') raise def can_translate_language(user, langid): try: if not rosetta_settings.ROSETTA_LANGUAGE_GROUPS: return can_translate(user) 49 ↛ 50 line 49 didn't jump to line 50, because the condition on line 49 was never true elif not user.is_authenticated: return False 51 ↛ 52 line 51 didn't jump to line 52, because the condition on line 51 was never true elif user.is_superuser and user.is_staff: return True else: return user.groups.filter(name='translators-%s' % langid).exists() except AttributeError: if not hasattr(user, 'is_authenticated') or not hasattr(user, 'is_superuser') or not hasattr(user, 'groups'): raise ImproperlyConfigured('If you are using custom User Models you must implement a custom authentication method for Rosetta. See ROSETTA_ACCESS_CONTROL_FUNCTION here: https://django-rosetta.readthedocs.org/en/latest/settings.html') raise