-
Notifications
You must be signed in to change notification settings - Fork 1
/
helpers.py
53 lines (41 loc) · 1.58 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
from datetime import timedelta, datetime
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
from db_model import Puzzlehunt
def render(template, **kwargs):
parameters = {
"user": current_user,
"current_puzzlehunt_name": Puzzlehunt.get_current().puzzlehunt
}
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
def format_time(time):
if isinstance(time, timedelta):
hours = time.seconds // 3600
minutes = (time.seconds // 60) % 60
seconds = time.seconds % 60
return f"{hours:02}:{minutes:02}"
if isinstance(time, datetime):
return time.strftime("%H:%M")