2006-05-02 09:31:56 +08:00
|
|
|
from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME
|
|
|
|
from django.http import HttpResponseRedirect
|
2006-05-22 10:46:55 +08:00
|
|
|
from urllib import quote
|
2005-11-26 15:20:07 +08:00
|
|
|
|
2006-05-02 09:31:56 +08:00
|
|
|
def user_passes_test(test_func, login_url=LOGIN_URL):
|
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
|
|
|
"""
|
2005-10-24 06:42:44 +08:00
|
|
|
def _dec(view_func):
|
|
|
|
def _checklogin(request, *args, **kwargs):
|
|
|
|
if test_func(request.user):
|
|
|
|
return view_func(request, *args, **kwargs)
|
2006-05-22 10:46:55 +08:00
|
|
|
return HttpResponseRedirect('%s?%s=%s' % (login_url, REDIRECT_FIELD_NAME, quote(request.get_full_path())))
|
2006-07-28 01:41:28 +08:00
|
|
|
_checklogin.__doc__ = view_func.__doc__
|
|
|
|
_checklogin.__dict__ = view_func.__dict__
|
2006-05-02 09:31:56 +08:00
|
|
|
|
2005-10-24 06:42:44 +08:00
|
|
|
return _checklogin
|
|
|
|
return _dec
|
2005-10-22 08:04:55 +08:00
|
|
|
|
2006-07-19 10:09:26 +08:00
|
|
|
login_required = user_passes_test(lambda u: u.is_authenticated())
|
2005-10-24 06:42:44 +08:00
|
|
|
login_required.__doc__ = (
|
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.
|
|
|
|
"""
|
2005-10-24 06:42:44 +08:00
|
|
|
)
|