~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/contrib/admin/views/decorators.py

  • Committer: Package Import Robot
  • Author(s): Raphaël Hertzog
  • Date: 2014-09-17 14:15:11 UTC
  • mfrom: (1.3.17) (6.2.18 experimental)
  • Revision ID: package-import@ubuntu.com-20140917141511-icneokthe9ww5sk4
Tags: 1.7-2
* Release to unstable.
* Add a migrate-south sample script to help users apply their South
  migrations. Thanks to Brian May.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
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
1
from django.contrib.auth import REDIRECT_FIELD_NAME
6
 
 
7
 
 
8
 
def staff_member_required(view_func):
 
2
from django.contrib.auth.decorators import user_passes_test
 
3
 
 
4
 
 
5
def staff_member_required(view_func, redirect_field_name=REDIRECT_FIELD_NAME, login_url='admin:login'):
9
6
    """
10
7
    Decorator for views that checks that the user is logged in and is a staff
11
8
    member, displaying the login page if necessary.
12
9
    """
13
 
    @wraps(view_func)
14
 
    def _checklogin(request, *args, **kwargs):
15
 
        if request.user.is_active and request.user.is_staff:
16
 
            # The user is valid. Continue to the admin page.
17
 
            return view_func(request, *args, **kwargs)
18
 
 
19
 
        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'."
20
 
        defaults = {
21
 
            'template_name': 'admin/login.html',
22
 
            'authentication_form': AdminAuthenticationForm,
23
 
            'extra_context': {
24
 
                'title': _('Log in'),
25
 
                'app_path': request.get_full_path(),
26
 
                REDIRECT_FIELD_NAME: request.get_full_path(),
27
 
            },
28
 
        }
29
 
        return login(request, **defaults)
30
 
    return _checklogin
 
10
    return user_passes_test(
 
11
        lambda u: u.is_active and u.is_staff,
 
12
        login_url=login_url,
 
13
        redirect_field_name=redirect_field_name
 
14
    )(view_func)