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