summaryrefslogtreecommitdiff
path: root/django/views/decorators/auth.py
blob: 478f0ac84d8fbc240edaf3d815b66e2e5c9d725f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
from django.views.auth import login

def user_passes_test(test_func, login_url=login.LOGIN_URL):
    """
    Decorator for views that checks that the user passes the given test,
    redirecting to the log-in page if necessary. The test should be a callable
    that takes the user object and returns True if the user passes.
    """
    def _dec(view_func):
        def _checklogin(request, *args, **kwargs):
            if test_func(request.user):
                return view_func(request, *args, **kwargs)
            return login.redirect_to_login(request.path, login_url)
        return _checklogin
    return _dec

login_required = user_passes_test(lambda u: not u.is_anonymous())
login_required.__doc__ = (
    """
    Decorator for views that checks that the user is logged in, redirecting
    to the log-in page if necessary.
    """
    )