-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
Copy pathdecorators.py
38 lines (27 loc) · 1.13 KB
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from functools import wraps
def permission_required(*permissions, fn=None):
def decorator(view):
def wrapped_view(self, request, *args, **kwargs):
if callable(fn):
obj = fn(request, *args, **kwargs)
else:
obj = fn
missing_permissions = [perm for perm in permissions if not request.user.has_perm(perm, obj)]
if any(missing_permissions):
# raises a permission denied exception causing a 403 response
self.permission_denied(
request, message=('Permission denied: {}'.format(', '.join(missing_permissions)))
)
return view(self, request, *args, **kwargs)
return wrapped_view
return decorator
def override_report_only_csp(view_func):
"""
Decorator to switch report-only CSP to regular CSP. For use with core.middleware.HumanSignalCspMiddleware.
"""
@wraps(view_func)
def wrapper(*args, **kwargs):
response = view_func(*args, **kwargs)
setattr(response, '_override_report_only_csp', True)
return response
return wrapper