From d9044ff53e33326ffaec32042a2a8aca7cbbd92c Mon Sep 17 00:00:00 2001 From: Alivecow Date: Wed, 26 Mar 2025 19:36:39 +0100 Subject: [PATCH 1/3] feat: Add inital page to add random Haiku to starpage Refs: OPS-62 --- senju/main.py | 11 ++++++++++- senju/store_manager.py | 9 +++++++++ senju/templates/index.html | 22 +++++++++++++++++++++- 3 files changed, 40 insertions(+), 2 deletions(-) diff --git a/senju/main.py b/senju/main.py index 306a532..6a79c18 100644 --- a/senju/main.py +++ b/senju/main.py @@ -44,6 +44,7 @@ for the complete web interface are defined within this module. from __future__ import annotations import os +import random from pathlib import Path from flask import (Flask, redirect, render_template, request, @@ -65,7 +66,15 @@ def index_view(): :return: The index.html template with title "Senju". :rtype: flask.Response """ - return render_template("index.html", title="Senju") + random_number = random.randint(0, store.count_entries()) + haiku: Haiku | None = store.load_haiku(random_number) + if haiku is None: + raise KeyError("haiku not found") + context: dict = { + "haiku": haiku, + } + + return render_template("index.html", context=context, title="Senju") @app.route("/haiku/") diff --git a/senju/store_manager.py b/senju/store_manager.py index e94295b..aeb86a5 100644 --- a/senju/store_manager.py +++ b/senju/store_manager.py @@ -175,6 +175,15 @@ class StoreManager: """ return self._save(data.__dict__) + def count_entries(self) -> int: + """ + Query the store how many Haikus are stored. + + :return: Number of stored haikus. + :rtype: int + """ + return len(self._db.all()) + def get_id_of_latest_haiku(self) -> Optional[int]: """ Get the ID of the most recently added haiku. diff --git a/senju/templates/index.html b/senju/templates/index.html index 67a7d27..8b3103a 100644 --- a/senju/templates/index.html +++ b/senju/templates/index.html @@ -1,4 +1,24 @@ {% extends "base.html" %} {% block content %} -

+
+
+
+

{{ title }}

+

+ {% for line in context.haiku.lines %} + {{ line }}
+ {% endfor %} +

+
+ {% if context.is_default %} +
+ Note: No haikus have been found in the haiku store. +
+ {% endif %} + + Back to Home + +
+
{% endblock %} From 877eaafbaffd19653c443bb69651f86f9b6febf9 Mon Sep 17 00:00:00 2001 From: Alivecow Date: Wed, 26 Mar 2025 20:21:45 +0100 Subject: [PATCH 2/3] feat: Change Haiku retrival to only change on each day once Refs: OPS-62 --- senju/main.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/senju/main.py b/senju/main.py index 6a79c18..f807078 100644 --- a/senju/main.py +++ b/senju/main.py @@ -33,6 +33,7 @@ Dependencies * Flask: Core web application framework * Haiku: Custom class for poem representation and generation * StoreManager: Database abstraction for persistence operations +* datetime: Datetime helper to facilitate Haiku of the day Implementation -------------- @@ -45,6 +46,7 @@ from __future__ import annotations import os import random +from datetime import date from pathlib import Path from flask import (Flask, redirect, render_template, request, @@ -57,6 +59,8 @@ app = Flask(__name__) store = StoreManager(Path("/tmp/store.db")) +stored_date = date.today() + @app.route("/") def index_view(): @@ -66,7 +70,13 @@ def index_view(): :return: The index.html template with title "Senju". :rtype: flask.Response """ - random_number = random.randint(0, store.count_entries()) + global stored_date + + random_number = 1 + if stored_date != date.today(): + random_number = random.randint(0, store.count_entries()) + stored_date = date.today() + haiku: Haiku | None = store.load_haiku(random_number) if haiku is None: raise KeyError("haiku not found") @@ -74,7 +84,8 @@ def index_view(): "haiku": haiku, } - return render_template("index.html", context=context, title="Senju") + return render_template("index.html", context=context, + title="Haiku of the day") @app.route("/haiku/") From dc078b0b36dc1781aeb10df3c1946acb45f5358a Mon Sep 17 00:00:00 2001 From: Alivecow Date: Thu, 27 Mar 2025 09:07:55 +0100 Subject: [PATCH 3/3] refactor: Change location of haiku index to persist across funciton execution Refs: OPS-62 --- senju/main.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/senju/main.py b/senju/main.py index f807078..3434e76 100644 --- a/senju/main.py +++ b/senju/main.py @@ -60,6 +60,7 @@ app = Flask(__name__) store = StoreManager(Path("/tmp/store.db")) stored_date = date.today() +random_number = 1 @app.route("/") @@ -71,8 +72,8 @@ def index_view(): :rtype: flask.Response """ global stored_date + global random_number - random_number = 1 if stored_date != date.today(): random_number = random.randint(0, store.count_entries()) stored_date = date.today()