-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
54 lines (45 loc) · 1.38 KB
/
utils.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
from functools import wraps
from typing import Callable
from flask import (
Flask,
render_template,
session,
)
SESSION_NO_SPOILER = "noSpoiler"
SESSION_FULFILLED_WISHES = "fulfilledWishes"
SESSION_IS_LOGGED_IN = "isLoggedIn"
def setDefaultConfigValues(app):
# Default config value dict
defaultConfig = {
"OWNER_NAME": "Jemand",
"SQLALCHEMY_DATABASE_URI": "sqlite:///wishes.sqlite3",
"THEME_HUE": 260,
}
# for any key not already set, set the default value
for key, value in defaultConfig.items():
if key not in app.config:
app.config[key] = value
def admin(app: Flask):
def adminDecorator(f: Callable):
@wraps(f)
def adminWrapper(*args, **kwds):
if not session.get(SESSION_IS_LOGGED_IN, False):
return error(
app,
code=401,
title="Nicht eingeloggt!",
message="Bitte logge dich als Admin ein, um diese Seite zu benutzen.",
)
return f(*args, **kwds)
return adminWrapper
return adminDecorator
def error(app: Flask, code: int, title: str, message: str):
return (
render_template(
"error.html",
errorTitle=title,
errorMessage=message,
loggedIn=session.get(SESSION_IS_LOGGED_IN),
),
code,
)