Ticket #33299: decorators_sol3.py

File decorators_sol3.py, 4.1 KB (added by Iago González, 3 years ago)

Solution 3

Line 
1from functools import wraps
2from urllib.parse import urlparse
3
4from django.conf import settings
5from django.contrib.auth import REDIRECT_FIELD_NAME
6from django.core.exceptions import PermissionDenied
7from django.shortcuts import resolve_url
8
9
10def request_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
11 """
12 Decorator for views that checks that the request passes the given test,
13 redirecting to the log-in page if necessary. The test should be a callable
14 that takes the request object and returns True if the request passes.
15 """
16
17 def decorator(view_func):
18 @wraps(view_func)
19 def _wrapped_view(request, *args, **kwargs):
20 if test_func(request):
21 return view_func(request, *args, **kwargs)
22 path = request.build_absolute_uri()
23 resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
24 # If the login url is the same scheme and net location then just
25 # use the path as the "next" url.
26 login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
27 current_scheme, current_netloc = urlparse(path)[:2]
28 if ((not login_scheme or login_scheme == current_scheme) and
29 (not login_netloc or login_netloc == current_netloc)):
30 path = request.get_full_path()
31 from django.contrib.auth.views import redirect_to_login
32 return redirect_to_login(
33 path, resolved_login_url, redirect_field_name)
34 return _wrapped_view
35 return decorator
36
37
38def user_passes_test(test_func, login_url=None, redirect_field_name=REDIRECT_FIELD_NAME):
39 """
40 Decorator for views that checks that the user passes the given test,
41 redirecting to the log-in page if necessary. The test should be a callable
42 that takes the user object and returns True if the user passes.
43 """
44
45 def decorator(view_func):
46 @wraps(view_func)
47 def _wrapped_view(request, *args, **kwargs):
48 if test_func(request.user):
49 return view_func(request, *args, **kwargs)
50 path = request.build_absolute_uri()
51 resolved_login_url = resolve_url(login_url or settings.LOGIN_URL)
52 # If the login url is the same scheme and net location then just
53 # use the path as the "next" url.
54 login_scheme, login_netloc = urlparse(resolved_login_url)[:2]
55 current_scheme, current_netloc = urlparse(path)[:2]
56 if ((not login_scheme or login_scheme == current_scheme) and
57 (not login_netloc or login_netloc == current_netloc)):
58 path = request.get_full_path()
59 from django.contrib.auth.views import redirect_to_login
60 return redirect_to_login(
61 path, resolved_login_url, redirect_field_name)
62 return _wrapped_view
63 return decorator
64
65
66def login_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url=None):
67 """
68 Decorator for views that checks that the user is logged in, redirecting
69 to the log-in page if necessary.
70 """
71 actual_decorator = user_passes_test(
72 lambda u: u.is_authenticated,
73 login_url=login_url,
74 redirect_field_name=redirect_field_name
75 )
76 if function:
77 return actual_decorator(function)
78 return actual_decorator
79
80
81def permission_required(perm, login_url=None, raise_exception=False):
82 """
83 Decorator for views that checks whether a user has a particular permission
84 enabled, redirecting to the log-in page if necessary.
85 If the raise_exception parameter is given the PermissionDenied exception
86 is raised.
87 """
88 def check_perms(user):
89 if isinstance(perm, str):
90 perms = (perm,)
91 else:
92 perms = perm
93 # First check if the user has the permission (even anon users)
94 if user.has_perms(perms):
95 return True
96 # In case the 403 handler should be called raise the exception
97 if raise_exception:
98 raise PermissionDenied
99 # As the last resort, show the login form
100 return False
101 return user_passes_test(check_perms, login_url=login_url)
Back to Top