From 44ad774ada0905857bbd20d675cba77b1ff7f6e9 Mon Sep 17 00:00:00 2001 From: "Christoph J. Scherr" Date: Tue, 25 Feb 2025 18:05:16 +0100 Subject: [PATCH] feat: redirect /haiku to /haiku/ Refs: OPS-25 --- senju/main.py | 37 +++++++++++++++++++++++++++++++++---- senju/templates/base.html | 33 ++++++++++++++++----------------- 2 files changed, 49 insertions(+), 21 deletions(-) diff --git a/senju/main.py b/senju/main.py index e963f1b..195a3ff 100644 --- a/senju/main.py +++ b/senju/main.py @@ -1,16 +1,45 @@ -from flask import Flask, render_template +from logging import Logger +from pathlib import Path +from flask import Flask, redirect, render_template, url_for + +from senju.haiku import Haiku +from senju.store_manager import StoreManager app = Flask(__name__) +store = StoreManager(Path("/tmp/store.db")) + +testh = Haiku("hello world this is a haiku (not)") +some_uuid = store.save_haiku(testh) + @app.route("/") def index_view(): return render_template("index.html", title="Senju") -@app.route("/haiku") -def haiku_view(): +@app.route("/haiku/") +def haiku_index_view(): + haiku_id: int | None = store.get_id_of_latest_haiku() + if haiku_id is None: + # TODO: add "empty haiku list" error page + raise KeyError("no haiku exist yet") + return redirect(url_for("haiku_view", haiku_id=haiku_id)) + + +@app.route("/haiku/") +def haiku_view(haiku_id): + logger = Logger(__name__) + logger.debug(haiku_id) + haiku: Haiku | None = store.load_haiku(haiku_id) + if haiku is None: + # TODO: add "haiku not found" page + raise KeyError("haiku not found") + context: dict = { + "haiku": haiku + } return render_template( - "haiku.html", + "haiku.jinja", + context=context, title="Haiku of the Day") diff --git a/senju/templates/base.html b/senju/templates/base.html index d567c40..c48a7fe 100644 --- a/senju/templates/base.html +++ b/senju/templates/base.html @@ -25,24 +25,23 @@ -

{{ title }}