vtt/src/ttfrog/web.py

52 lines
1.4 KiB
Python
Raw Normal View History

2025-09-27 16:20:08 -07:00
from flask import Response, render_template
from tinydb import where
2025-09-28 14:14:16 -07:00
from ttfrog import app
2025-09-21 22:11:56 -07:00
2025-09-28 14:14:16 -07:00
def get_page(stub):
"""
Get one page, including its subpages, but not recursively.
"""
app.web.logger.debug(f"Looking for page with {stub = }")
matches = app.db.Page.search(where("stub") == stub, recurse=False)
if not matches:
return
uids = [pointer.split("::")[-1] for pointer in matches[0].pages]
subpages = app.db.Page.search(where("uid").one_of(uids), recurse=False)
matches[0].pages = subpages
return matches[0]
2025-09-21 22:11:56 -07:00
2025-09-27 16:20:08 -07:00
@app.web.route("/")
def index():
2025-09-28 14:14:16 -07:00
page = get_page("Home")
if not page:
return Response("Home page not found", status=404)
return render_template("page.html", page=page)
2025-09-27 16:20:08 -07:00
2025-09-28 14:14:16 -07:00
@app.web.route("/view/<path:path>")
def page_view(path):
path = path.rstrip("/")
stub = path.split("/")[-1]
page = get_page(stub)
app.web.logger.debug(f"page_view: {path =} {stub =} {page =}")
2025-09-27 16:20:08 -07:00
if not page:
2025-09-28 14:14:16 -07:00
return Response(f"{stub} ({path}) not found", status=404)
return render_template(
"page.html",
page=page,
meta={
'uri': path,
}
)
@app.web.after_request
def add_header(r):
r.headers["Cache-Control"] = "no-cache, no-store, must-revalidate, public, max-age=0"
r.headers["Pragma"] = "no-cache"
r.headers["Expires"] = "0"
return r