Ticket #4617: decorators.2.diff
File decorators.2.diff, 1.9 KB (added by , 15 years ago) |
---|
-
django/contrib/auth/decorators.py
4 4 from django.utils.functional import update_wrapper, wraps # Python 2.4 fallback. 5 5 6 6 from django.contrib.auth import REDIRECT_FIELD_NAME 7 from django.http import HttpResponseRedirect 7 from django.http import HttpResponseRedirect, HttpResponseForbidden 8 from django.template import RequestContext, loader 8 9 from django.utils.decorators import available_attrs 9 10 from django.utils.http import urlquote 10 11 11 12 12 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME ):13 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME, template_403='403.html'): 13 14 """ 14 15 Decorator for views that checks that the user passes the given test, 15 16 redirecting to the log-in page if necessary. The test should be a callable … … 23 24 def _wrapped_view(request, *args, **kwargs): 24 25 if test_func(request.user): 25 26 return view_func(request, *args, **kwargs) 26 path = urlquote(request.get_full_path()) 27 tup = login_url, redirect_field_name, path 28 return HttpResponseRedirect('%s?%s=%s' % tup) 27 elif not request.user.is_authenticated(): 28 path = urlquote(request.get_full_path()) 29 tup = login_url, redirect_field_name, path 30 return HttpResponseRedirect('%s?%s=%s' % tup) 31 else: 32 # authorization test failed for authenticated user 33 t = loader.get_template(template_403) # You need to create a 403.html template. 34 return HttpResponseForbidden(t.render(RequestContext(request))) 29 35 return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view) 30 36 return decorator 31 37