feat: redirect /haiku to /haiku/<latest-id>

Refs: OPS-25
This commit is contained in:
Christoph J. Scherr 2025-02-25 18:05:16 +01:00
parent 588cbeb001
commit 44ad774ada
No known key found for this signature in database
GPG key ID: 9EB784BB202BB7BB
2 changed files with 49 additions and 21 deletions

View file

@ -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/<int:haiku_id>")
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")

View file

@ -26,23 +26,22 @@
<div class="ml-10 flex items-baseline space-x-4">
<!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
<a href="{{ url_for('index_view') }}"
class="rounded-md px-3 py-2 text-sm font-medium
{% if request.endpoint == 'index_view' %} bg-gray-900 text-white {% else %} text-gray-300 hover:bg-gray-700 hover:text-white {% endif %}"
>
Start
</a>
class="rounded-md px-3 py-2 text-sm font-medium
{% if request.endpoint == 'index_view' %} bg-gray-900 text-white {% else %} text-gray-300 hover:bg-gray-700 hover:text-white {% endif %}">
Start
</a>
<a href="{{ url_for('haiku_view') }}"
class="rounded-md px-3 py-2 text-sm font-mediRefs: OPS-11um
{% if request.endpoint == 'haiku_view' %} bg-gray-900 text-white {% else %} text-gray-300 hover:bg-gray-700 hover:text-white {% endif %}"
>
Haiku
</a>
<a href="{{ url_for('haiku_index_view') }}"
class="rounded-md px-3 py-2 text-sm font-mediRefs: OPS-11um
{% if request.endpoint == 'haiku_view' %} bg-gray-900 text-white {% else %} text-gray-300 hover:bg-gray-700 hover:text-white {% endif %}">
Haiku
</a>
<a href="#"
class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">
Information
</a> </div>
<a href="#"
class="rounded-md px-3 py-2 text-sm font-medium text-gray-300 hover:bg-gray-700 hover:text-white">
Information
</a>
</div>
</div>
</div>
<div class="hidden md:block">