summaryrefslogtreecommitdiff
path: root/django/views
diff options
context:
space:
mode:
authorAdrian Holovaty <adrian@holovaty.com>2005-10-22 00:04:55 +0000
committerAdrian Holovaty <adrian@holovaty.com>2005-10-22 00:04:55 +0000
commit4f47ef85f8907f7b75ae12ff2c7ff4d4b3a3c26d (patch)
tree0ff7112e01e2734741e49b9ff5dec28eec3404ea /django/views
parentcc3635d62fa414ea7e4a263f0ae8e141b62fa8b6 (diff)
Added django.views.decorators.auth.user_passes_test, which is a more generic hook into authentication based on a test. Refactored login_required to use user_passes_test
git-svn-id: http://code.djangoproject.com/svn/django/trunk@988 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'django/views')
-rw-r--r--django/views/decorators/auth.py19
1 files changed, 13 insertions, 6 deletions
diff --git a/django/views/decorators/auth.py b/django/views/decorators/auth.py
index ae27fe33a1..f543a6aa48 100644
--- a/django/views/decorators/auth.py
+++ b/django/views/decorators/auth.py
@@ -1,12 +1,19 @@
-def login_required(view_func):
+def user_passes_test(view_func, test_func):
"""
- Decorator for views that checks that the user is logged in, redirecting
- to the log-in page if necessary.
+ 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.
"""
from django.views.auth.login import redirect_to_login
def _checklogin(request, *args, **kwargs):
- if request.user.is_anonymous():
- return redirect_to_login(request.path)
- else:
+ if test_func(request.user):
return view_func(request, *args, **kwargs)
+ return redirect_to_login(request.path)
return _checklogin
+
+def login_required(view_func):
+ """
+ Decorator for views that checks that the user is logged in, redirecting
+ to the log-in page if necessary.
+ """
+ return user_passes_test(lambda u: not u.is_anonymous())