=== django/views/decorators/auth.py
==================================================================
|
|
|
|
| 1 | | def user_passes_test(view_func, test_func): |
| | 1 | def user_passes_test(test_func): |
| 2 | 2 | """ |
| 3 | 3 | Decorator for views that checks that the user passes the given test, |
| 4 | 4 | redirecting to the log-in page if necessary. The test should be a callable |
| 5 | 5 | that takes the user object and returns True if the user passes. |
| 6 | 6 | """ |
| 7 | | from django.views.auth.login import redirect_to_login |
| 8 | | def _checklogin(request, *args, **kwargs): |
| 9 | | if test_func(request.user): |
| 10 | | return view_func(request, *args, **kwargs) |
| 11 | | return redirect_to_login(request.path) |
| 12 | | return _checklogin |
| | 7 | |
| | 8 | def _dec(view_func): |
| | 9 | def _checklogin(request, *args, **kwargs): |
| | 10 | from django.views.auth.login import redirect_to_login |
| | 11 | if test_func(request.user): |
| | 12 | return view_func(request, *args, **kwargs) |
| | 13 | return redirect_to_login(request.path) |
| | 14 | return _checklogin |
| | 15 | return _dec |
| 13 | 16 | |
| 14 | | def login_required(view_func): |
| | 17 | |
| | 18 | login_required = user_passes_test(lambda u: not u.is_anonymous()) |
| | 19 | login_required.__doc__ = ( |
| 15 | 20 | """ |
| 16 | 21 | Decorator for views that checks that the user is logged in, redirecting |
| 17 | 22 | to the log-in page if necessary. |
| 18 | 23 | """ |
| 19 | | return user_passes_test(lambda u: not u.is_anonymous()) |
| | 24 | ) |
| | 25 | |