-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
96 lines (74 loc) · 2.6 KB
/
helpers.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
import functools
from urllib.parse import urlparse, urljoin
from flask import request, abort
from flask_login import current_user
from dominate import tags
from flask_nav.renderers import Renderer
# http://flask.pocoo.org/snippets/62/
def is_safe_url(target):
ref_url = urlparse(request.host_url)
test_url = urlparse(urljoin(request.host_url, target))
return test_url.scheme in ('http', 'https') and \
ref_url.netloc == test_url.netloc
def require_admin(f):
"""
Require that the logged in user has admin permissions.
If the user has no admin permissions, a 403 error will be returned.
"""
@functools.wraps(f)
def decorator(*args, **kwargs):
if current_user.admin is not True:
abort(403)
return f(*args, **kwargs)
return decorator
def require_sensor_permission(f):
"""
Check if the current user (which may be logged in or not) has the permission to view data of this sensor.
This will be the case if a) the sensor is public or b) the user has been granted access to the sensor by an admin.
If the user has no permissions, a 403 error will be returned.
"""
@functools.wraps(f)
def decorator(*args, **kwargs):
from model import Sensor
sensor = Sensor.query.get(kwargs['id'])
if sensor is None:
return abort(404)
if sensor.public is True:
pass
else:
if current_user.is_authenticated is False or sensor not in current_user.sensors:
abort(403)
return f(*args, **kwargs)
return decorator
def gen_password(len: int = 10):
"""
Generate a random password.
:param len: Length of the password.
:return: The generated password
"""
import random
import string
choices = []
for i in range(0,len):
choices.append(random.choice(string.ascii_letters + string.digits))
return ''.join(choices)
class IconText():
"""
Workaround to store title and icon for items in flask-nav.
"""
def __init__(self, text: str, icon: str = 'square'):
self.text = text
self.icon = icon
class BootstrapNavRenderer(Renderer):
def visit_Navbar(self, node):
sub = []
for item in node.items:
sub.append(self.visit(item))
return tags.ul(cls='nav flex-column', *sub)
def visit_View(self, node):
active = 'active' if node.active else ''
return tags.li(
tags.a(tags.span(data_feather=node.text.icon), node.text.text, href=node.get_url(),
cls='nav-link {}'.format(active)),
cls='nav-item'
)