diff --git a/README.md b/README.md index 559c130..05deeb8 100644 --- a/README.md +++ b/README.md @@ -26,12 +26,29 @@ views or other methods should be running in demo mode. ```python from drf_simplepermissions import is_demo -if is_demo(User, demo_group='demo'): +if is_demo(User): # We should run in demo mode when the user is in 'demo' ``` -You can override enable demo mode globally by adding `DEMO = True` to your -application's `settings.py`. +# Settings + +Settings can be managed in `settings.py`. + +## DEMO + +Global demo mode can be enabled by changing `settings.DEMO` to True. If +it's set to False you can override it by adding users to a group. You +can't override `settings.DEMO` when it's set to True. When global demo +mode is set to true, all users no matter their group membership will be +considered demo users. + +## DEMO_GROUPS + +`settings.DEMO_GROUPS` contains a string or a list of groups that should be +considered demo users. It will try to match against any group and if a match +is found, it will return true. It is set to 'demo' by default. Groups are +not managed by this module and should be added manually or with a custom +migration. # Installation diff --git a/drf_simplepermissions/permissions.py b/drf_simplepermissions/permissions.py index 7f43733..403a3fa 100644 --- a/drf_simplepermissions/permissions.py +++ b/drf_simplepermissions/permissions.py @@ -23,10 +23,10 @@ def has_permission(self, request, view): return False -def is_demo(user, demo_group='demo'): - '''is_demo checks if a user is added to a demo group or groups. If a - list of groups is supplied, it will try to match against any group and - if a match is found, it will return true. +def is_demo(user): + '''is_demo checks if a user is added to a demo group or groups. It will try + to match against any group and if a match is found, it will return true. It + uses `settings.DEMO_GROUPS` as its source to check against. Global demo mode can be enabled by changing `settings.DEMO` to True. If it's set to False you can override it by adding users to a group. You @@ -41,6 +41,8 @@ def is_demo(user, demo_group='demo'): except NameError: basestring = str + demo_groups = 'demo' + if hasattr(settings, 'DEMO'): if not isinstance(settings.DEMO, bool): raise SimpleModeException('DEMO {0} is unsupported'.format( @@ -50,11 +52,14 @@ def is_demo(user, demo_group='demo'): if settings.DEMO: return settings.DEMO + if hasattr(settings, 'DEMO_GROUPS'): + demo_groups = settings.DEMO_GROUPS + if isinstance(user, User): - if isinstance(demo_group, basestring): - return user.groups.filter(name=demo_group).exists() + if isinstance(demo_groups, basestring): + return user.groups.filter(name=demo_groups).exists() - if isinstance(demo_group, (list)): - return user.groups.filter(name__in=demo_group).exists() + if isinstance(demo_groups, (list)): + return user.groups.filter(name__in=demo_groups).exists() return False diff --git a/drf_simplepermissions/tests/test_permissions.py b/drf_simplepermissions/tests/test_permissions.py index 82085dd..6ef4120 100644 --- a/drf_simplepermissions/tests/test_permissions.py +++ b/drf_simplepermissions/tests/test_permissions.py @@ -90,23 +90,26 @@ def test_demo_group_default_name(self): group.user_set.add(self.user) self.assertEqual(is_demo(user=self.user), True) + @override_settings(DEMO_GROUPS='foobar') def test_demo_group_custom_name(self): group = Group.objects.create(name='foobar') group.user_set.add(self.user) - self.assertEqual(is_demo(user=self.user, demo_group='foobar'), True) + self.assertEqual(is_demo(user=self.user), True) + @override_settings(DEMO_GROUPS='foobar') def test_demo_group_custom_name_with_default_group(self): group = Group.objects.create(name='demo') group.user_set.add(self.user) - self.assertEqual(is_demo(user=self.user, demo_group='foobar'), False) + self.assertEqual(is_demo(user=self.user), False) + @override_settings(DEMO_GROUPS=['group1', 'group2']) def test_demo_multiple_groups_true(self): groups = ['group1', 'group2'] for group_name in groups: group = Group.objects.create(name=group_name) group.user_set.add(self.user) - self.assertEqual(is_demo(user=self.user, demo_group=groups), True) + self.assertEqual(is_demo(user=self.user), True) @override_settings(DEMO=False) def test_demo_mode_for_global_false_but_user_in_demo_group(self): @@ -114,9 +117,9 @@ def test_demo_mode_for_global_false_but_user_in_demo_group(self): group.user_set.add(self.user) self.assertEqual(is_demo(user=self.user), True) + @override_settings(DEMO_GROUPS=type('demo_group', (), {})()) def test_demo_group_unsupported_object(self): - unsupported_group = type('demo_group', (), {})() - self.assertEqual(is_demo(user=self.user, demo_group=unsupported_group), False) # noqa + self.assertEqual(is_demo(user=self.user), False) # noqa @override_settings(DEMO='foo') def test_demo_group_unsupported_demo_mode(self):