Skip to content

Commit

Permalink
Drop demo_mode 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 if
settings.DEMO is True and if so, we'll enable demo mode. If it's
False, you can override it by adding the user to a predefined
group.
  • Loading branch information
ju5t committed Aug 25, 2018
1 parent 47d9254 commit ffbdce8
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ from drf_simplepermissions import is_demo

if is_demo(User, demo_group='demo'):
# We should run in demo mode when the user is in 'demo'

if is_demo(User, demo_mode=True):
# We should run in demo mode as it's set globally
```

You can override enable demo mode globally by adding `DEMO = True` to your
application's `settings.py`.

# Installation

Not documented yet.
20 changes: 13 additions & 7 deletions drf_simplepermissions/permissions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.conf import settings
from django.contrib.auth.models import User
from rest_framework import permissions
from drf_simplepermissions.exceptions import SimpleModeException
Expand All @@ -22,27 +23,32 @@ def has_permission(self, request, view):
return False


def is_demo(user, demo_group='demo', demo_mode=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.
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.
This allows you to setup demo accounts that are restricted in what they
can do or what they can see. Keep in mind that it is not intended to
be used as a permission_classes' class.'''
be used as a permission_classes' class as that has a different purpose.'''

try:
basestring
except NameError:
basestring = str

if demo_mode is not False:
if not isinstance(demo_mode, bool):
raise SimpleModeException('demo_mode {0} is unsupported'.format(
demo_mode,
if hasattr(settings, 'DEMO'):
if not isinstance(settings.DEMO, bool):
raise SimpleModeException('DEMO {0} is unsupported'.format(
settings.DEMO,
))

return demo_mode
if settings.DEMO:
return settings.DEMO

if isinstance(user, User):
if isinstance(demo_group, basestring):
Expand Down
16 changes: 13 additions & 3 deletions drf_simplepermissions/tests/test_permissions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.test import TestCase
from django.test import override_settings
from django.contrib.auth.models import User
from django.contrib.auth.models import Group
from django.contrib.auth.models import Permission
Expand Down Expand Up @@ -76,11 +77,13 @@ def setUp(self):
def test_demo_default(self):
self.assertEqual(is_demo(user=self.user), False)

@override_settings(DEMO=True)
def test_demo_mode_true(self):
self.assertEqual(is_demo(user=self.user, demo_mode=True), True)
self.assertEqual(is_demo(user=self.user), True)

@override_settings(DEMO=False)
def test_demo_mode_false(self):
self.assertEqual(is_demo(user=self.user, demo_mode=False), False)
self.assertEqual(is_demo(user=self.user), False)

def test_demo_group_default_name(self):
group = Group.objects.create(name='demo')
Expand All @@ -105,12 +108,19 @@ def test_demo_multiple_groups_true(self):

self.assertEqual(is_demo(user=self.user, demo_group=groups), 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)

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

@override_settings(DEMO='foo')
def test_demo_group_unsupported_demo_mode(self):
self.assertRaises(SimpleModeException, is_demo, user=self.user, demo_mode='foo') # noqa
self.assertRaises(SimpleModeException, is_demo, user=self.user)

def test_demo_unsupported_user_object(self):
self.assertEqual(is_demo(user=False), False)

0 comments on commit ffbdce8

Please sign in to comment.