Skip to content

Instantly share code, notes, and snippets.

@grekodev
Last active November 10, 2017 17:15
Show Gist options
  • Save grekodev/2d68d59b64ddb23f5d249b5b2619a26c to your computer and use it in GitHub Desktop.
Save grekodev/2d68d59b64ddb23f5d249b5b2619a26c to your computer and use it in GitHub Desktop.
# put it to your app/shared/decorators.py and than import when required
from django.core.exceptions import PermissionDenied
from django.shortcuts import redirect
def superuser_only(function):
"""
Limit view to superusers only.
Usage:
--------------------------------------------------------------------------
@superuser_only
def my_view(request):
...
--------------------------------------------------------------------------
or in urls:
--------------------------------------------------------------------------
urlpatterns = patterns('',
(r'^foobar/(.*)', is_staff(my_view)),
)
--------------------------------------------------------------------------
"""
def _inner(request, *args, **kwargs):
if not request.user.is_superuser:
return redirect('/')
# raise PermissionDenied
return function(request, *args, **kwargs)
return _inner
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment