2008-02-25 14:02:35 +08:00
|
|
|
try:
|
2008-07-22 11:24:09 +08:00
|
|
|
from functools import update_wrapper
|
2008-02-25 14:02:35 +08:00
|
|
|
except ImportError:
|
2008-07-22 11:24:09 +08:00
|
|
|
from django.utils.functional import update_wrapper # Python 2.3, 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
|
2007-09-15 05:53:15 +08:00
|
|
|
from django.utils.http import urlquote
|
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
|
|
|
"""
|
2007-11-08 06:45:07 +08:00
|
|
|
def decorate(view_func):
|
|
|
|
return _CheckLogin(view_func, test_func, login_url, redirect_field_name)
|
|
|
|
return decorate
|
2005-10-22 08:04:55 +08:00
|
|
|
|
2007-09-15 03:25:37 +08:00
|
|
|
def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
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(),
|
|
|
|
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
|
|
|
|
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)
|
|
|
|
|
2007-11-08 06:45:07 +08:00
|
|
|
class _CheckLogin(object):
|
|
|
|
"""
|
|
|
|
Class that checks that the user passes the given test, redirecting to
|
|
|
|
the log-in page if necessary. If the test is passed, the view function
|
|
|
|
is invoked. The test should be a callable that takes the user object
|
|
|
|
and returns True if the user passes.
|
|
|
|
|
|
|
|
We use a class here so that we can define __get__. This way, when a
|
|
|
|
_CheckLogin object is used as a method decorator, the view function
|
|
|
|
is properly bound to its instance.
|
|
|
|
"""
|
|
|
|
def __init__(self, view_func, test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
|
|
|
|
if not login_url:
|
|
|
|
from django.conf import settings
|
|
|
|
login_url = settings.LOGIN_URL
|
|
|
|
self.view_func = view_func
|
|
|
|
self.test_func = test_func
|
|
|
|
self.login_url = login_url
|
|
|
|
self.redirect_field_name = redirect_field_name
|
2008-02-25 14:02:35 +08:00
|
|
|
update_wrapper(self, view_func)
|
2007-11-08 06:45:07 +08:00
|
|
|
|
|
|
|
def __get__(self, obj, cls=None):
|
|
|
|
view_func = self.view_func.__get__(obj, cls)
|
|
|
|
return _CheckLogin(view_func, self.test_func, self.login_url, self.redirect_field_name)
|
|
|
|
|
|
|
|
def __call__(self, request, *args, **kwargs):
|
|
|
|
if self.test_func(request.user):
|
|
|
|
return self.view_func(request, *args, **kwargs)
|
|
|
|
path = urlquote(request.get_full_path())
|
|
|
|
tup = self.login_url, self.redirect_field_name, path
|
|
|
|
return HttpResponseRedirect('%s?%s=%s' % tup)
|