PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/django/contrib/admin/views/decorators.py

https://code.google.com/p/mango-py/
Python | 32 lines | 24 code | 3 blank | 5 comment | 2 complexity | d151cd4a9fc1e2d69c65c44529f01fa5 MD5 | raw file
 1try:
 2    from functools import wraps
 3except ImportError:
 4    from django.utils.functional import wraps  # Python 2.4 fallback.
 5
 6from django.utils.translation import ugettext as _
 7from django.contrib.admin.forms import AdminAuthenticationForm
 8from django.contrib.auth.views import login
 9from django.contrib.auth import REDIRECT_FIELD_NAME
10
11def staff_member_required(view_func):
12    """
13    Decorator for views that checks that the user is logged in and is a staff
14    member, displaying the login page if necessary.
15    """
16    def _checklogin(request, *args, **kwargs):
17        if request.user.is_active and request.user.is_staff:
18            # The user is valid. Continue to the admin page.
19            return view_func(request, *args, **kwargs)
20
21        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'."
22        defaults = {
23            'template_name': 'admin/login.html',
24            'authentication_form': AdminAuthenticationForm,
25            'extra_context': {
26                'title': _('Log in'),
27                'app_path': request.get_full_path(),
28                REDIRECT_FIELD_NAME: request.get_full_path(),
29            },
30        }
31        return login(request, **defaults)
32    return wraps(view_func)(_checklogin)