mirror of https://github.com/django/django.git
Rewrote user_passes_test to use auto_adapt_to_methods, removing the need for _CheckLogin
git-svn-id: http://code.djangoproject.com/svn/django/trunk@11587 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
afeafcd492
commit
af02f38e02
|
@ -1,11 +1,12 @@
|
||||||
try:
|
try:
|
||||||
from functools import update_wrapper
|
from functools import update_wrapper, wraps
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from django.utils.functional import update_wrapper # Python 2.3, 2.4 fallback.
|
from django.utils.functional import update_wrapper, wraps # Python 2.3, 2.4 fallback.
|
||||||
|
|
||||||
from django.contrib.auth import REDIRECT_FIELD_NAME
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
||||||
from django.http import HttpResponseRedirect
|
from django.http import HttpResponseRedirect
|
||||||
from django.utils.http import urlquote
|
from django.utils.http import urlquote
|
||||||
|
from django.utils.decorators import auto_adapt_to_methods
|
||||||
|
|
||||||
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
||||||
"""
|
"""
|
||||||
|
@ -13,9 +14,19 @@ def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIE
|
||||||
redirecting to the log-in page if necessary. The test should be a callable
|
redirecting to the log-in page if necessary. The test should be a callable
|
||||||
that takes the user object and returns True if the user passes.
|
that takes the user object and returns True if the user passes.
|
||||||
"""
|
"""
|
||||||
def decorate(view_func):
|
if not login_url:
|
||||||
return _CheckLogin(view_func, test_func, login_url, redirect_field_name)
|
from django.conf import settings
|
||||||
return decorate
|
login_url = settings.LOGIN_URL
|
||||||
|
|
||||||
|
def decorator(view_func):
|
||||||
|
def _wrapped_view(request, *args, **kwargs):
|
||||||
|
if test_func(request.user):
|
||||||
|
return view_func(request, *args, **kwargs)
|
||||||
|
path = urlquote(request.get_full_path())
|
||||||
|
tup = login_url, redirect_field_name, path
|
||||||
|
return HttpResponseRedirect('%s?%s=%s' % tup)
|
||||||
|
return wraps(view_func)(_wrapped_view)
|
||||||
|
return auto_adapt_to_methods(decorator)
|
||||||
|
|
||||||
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
||||||
"""
|
"""
|
||||||
|
@ -36,46 +47,3 @@ def permission_required(perm, login_url=None):
|
||||||
enabled, redirecting to the log-in page if necessary.
|
enabled, redirecting to the log-in page if necessary.
|
||||||
"""
|
"""
|
||||||
return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
|
return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
|
||||||
|
|
||||||
class _CheckLogin(object):
|
|
||||||
"""
|
|
||||||
Class that checks that the user passes the given test, redirecting to
|
|
||||||
the log-in page if necessary. If the test is passed, the view function
|
|
||||||
is invoked. The test should be a callable that takes the user object
|
|
||||||
and returns True if the user passes.
|
|
||||||
|
|
||||||
We use a class here so that we can define __get__. This way, when a
|
|
||||||
_CheckLogin object is used as a method decorator, the view function
|
|
||||||
is properly bound to its instance.
|
|
||||||
"""
|
|
||||||
def __init__(self, view_func, test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
|
||||||
if not login_url:
|
|
||||||
from django.conf import settings
|
|
||||||
login_url = settings.LOGIN_URL
|
|
||||||
self.view_func = view_func
|
|
||||||
self.test_func = test_func
|
|
||||||
self.login_url = login_url
|
|
||||||
self.redirect_field_name = redirect_field_name
|
|
||||||
|
|
||||||
# We can't blindly apply update_wrapper because it udpates __dict__ and
|
|
||||||
# if the view function is already a _CheckLogin object then
|
|
||||||
# self.test_func and friends will get stomped. However, we also can't
|
|
||||||
# *not* update the wrapper's dict because then view function attributes
|
|
||||||
# don't get updated into the wrapper. So we need to split the
|
|
||||||
# difference: don't let update_wrapper update __dict__, but then update
|
|
||||||
# the (parts of) __dict__ that we care about ourselves.
|
|
||||||
update_wrapper(self, view_func, updated=())
|
|
||||||
for k in view_func.__dict__:
|
|
||||||
if k not in self.__dict__:
|
|
||||||
self.__dict__[k] = view_func.__dict__[k]
|
|
||||||
|
|
||||||
def __get__(self, obj, cls=None):
|
|
||||||
view_func = self.view_func.__get__(obj, cls)
|
|
||||||
return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name)
|
|
||||||
|
|
||||||
def __call__(self, request, *args, **kwargs):
|
|
||||||
if self.test_func(request.user):
|
|
||||||
return self.view_func(request, *args, **kwargs)
|
|
||||||
path = urlquote(request.get_full_path())
|
|
||||||
tup = self.login_url, self.redirect_field_name, path
|
|
||||||
return HttpResponseRedirect('%s?%s=%s' % tup)
|
|
||||||
|
|
Loading…
Reference in New Issue