diff options
| author | Luke Plant <L.Plant.98@cantab.net> | 2007-11-07 22:45:07 +0000 |
|---|---|---|
| committer | Luke Plant <L.Plant.98@cantab.net> | 2007-11-07 22:45:07 +0000 |
| commit | 8eeb9feab071caf5ad568ce292d32a2ebf540f62 (patch) | |
| tree | 54301c5c290db761803b705996c814e84e42b462 /django | |
| parent | 8216abe74889cc867a4cf89e6708f37cec6b2e72 (diff) | |
Fixed #4376 -- login_required now works with bound methods. Thanks, Steven Bethard.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@6658 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/auth/decorators.py | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/django/contrib/auth/decorators.py b/django/contrib/auth/decorators.py index b6481ea52c..2f216ab758 100644 --- a/django/contrib/auth/decorators.py +++ b/django/contrib/auth/decorators.py @@ -8,19 +8,9 @@ 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 that takes the user object and returns True if the user passes. """ - if not login_url: - from django.conf import settings - login_url = settings.LOGIN_URL - def _dec(view_func): - def _checklogin(request, *args, **kwargs): - if test_func(request.user): - return view_func(request, *args, **kwargs) - return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, urlquote(request.get_full_path()))) - _checklogin.__doc__ = view_func.__doc__ - _checklogin.__dict__ = view_func.__dict__ - - return _checklogin - return _dec + def decorate(view_func): + return _CheckLogin(view_func, test_func, login_url, redirect_field_name) + return decorate def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME): """ @@ -42,3 +32,33 @@ def permission_required(perm, login_url=None): """ 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 + + 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) |
