Code

Ticket #4617: decorator.diff

File decorator.diff, 3.5 KB (added by MilosU, 6 years ago)

redirect in case of test failed and user is already authenticated

Line 
1--- decorators.py       2008-08-21 16:09:07.962554500 +0200
2+++ decorators.py.orig  2008-08-21 16:10:22.165439900 +0200
3@@ -7,17 +7,17 @@
4 from django.http import HttpResponseRedirect
5 from django.utils.http import urlquote
6 
7-def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME, noperm_url = None):
8+def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
9     """
10     Decorator for views that checks that the user passes the given test,
11     redirecting to the log-in page if necessary. The test should be a callable
12     that takes the user object and returns True if the user passes.
13     """
14     def decorate(view_func):
15-        return _CheckLogin(view_func, test_func, login_url, redirect_field_name, noperm_url)
16+        return _CheckLogin(view_func, test_func, login_url, redirect_field_name)
17     return decorate
18 
19-def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, noperm_url = None):
20+def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
21     """
22     Decorator for views that checks that the user is logged in, redirecting
23     to the log-in page if necessary.
24@@ -25,18 +25,17 @@
25     actual_decorator = user_passes_test(
26         lambda u: u.is_authenticated(),
27         redirect_field_name=redirect_field_name,
28-       noperm_url=noperm_url,
29     )
30     if function:
31         return actual_decorator(function)
32     return actual_decorator
33 
34-def permission_required(perm, login_url=None, noperm_url=None):
35+def permission_required(perm, login_url=None):
36     """
37     Decorator for views that checks whether a user has a particular permission
38     enabled, redirecting to the log-in page if necessary.
39     """
40-    return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url, noperm_url=None)
41+    return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
42 
43 class _CheckLogin(object):
44     """
45@@ -49,31 +48,24 @@
46     _CheckLogin object is used as a method decorator, the view function
47     is properly bound to its instance.
48     """
49-    def __init__(self, view_func, test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME, noperm_url=None):
50+    def __init__(self, view_func, test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
51         if not login_url:
52             from django.conf import settings
53             login_url = settings.LOGIN_URL
54 
55-       if not noperm_url:
56-           noperm_url = '/'
57-
58         self.view_func = view_func
59         self.test_func = test_func
60         self.login_url = login_url
61         self.redirect_field_name = redirect_field_name
62-       self.noperm_url = noperm_url
63         update_wrapper(self, view_func)
64         
65     def __get__(self, obj, cls=None):
66         view_func = self.view_func.__get__(obj, cls)
67-        return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name, self.noperm_url)
68+        return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name)
69     
70     def __call__(self, request, *args, **kwargs):
71         if self.test_func(request.user):
72             return self.view_func(request, *args, **kwargs)
73-        elif not request.user.is_authenticated():
74-           path = urlquote(request.get_full_path())
75-           tup = self.login_url, self.redirect_field_name, path
76-           return HttpResponseRedirect('%s?%s=%s' % tup)
77-       else:
78-           return HttpResponseRedirect('%s' % self.noperm_url)
79+       path = urlquote(request.get_full_path())
80+       tup = self.login_url, self.redirect_field_name, path
81+       return HttpResponseRedirect('%s?%s=%s' % tup)