Ticket #4617: decorator.diff

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

redirect in case of test failed and user is already authenticated

  • decorators.py

    old new  
    77from django.http import HttpResponseRedirect
    88from django.utils.http import urlquote
    99
    10 def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME, noperm_url = None):
     10def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
    1111    """
    1212    Decorator for views that checks that the user passes the given test,
    1313    redirecting to the log-in page if necessary. The test should be a callable
    1414    that takes the user object and returns True if the user passes.
    1515    """
    1616    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)
    1818    return decorate
    1919
    20 def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, noperm_url = None):
     20def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
    2121    """
    2222    Decorator for views that checks that the user is logged in, redirecting
    2323    to the log-in page if necessary.
     
    2525    actual_decorator = user_passes_test(
    2626        lambda u: u.is_authenticated(),
    2727        redirect_field_name=redirect_field_name,
    28         noperm_url=noperm_url,
    2928    )
    3029    if function:
    3130        return actual_decorator(function)
    3231    return actual_decorator
    3332
    34 def permission_required(perm, login_url=None, noperm_url=None):
     33def permission_required(perm, login_url=None):
    3534    """
    3635    Decorator for views that checks whether a user has a particular permission
    3736    enabled, redirecting to the log-in page if necessary.
    3837    """
    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)
    4039
    4140class _CheckLogin(object):
    4241    """
     
    4948    _CheckLogin object is used as a method decorator, the view function
    5049    is properly bound to its instance.
    5150    """
    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):
    5352        if not login_url:
    5453            from django.conf import settings
    5554            login_url = settings.LOGIN_URL
    5655
    57         if not noperm_url:
    58             noperm_url = '/'
    59 
    6056        self.view_func = view_func
    6157        self.test_func = test_func
    6258        self.login_url = login_url
    6359        self.redirect_field_name = redirect_field_name
    64         self.noperm_url = noperm_url
    6560        update_wrapper(self, view_func)
    6661       
    6762    def __get__(self, obj, cls=None):
    6863        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)
    7065   
    7166    def __call__(self, request, *args, **kwargs):
    7267        if self.test_func(request.user):
    7368            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)
Back to Top