/django/contrib/admin/views/decorators.py
Python | 32 lines | 24 code | 3 blank | 5 comment | 4 complexity | d151cd4a9fc1e2d69c65c44529f01fa5 MD5 | raw file
Possible License(s): BSD-3-Clause
- try:
- from functools import wraps
- except ImportError:
- from django.utils.functional import wraps # Python 2.4 fallback.
- from django.utils.translation import ugettext as _
- from django.contrib.admin.forms import AdminAuthenticationForm
- from django.contrib.auth.views import login
- from django.contrib.auth import REDIRECT_FIELD_NAME
- 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):
- if request.user.is_active and request.user.is_staff:
- # The user is valid. Continue to the admin page.
- return view_func(request, *args, **kwargs)
- 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'."
- 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)
- return wraps(view_func)(_checklogin)