-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
39 lines (30 loc) · 1.16 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
from functools import wraps
from urllib.parse import urlparse, urljoin
from flask import render_template, redirect, flash, request
from flask_login import current_user, login_required
def render(template, **kwargs):
parameters = {
"current_user": current_user,
}
parameters.update(kwargs)
return render_template(template, **parameters)
def admin_required(func):
"""
If you decorate a view with this, it will ensure that the current user is admin.
"""
@wraps(func)
def decorated_view(*args, **kwargs):
if not current_user.is_admin:
flash("Tato stránka je dostupná pouze organizátorům.", "danger")
return redirect("/")
return func(*args, **kwargs)
return login_required(decorated_view)
def is_safe_url(target):
"""
Ensures that a redirect target will lead to the same server.
Taken from: <https://web.archive.org/web/20120517003641/http://flask.pocoo.org/snippets/62/>
"""
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