Login

is_staff decorator

Author:
munhitsu
Posted:
August 15, 2008
Language:
Python
Version:
.96
Tags:
decorator auth
Score:
0 (after 2 ratings)

Decorator for views that checks that the user is staff, redirecting to the log-in page if necessary. A wrapper for user_passes_test decorator based on login_required

Possible usage: @is_staff def view....

urlpatterns = patterns('', (r'^databrowse/(.*)', is_staff(databrowse.site.root)), )

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#put it to your app/shared/decorators.py and than import when required

from django.contrib.auth.decorators import user_passes_test
from django.contrib.auth import REDIRECT_FIELD_NAME

def is_staff(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
    """
    Decorator for views that checks that the user is staff, redirecting
    to the log-in page if necessary.
    Possible usage:
    @is_staff
    def view....
    
    urlpatterns = patterns('',
        (r'^databrowse/(.*)', is_staff(databrowse.site.root)),
    )
    """
    actual_decorator = user_passes_test(
        lambda u: u.is_staff,
        redirect_field_name=redirect_field_name
    )
    if function:
        return actual_decorator(function)
    return actual_decorator

More like this

  1. login_required decorator that doesn't redirect by brutasse 4 years, 1 month ago
  2. anonymous_required decorator by yetty 1 year, 8 months ago
  3. API_magic API view decorator by jobs@flowgram.com 6 years, 9 months ago
  4. Internal view decorator by gsakkis 4 years, 8 months ago
  5. superuser only decorator by jedie 5 years, 9 months ago

Comments

munhitsu (on August 15, 2008):

Honestly speaking I wasn't aware

thanks

#

Please login first before commenting.