Last active
November 10, 2017 17:15
-
-
Save grekodev/2d68d59b64ddb23f5d249b5b2619a26c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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