Ticket #4617: decorators.2.diff

File decorators.2.diff, 1.9 KB (added by django@…, 15 years ago)

Updated patch for Django 1.2 trunk / RC

  • django/contrib/auth/decorators.py

     
    44    from django.utils.functional import update_wrapper, wraps  # Python 2.4 fallback.
    55
    66from django.contrib.auth import REDIRECT_FIELD_NAME
    7 from django.http import HttpResponseRedirect
     7from django.http import HttpResponseRedirect, HttpResponseForbidden
     8from django.template import RequestContext, loader
    89from django.utils.decorators import available_attrs
    910from django.utils.http import urlquote
    1011
    1112
    12 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
     13def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME, template_403='403.html'):
    1314    """
    1415    Decorator for views that checks that the user passes the given test,
    1516    redirecting to the log-in page if necessary. The test should be a callable
     
    2324        def _wrapped_view(request, *args, **kwargs):
    2425            if test_func(request.user):
    2526                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)))
    2935        return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
    3036    return decorator
    3137
Back to Top