Skip to content

Commit

Permalink
Drop demo_group as an attribute of is_demo()
Browse files Browse the repository at this point in the history
This would lead to a lot of duplicate code. Instead we now check
settings.DEMO_GROUPS for a list of valid demo groups.
  • Loading branch information
ju5t committed Aug 25, 2018
1 parent ffbdce8 commit c5b03c4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 16 deletions.
23 changes: 20 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
21 changes: 13 additions & 8 deletions drf_simplepermissions/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(
Expand All @@ -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
13 changes: 8 additions & 5 deletions drf_simplepermissions/tests/test_permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,33 +90,36 @@ 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):
group = Group.objects.create(name='demo')
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):
Expand Down

0 comments on commit c5b03c4

Please sign in to comment.