Ticket #16797: active_login_required_path.diff

File active_login_required_path.diff, 1.7 KB (added by wim@…, 13 years ago)

a patch for ticket 16797: adding a stronger login_required decorator

  • decorators.py

     
     1"""Handy decorators, to decorate your views."""
     2
    13import urlparse
    24from functools import wraps
    35from django.conf import settings
     
    4850    return actual_decorator
    4951
    5052
     53def active_login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
     54    """
     55    Decorator for views that checks that the user is logged in and still active, redirecting
     56    to the log-in page if necessary.
     57    """
     58    actual_decorator = user_passes_test(
     59        lambda u: u.is_authenticated() and getattr(u, 'is_active', True),
     60        login_url=login_url,
     61        redirect_field_name=redirect_field_name
     62    )
     63    if function:
     64        return actual_decorator(function)
     65    return actual_decorator
     66
     67
    5168def permission_required(perm, login_url=None, raise_exception=False):
    5269    """
    5370    Decorator for views that checks whether a user has a particular permission
     
    5673    is raised.
    5774    """
    5875    def check_perms(user):
    59         # First check if the user has the permission (even anon users)
     76        """
     77        First check if the user has the permission (even anon users)
     78        In case the 403 handler should be called raise the exception
     79        As the last resort, show the login form
     80        """
    6081        if user.has_perm(perm):
    6182            return True
    62         # In case the 403 handler should be called raise the exception
    6383        if raise_exception:
    6484            raise PermissionDenied
    65         # As the last resort, show the login form
    6685        return False
    6786    return user_passes_test(check_perms, login_url=login_url)
Back to Top