45 lines
1.7 KiB
Python
45 lines
1.7 KiB
Python
from django.contrib.auth import REDIRECT_FIELD_NAME
|
|
from django.http import HttpResponseRedirect
|
|
from django.utils.http import urlquote
|
|
|
|
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
|
"""
|
|
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.
|
|
"""
|
|
if not login_url:
|
|
from django.conf import settings
|
|
login_url = settings.LOGIN_URL
|
|
def _dec(view_func):
|
|
def _checklogin(request, *args, **kwargs):
|
|
if test_func(request.user):
|
|
return view_func(request, *args, **kwargs)
|
|
return HttpResponseRedirect('%s?%s=%s' % (login_url, redirect_field_name, urlquote(request.get_full_path())))
|
|
_checklogin.__doc__ = view_func.__doc__
|
|
_checklogin.__dict__ = view_func.__dict__
|
|
|
|
return _checklogin
|
|
return _dec
|
|
|
|
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
|
"""
|
|
Decorator for views that checks that the user is logged in, redirecting
|
|
to the log-in page if necessary.
|
|
"""
|
|
actual_decorator = user_passes_test(
|
|
lambda u: u.is_authenticated(),
|
|
redirect_field_name=redirect_field_name
|
|
)
|
|
if function:
|
|
return actual_decorator(function)
|
|
return actual_decorator
|
|
|
|
def permission_required(perm, login_url=None):
|
|
"""
|
|
Decorator for views that checks whether a user has a particular permission
|
|
enabled, redirecting to the log-in page if necessary.
|
|
"""
|
|
return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
|
|
|