2008-02-25 14:02:35 +08:00
|
|
|
try:
|
2009-09-22 06:34:45 +08:00
|
|
|
from functools import update_wrapper, wraps
|
2008-02-25 14:02:35 +08:00
|
|
|
except ImportError:
|
2010-05-04 22:00:30 +08:00
|
|
|
from django.utils.functional import update_wrapper, wraps # Python 2.4 fallback.
|
2008-02-25 14:02:35 +08:00
|
|
|
|
2007-04-25 16:49:57 +08:00
|
|
|
from django.contrib.auth import REDIRECT_FIELD_NAME
|
2006-05-02 09:31:56 +08:00
|
|
|
from django.http import HttpResponseRedirect
|
2010-04-09 19:07:17 +08:00
|
|
|
from django.utils.decorators import available_attrs
|
2007-09-15 05:53:15 +08:00
|
|
|
from django.utils.http import urlquote
|
2010-02-09 23:02:39 +08:00
|
|
|
|
2005-11-26 15:20:07 +08:00
|
|
|
|
2007-09-15 03:25:37 +08:00
|
|
|
def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2005-10-22 08:04:55 +08:00
|
|
|
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.
|
2005-07-13 09:25:57 +08:00
|
|
|
"""
|
2009-09-22 06:34:45 +08:00
|
|
|
if not login_url:
|
|
|
|
from django.conf import settings
|
|
|
|
login_url = settings.LOGIN_URL
|
|
|
|
|
|
|
|
def decorator(view_func):
|
|
|
|
def _wrapped_view(request, *args, **kwargs):
|
|
|
|
if test_func(request.user):
|
|
|
|
return view_func(request, *args, **kwargs)
|
|
|
|
path = urlquote(request.get_full_path())
|
|
|
|
tup = login_url, redirect_field_name, path
|
|
|
|
return HttpResponseRedirect('%s?%s=%s' % tup)
|
2010-04-09 19:07:17 +08:00
|
|
|
return wraps(view_func, assigned=available_attrs(view_func))(_wrapped_view)
|
2010-02-09 23:02:39 +08:00
|
|
|
return decorator
|
|
|
|
|
2005-10-22 08:04:55 +08:00
|
|
|
|
2010-09-11 03:38:57 +08:00
|
|
|
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
|
2005-10-22 08:04:55 +08:00
|
|
|
"""
|
|
|
|
Decorator for views that checks that the user is logged in, redirecting
|
|
|
|
to the log-in page if necessary.
|
|
|
|
"""
|
2007-09-15 03:25:37 +08:00
|
|
|
actual_decorator = user_passes_test(
|
|
|
|
lambda u: u.is_authenticated(),
|
2010-09-11 03:38:57 +08:00
|
|
|
login_url=login_url,
|
2007-09-15 03:25:37 +08:00
|
|
|
redirect_field_name=redirect_field_name
|
2005-10-24 06:42:44 +08:00
|
|
|
)
|
2007-09-15 03:25:37 +08:00
|
|
|
if function:
|
|
|
|
return actual_decorator(function)
|
|
|
|
return actual_decorator
|
2006-09-22 09:44:28 +08:00
|
|
|
|
2010-02-09 23:02:39 +08:00
|
|
|
|
2007-04-25 16:49:57 +08:00
|
|
|
def permission_required(perm, login_url=None):
|
2006-09-22 09:44:28 +08:00
|
|
|
"""
|
2006-09-26 01:33:17 +08:00
|
|
|
Decorator for views that checks whether a user has a particular permission
|
|
|
|
enabled, redirecting to the log-in page if necessary.
|
2006-09-22 09:44:28 +08:00
|
|
|
"""
|
|
|
|
return user_passes_test(lambda u: u.has_perm(perm), login_url=login_url)
|