diff options
author | Jason A. Donenfeld <Jason@zx2c4.com> | 2009-10-19 20:20:09 -0400 |
---|---|---|
committer | Jason A. Donenfeld <Jason@zx2c4.com> | 2009-10-19 20:20:09 -0400 |
commit | 2d6dd2c5ade3f5fad3e2257dce52a6e188fe7535 (patch) | |
tree | da9c93d2f87df6d2b688a455a31e69859117ba1e /google_appengine/lib/django/django/contrib/auth/decorators.py | |
download | FramedPrototype-2d6dd2c5ade3f5fad3e2257dce52a6e188fe7535.tar.xz FramedPrototype-2d6dd2c5ade3f5fad3e2257dce52a6e188fe7535.zip |
Initial import.
Diffstat (limited to 'google_appengine/lib/django/django/contrib/auth/decorators.py')
-rwxr-xr-x | google_appengine/lib/django/django/contrib/auth/decorators.py | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/google_appengine/lib/django/django/contrib/auth/decorators.py b/google_appengine/lib/django/django/contrib/auth/decorators.py new file mode 100755 index 0000000..37e948f --- /dev/null +++ b/google_appengine/lib/django/django/contrib/auth/decorators.py @@ -0,0 +1,36 @@ +from django.contrib.auth import LOGIN_URL, REDIRECT_FIELD_NAME +from django.http import HttpResponseRedirect +from urllib import quote + +def user_passes_test(test_func, login_url=LOGIN_URL): + """ + 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. + """ + 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, quote(request.get_full_path()))) + _checklogin.__doc__ = view_func.__doc__ + _checklogin.__dict__ = view_func.__dict__ + + return _checklogin + return _dec + +login_required = user_passes_test(lambda u: u.is_authenticated()) +login_required.__doc__ = ( + """ + Decorator for views that checks that the user is logged in, redirecting + to the log-in page if necessary. + """ + ) + +def permission_required(perm, login_url=LOGIN_URL): + """ + 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) + |