Ticket #16797: active_login_required_path.diff
File active_login_required_path.diff, 1.7 KB (added by , 13 years ago) |
---|
-
decorators.py
1 """Handy decorators, to decorate your views.""" 2 1 3 import urlparse 2 4 from functools import wraps 3 5 from django.conf import settings … … 48 50 return actual_decorator 49 51 50 52 53 def active_login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None): 54 """ 55 Decorator for views that checks that the user is logged in and still active, redirecting 56 to the log-in page if necessary. 57 """ 58 actual_decorator = user_passes_test( 59 lambda u: u.is_authenticated() and getattr(u, 'is_active', True), 60 login_url=login_url, 61 redirect_field_name=redirect_field_name 62 ) 63 if function: 64 return actual_decorator(function) 65 return actual_decorator 66 67 51 68 def permission_required(perm, login_url=None, raise_exception=False): 52 69 """ 53 70 Decorator for views that checks whether a user has a particular permission … … 56 73 is raised. 57 74 """ 58 75 def check_perms(user): 59 # First check if the user has the permission (even anon users) 76 """ 77 First check if the user has the permission (even anon users) 78 In case the 403 handler should be called raise the exception 79 As the last resort, show the login form 80 """ 60 81 if user.has_perm(perm): 61 82 return True 62 # In case the 403 handler should be called raise the exception63 83 if raise_exception: 64 84 raise PermissionDenied 65 # As the last resort, show the login form66 85 return False 67 86 return user_passes_test(check_perms, login_url=login_url)