2008-02-25 14:02:35 +08:00
try :
from functools import wraps
except ImportError :
2010-05-04 22:00:30 +08:00
from django . utils . functional import wraps # Python 2.4 fallback.
2008-02-25 14:02:35 +08:00
2010-12-02 08:44:35 +08:00
from django import template
2006-05-02 09:31:56 +08:00
from django . shortcuts import render_to_response
2010-12-02 08:44:35 +08:00
from django . utils . translation import ugettext as _
2005-10-18 12:21:07 +08:00
2010-12-02 08:44:35 +08:00
from django . contrib . admin . forms import AdminAuthenticationForm
from django . contrib . auth . views import login
from django . contrib . auth import REDIRECT_FIELD_NAME
2005-10-18 12:21:07 +08:00
def staff_member_required ( view_func ) :
"""
Decorator for views that checks that the user is logged in and is a staff
member , displaying the login page if necessary .
"""
def _checklogin ( request , * args , * * kwargs ) :
2010-01-11 00:51:13 +08:00
if request . user . is_active and request . user . is_staff :
2005-10-18 12:21:07 +08:00
# The user is valid. Continue to the admin page.
return view_func ( request , * args , * * kwargs )
2006-05-02 09:31:56 +08:00
assert hasattr ( request , ' session ' ) , " The Django admin requires session middleware to be installed. Edit your MIDDLEWARE_CLASSES setting to insert ' django.contrib.sessions.middleware.SessionMiddleware ' . "
2010-12-02 08:44:35 +08:00
defaults = {
' template_name ' : ' admin/login.html ' ,
' authentication_form ' : AdminAuthenticationForm ,
' extra_context ' : {
' title ' : _ ( ' Log in ' ) ,
' app_path ' : request . get_full_path ( ) ,
REDIRECT_FIELD_NAME : request . get_full_path ( ) ,
} ,
}
return login ( request , * * defaults )
2008-02-25 14:02:35 +08:00
return wraps ( view_func ) ( _checklogin )