from flask import Response, render_template from tinydb import where from ttfrog import app 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] @app.web.route("/") def index(): page = get_page("Home") if not page: return Response("Home page not found", status=404) return render_template("page.html", page=page) @app.web.route("/view/") 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 =}") if not page: 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