is_staff decorator

 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. Auth decorators with 403 by Magus 6 years ago
  2. login_required decorator that doesn't redirect by brutasse 2 years, 3 months ago
  3. Decorate every view in a url tree by sjzabel 1 year, 6 months ago
  4. Simple Age Verification Middleware by eculver 3 years, 9 months ago
  5. LoginAsForm - Login as any User without a password by johnboxall 3 years, 11 months ago

Comments

MasonM (on August 15, 2008):

uh, there's already a decorator for that built into Django.

I guess it's not not as versatile as yours, since the staff_member_required() decorator always displays the Admin login page, but unless you've got really specific requirements I don't see why you wouldn't want to use the Admin login page.

#

munhitsu (on August 15, 2008):

Honestly speaking I wasn't aware

thanks

#

(Forgotten your password?)