-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathdecorators.py
123 lines (100 loc) · 4.08 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
from django.core.exceptions import PermissionDenied
from Access.helpers import get_possible_approver_permissions
from django.core.paginator import Paginator
from django.template.response import TemplateResponse
def user_admin_or_ops(function):
def wrap(request, *args, **kwargs):
if request.user.user.is_admin_or_ops():
return function(request, *args, **kwargs)
else:
raise PermissionDenied
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
def authentication_classes(authentication_classes):
def decorator(func):
func.authentication_classes = authentication_classes
return func
return decorator
def user_with_permission(permissions_list):
def user_with_permission_decorator(function):
def wrap(request, *args, **kwargs):
if hasattr(request.user, "user"):
permission_labels = [
permission.label for permission in request.user.user.permissions
]
if len(set(permissions_list).intersection(permission_labels)) > 0:
return function(request, *args, **kwargs)
raise PermissionDenied
return wrap
return user_with_permission_decorator
def user_any_approver(function):
def wrap(request, *args, **kwargs):
all_approve_permissions = get_possible_approver_permissions()
is_any_approver = request.user.user.is_an_approver(all_approve_permissions)
if is_any_approver:
return function(request, *args, **kwargs)
else:
raise PermissionDenied
wrap.__doc__ = function.__doc__
wrap.__name__ = function.__name__
return wrap
def paginated_search(view_function):
def wrap(request, *args, **kwargs):
response = view_function(request, *args, **kwargs)
if type(response) is not tuple:
return response
template, context = response
if not context:
if type(template) == TemplateResponse:
return template.render()
return template
if context.get("error"):
template.context_data = context
return template.render()
page = int(request.GET.get("page", 1))
max_page_size = 25
key = context["search_data_key"]
search_rows = context["search_rows"]
filter_rows = context["filter_rows"]
search = request.GET.get("search")
filters = {}
for filter_row in filter_rows:
params = request.GET.getlist(filter_row)
if not params and len(params) == 0:
continue
filters[filter_row] = params
final_values = []
for value in context[key]:
in_final_values = True
if search:
in_any_search_row = False
for row in search_rows:
in_any_search_row = in_any_search_row or (search.lower() in value[row].lower())
in_final_values = in_any_search_row
if filters:
for row, val in filters.items():
if value[row] not in val:
in_final_values = in_final_values and False
if in_final_values:
final_values.append(value)
if len(final_values) != 0:
context[key] = final_values
else:
context[
"search_error"
] = "Please try adjusting your search criteria or browse by filters to find what you're looking for."
paginator = Paginator(context[key], max_page_size)
if not page:
page = 1
context[key] = paginator.get_page(1)
else:
context[key] = paginator.get_page(page)
context["maxPagination"] = int(paginator.num_pages)
context["allPages"] = range(max(1, page-5), min(paginator.num_pages + 1, page+5))
context["currentPagination"] = int(page)
template.context_data = context
return template.render()
wrap.__doc__ = view_function.__doc__
wrap.__name__ = view_function.__name__
return wrap