-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathdecorators.py
121 lines (107 loc) · 4.18 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
"""define decorators for use across app."""
from datetime import date
from functools import wraps
from typing import Any, Callable, Dict, List, Optional, Union
import git
from flask import g, render_template, request
from database import EnumSymbol
from mod_auth.models import User
def get_menu_entries(user: Optional[User], title: str, icon: str, access: Optional[List] = None, route: str = '',
all_entries: Optional[List[Dict[str, Union[str, List[EnumSymbol]]]]] = None) -> Dict[Any, Any]:
"""
Parse a given set of entries and checks which ones the user can access.
:param access: Grant access to these roles. Empty means public access.
:type access: list[str]
:param user: The user object.
:type user: mod_auth.models.User
:param title: The title of the root menu entry.
:type title: str
:param icon: The icon of the root menu entry.
:type icon: str
:param route: The route of the root menu entry.
:type route: str
:param all_entries: The sub entries for this menu entry.
:type all_entries: list[dict]
:return: A dict consisting of the menu entry.
:rtype: dict
"""
if all_entries is None:
all_entries = []
if access is None:
access = []
result: Dict[Any, Any] = {
'title': title,
'icon': icon
}
allowed_entries = []
passed = False
if user is not None:
if len(route) > 0:
result['route'] = route
passed = len(access) == 0 or user.role in access
else:
for entry in all_entries:
# TODO: make this recursive if necessary
if len(entry['access']) == 0 or user.role in entry['access']:
allowed_entries.append(entry)
if len(allowed_entries) > 0:
result['entries'] = allowed_entries
passed = True
elif len(access) == 0:
if len(route) > 0:
result['route'] = route
passed = True
else:
for entry in all_entries:
# TODO: make this recursive if necessary
if len(entry['access']) == 0:
allowed_entries.append(entry)
if len(allowed_entries) > 0:
result['entries'] = allowed_entries
passed = True
return result if passed else {}
def template_renderer(template: Optional[str] = None, status: int = 200) -> Callable:
"""
Decorate to render a template.
:param template: The template if it's not equal to the name of the endpoint.
:type template: str
:param status: The return code
:type status: int
"""
def decorator(f):
@wraps(f)
def decorated_function(*args, **kwargs):
template_name = template
if template_name is None:
template_name = request.endpoint.replace('.', '/') + '.html'
ctx = f(*args, **kwargs)
if ctx is None:
ctx = {}
elif not isinstance(ctx, dict):
return ctx
# Add default values
ctx['applicationName'] = 'CCExtractor CI platform'
ctx['applicationVersion'] = getattr(g, 'version', 'Unknown')
ctx['currentYear'] = date.today().strftime('%Y')
ctx['build_commit'] = getattr(g, 'build_commit', "Unknown")
user = getattr(g, 'user', None)
ctx['user'] = user
# Create menu entries
menu_entries = getattr(g, 'menu_entries', {})
ctx['menu'] = [
menu_entries.get('home', {}),
menu_entries.get('samples', {}),
menu_entries.get('upload', {}),
menu_entries.get('custom', {}),
menu_entries.get('tests', {}),
menu_entries.get('regression', {}),
menu_entries.get('config', {}),
menu_entries.get('account', {}),
menu_entries.get('auth', {}),
menu_entries.get('signup', {}),
]
ctx['active_route'] = request.endpoint
# Render template & return
return render_template(template_name, **ctx), status
return decorated_function
return decorator