-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
75 lines (52 loc) · 1.87 KB
/
app.py
File metadata and controls
75 lines (52 loc) · 1.87 KB
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
from flask import Flask, render_template, request, make_response, send_file
from jinja2.exceptions import TemplateNotFound
import toml
import json
from plugins import MarkdownExtension
def read_toml(filename):
with open(filename, "r") as f:
return toml.loads(f.read())
def add_unlocked(response, module, old_unlocked = None):
unlocked = old_unlocked or json.loads(
request.cookies.get("unlocked", "[]")
)
print(f"Add unlocked: {unlocked}")
if module not in unlocked:
unlocked.append(module)
print(unlocked)
response.set_cookie("unlocked", json.dumps(unlocked))
return unlocked
def get_unlocked():
return [0] + json.loads(
request.cookies.get("unlocked", "[]")
)
app = Flask(__name__)
app.jinja_env.add_extension(MarkdownExtension)
config = read_toml("config.toml")
module_map = {x["url"]: x for x in config["module"]}
@app.route("/")
def hello_world():
print(config)
response = render_template("index.html", config=config, unlocked=get_unlocked())
return response
@app.route("/module/<module_url>")
def module_view(module_url):
module = module_map[module_url]
module_id = module["id"]
module_name = module["name"]
if module_id != 0 and module_id not in get_unlocked():
return f"Module \"{module_name}\" not unlocked"
try:
response = make_response(
render_template(f"{module_url}.html",
complete=module_id in get_unlocked())
)
except TemplateNotFound:
response = make_response(f"Module \"{module_name}\" not yet implemented")
unlocked = add_unlocked(response, module_id)
for next_module_id in module.get("next", []):
add_unlocked(response, next_module_id, unlocked)
return response
@app.route("/file/<filename>")
def file_view(filename):
return send_file(f"file/{filename}", as_attachment=True)