root / env / lib / python2.7 / site-packages / django / contrib / admin / views / decorators.py @ 1a305335
History | View | Annotate | Download (1.25 KB)
1 | 1a305335 | officers | from functools import wraps |
---|---|---|---|
2 | from django.utils.translation import ugettext as _ |
||
3 | from django.contrib.admin.forms import AdminAuthenticationForm |
||
4 | from django.contrib.auth.views import login |
||
5 | from django.contrib.auth import REDIRECT_FIELD_NAME |
||
6 | |||
7 | def staff_member_required(view_func): |
||
8 | """
|
||
9 | Decorator for views that checks that the user is logged in and is a staff
|
||
10 | member, displaying the login page if necessary.
|
||
11 | """
|
||
12 | @wraps(view_func)
|
||
13 | def _checklogin(request, *args, **kwargs): |
||
14 | if request.user.is_active and request.user.is_staff: |
||
15 | # The user is valid. Continue to the admin page.
|
||
16 | return view_func(request, *args, **kwargs)
|
||
17 | |||
18 | 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'." |
||
19 | defaults = { |
||
20 | 'template_name': 'admin/login.html', |
||
21 | 'authentication_form': AdminAuthenticationForm,
|
||
22 | 'extra_context': {
|
||
23 | 'title': _('Log in'), |
||
24 | 'app_path': request.get_full_path(),
|
||
25 | REDIRECT_FIELD_NAME: request.get_full_path(), |
||
26 | }, |
||
27 | } |
||
28 | return login(request, **defaults)
|
||
29 | return _checklogin |