Ticket #684: django-auth-decorators.patch

File django-auth-decorators.patch, 1.5 KB (added by rjwittams, 19 years ago)
  • django/views/decorators/auth.py

    === django/views/decorators/auth.py
    ==================================================================
     
    1 def user_passes_test(view_func, test_func):
     1def user_passes_test(test_func):
    22    """
    33    Decorator for views that checks that the user passes the given test,
    44    redirecting to the log-in page if necessary. The test should be a callable
    55    that takes the user object and returns True if the user passes.
    66    """
    7     from django.views.auth.login import redirect_to_login
    8     def _checklogin(request, *args, **kwargs):
    9         if test_func(request.user):
    10             return view_func(request, *args, **kwargs)
    11         return redirect_to_login(request.path)
    12     return _checklogin
     7   
     8    def _dec(view_func):
     9        def _checklogin(request, *args, **kwargs):
     10            from django.views.auth.login import redirect_to_login
     11            if test_func(request.user):
     12                return view_func(request, *args, **kwargs)
     13            return redirect_to_login(request.path)
     14        return _checklogin
     15    return _dec
    1316
    14 def login_required(view_func):
     17
     18login_required = user_passes_test(lambda u: not u.is_anonymous())
     19login_required.__doc__ = (
    1520    """
    1621    Decorator for views that checks that the user is logged in, redirecting
    1722    to the log-in page if necessary.
    1823    """
    19     return user_passes_test(lambda u: not u.is_anonymous())
     24    )
     25   
Back to Top