Ticket #4617: decorator.diff
File decorator.diff, 3.5 KB (added by , 16 years ago) |
---|
-
decorators.py
old new 7 7 from django.http import HttpResponseRedirect 8 8 from django.utils.http import urlquote 9 9 10 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME , noperm_url = None):10 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): 11 11 """ 12 12 Decorator for views that checks that the user passes the given test, 13 13 redirecting to the log-in page if necessary. The test should be a callable 14 14 that takes the user object and returns True if the user passes. 15 15 """ 16 16 def decorate(view_func): 17 return _CheckLogin(view_func, test_func, login_url, redirect_field_name , noperm_url)17 return _CheckLogin(view_func, test_func, login_url, redirect_field_name) 18 18 return decorate 19 19 20 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 21 """ 22 22 Decorator for views that checks that the user is logged in, redirecting 23 23 to the log-in page if necessary. … … 25 25 actual_decorator = user_passes_test( 26 26 lambda u: u.is_authenticated(), 27 27 redirect_field_name=redirect_field_name, 28 noperm_url=noperm_url,29 28 ) 30 29 if function: 31 30 return actual_decorator(function) 32 31 return actual_decorator 33 32 34 def permission_required(perm, login_url=None , noperm_url=None):33 def permission_required(perm, login_url=None): 35 34 """ 36 35 Decorator for views that checks whether a user has a particular permission 37 36 enabled, redirecting to the log-in page if necessary. 38 37 """ 39 return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url , noperm_url=None)38 return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url) 40 39 41 40 class _CheckLogin(object): 42 41 """ … … 49 48 _CheckLogin object is used as a method decorator, the view function 50 49 is properly bound to its instance. 51 50 """ 52 def __init__(self, view_func, test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME , noperm_url=None):51 def __init__(self, view_func, test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME): 53 52 if not login_url: 54 53 from django.conf import settings 55 54 login_url = settings.LOGIN_URL 56 55 57 if not noperm_url:58 noperm_url = '/'59 60 56 self.view_func = view_func 61 57 self.test_func = test_func 62 58 self.login_url = login_url 63 59 self.redirect_field_name = redirect_field_name 64 self.noperm_url = noperm_url65 60 update_wrapper(self, view_func) 66 61 67 62 def __get__(self, obj, cls=None): 68 63 view_func = self.view_func.__get__(obj, cls) 69 return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name , self.noperm_url)64 return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name) 70 65 71 66 def __call__(self, request, *args, **kwargs): 72 67 if self.test_func(request.user): 73 68 return self.view_func(request, *args, **kwargs) 74 elif not request.user.is_authenticated(): 75 path = urlquote(request.get_full_path()) 76 tup = self.login_url, self.redirect_field_name, path 77 return HttpResponseRedirect('%s?%s=%s' % tup) 78 else: 79 return HttpResponseRedirect('%s' % self.noperm_url) 69 path = urlquote(request.get_full_path()) 70 tup = self.login_url, self.redirect_field_name, path 71 return HttpResponseRedirect('%s?%s=%s' % tup)