From 69db48af8bb18e4e83a3adebea40d5d2ed66581c Mon Sep 17 00:00:00 2001
From: "Christoph J. Scherr"
Date: Sun, 23 Mar 2025 14:09:08 +0100
Subject: [PATCH 1/5] feat: add get_latest_haiku_or_default to store
Resf: OPS-65
---
senju/haiku.py | 4 ++++
senju/store_manager.py | 12 +++++++++++-
2 files changed, 15 insertions(+), 1 deletion(-)
diff --git a/senju/haiku.py b/senju/haiku.py
index 66d5348..f117a5f 100644
--- a/senju/haiku.py
+++ b/senju/haiku.py
@@ -59,3 +59,7 @@ class Haiku:
continue
return haiku
+
+
+DEFAULT_HAIKU: Haiku = Haiku(["Purple petals rise", "Defying fragile beauty",
+ "Fiercely breathing life"])
diff --git a/senju/store_manager.py b/senju/store_manager.py
index e99b98e..4c30543 100644
--- a/senju/store_manager.py
+++ b/senju/store_manager.py
@@ -7,7 +7,8 @@ from typing import Optional
from tinydb import TinyDB
from tinydb.queries import QueryImpl
-from senju.haiku import Haiku
+from senju import haiku
+from senju.haiku import DEFAULT_HAIKU, Haiku
DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json")
@@ -51,3 +52,12 @@ class StoreManager:
except IndexError as e:
self.logger.error(f"The database seems to be empty: {e}")
return None
+
+ def get_latest_haiku_or_default(self) -> Haiku:
+ id = self.get_id_of_latest_haiku()
+ if id is None:
+ return DEFAULT_HAIKU
+ haiku = self.load_haiku(id)
+ if haiku is None:
+ return DEFAULT_HAIKU
+ return haiku
From cf48ac81647163455523b7d5657503e0973490a0 Mon Sep 17 00:00:00 2001
From: "Christoph J. Scherr"
Date: Sun, 23 Mar 2025 14:09:26 +0100
Subject: [PATCH 2/5] test: add tests for get_latest_haiku_or_default
Refs: OPS-65
---
tests/test_store.py | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/tests/test_store.py b/tests/test_store.py
index dc770c7..d470cb8 100644
--- a/tests/test_store.py
+++ b/tests/test_store.py
@@ -2,9 +2,10 @@
# pytest fixtures to work
from __future__ import annotations
+from _pytest.mark.structures import store_mark
import pytest # noqa: F401
-from senju.haiku import Haiku
+from senju.haiku import DEFAULT_HAIKU, Haiku
from senju.store_manager import StoreManager # noqa: F401
@@ -42,3 +43,18 @@ def test_load_latest_with_empty_store(temp_data_dir):
store = StoreManager(temp_data_dir / "empty_store.json")
h = store.get_id_of_latest_haiku()
assert h is None
+
+
+def test_load_latest_or_default_with_empty(temp_data_dir):
+ store = StoreManager(temp_data_dir / "load_or_default_empty.json")
+ haiku = store.get_latest_haiku_or_default()
+ assert haiku == DEFAULT_HAIKU
+
+
+def test_load_latest_or_default_with_non_empty(temp_data_dir):
+ store = StoreManager(temp_data_dir / "load_or_default_not_empty.json")
+ nonsense_test_haiku = Haiku(["nonsense", "test", "haiku"])
+ store.save_haiku(nonsense_test_haiku)
+ haiku = store.get_latest_haiku_or_default()
+ assert haiku != DEFAULT_HAIKU
+ assert haiku == nonsense_test_haiku
From 8f3ccb94d104869043ba2fdea218e18de0f799b8 Mon Sep 17 00:00:00 2001
From: "Christoph J. Scherr"
Date: Sun, 23 Mar 2025 14:14:27 +0100
Subject: [PATCH 3/5] refactor: integrate get_latest_haiku_or_default into
load_haiku
Refs: OPS-65
---
senju/store_manager.py | 15 ++++-----------
tests/test_store.py | 5 +++--
2 files changed, 7 insertions(+), 13 deletions(-)
diff --git a/senju/store_manager.py b/senju/store_manager.py
index 4c30543..1cf3bc5 100644
--- a/senju/store_manager.py
+++ b/senju/store_manager.py
@@ -35,10 +35,12 @@ class StoreManager:
def _save(self, data: dict) -> int:
return self._db.insert(data)
- def load_haiku(self, key: int) -> Optional[Haiku]:
+ def load_haiku(self, key: Optional[int]) -> Haiku:
+ if key is None:
+ return DEFAULT_HAIKU
raw_haiku: dict | None = self._load(key)
if raw_haiku is None:
- return None
+ return DEFAULT_HAIKU
h = Haiku(**raw_haiku)
return h
@@ -52,12 +54,3 @@ class StoreManager:
except IndexError as e:
self.logger.error(f"The database seems to be empty: {e}")
return None
-
- def get_latest_haiku_or_default(self) -> Haiku:
- id = self.get_id_of_latest_haiku()
- if id is None:
- return DEFAULT_HAIKU
- haiku = self.load_haiku(id)
- if haiku is None:
- return DEFAULT_HAIKU
- return haiku
diff --git a/tests/test_store.py b/tests/test_store.py
index d470cb8..35ac981 100644
--- a/tests/test_store.py
+++ b/tests/test_store.py
@@ -37,6 +37,7 @@ def test_save_and_load_haiku(store_manager: StoreManager):
but should have"
assert h == h_loaded
+ assert h != DEFAULT_HAIKU
def test_load_latest_with_empty_store(temp_data_dir):
@@ -47,7 +48,7 @@ def test_load_latest_with_empty_store(temp_data_dir):
def test_load_latest_or_default_with_empty(temp_data_dir):
store = StoreManager(temp_data_dir / "load_or_default_empty.json")
- haiku = store.get_latest_haiku_or_default()
+ haiku = store.load_haiku(store.get_id_of_latest_haiku())
assert haiku == DEFAULT_HAIKU
@@ -55,6 +56,6 @@ def test_load_latest_or_default_with_non_empty(temp_data_dir):
store = StoreManager(temp_data_dir / "load_or_default_not_empty.json")
nonsense_test_haiku = Haiku(["nonsense", "test", "haiku"])
store.save_haiku(nonsense_test_haiku)
- haiku = store.get_latest_haiku_or_default()
+ haiku = store.load_haiku(store.get_id_of_latest_haiku())
assert haiku != DEFAULT_HAIKU
assert haiku == nonsense_test_haiku
From da52e643a75155fe3080d1c2d0a49b1d96b734dc Mon Sep 17 00:00:00 2001
From: "Christoph J. Scherr"
Date: Sun, 23 Mar 2025 14:26:54 +0100
Subject: [PATCH 4/5] feat: display the default haiku in the frontend with a
small note
Refs: OPS-65
---
senju/main.py | 14 ++++++--------
senju/templates/haiku.html | 5 +++++
2 files changed, 11 insertions(+), 8 deletions(-)
diff --git a/senju/main.py b/senju/main.py
index fd2e795..4f16689 100644
--- a/senju/main.py
+++ b/senju/main.py
@@ -24,20 +24,18 @@ def index_view():
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))
+ haiku_id = 0
+ return redirect(url_for("haiku_view", haiku_id=haiku_id, is_default=1))
@app.route("/haiku/")
def haiku_view(haiku_id):
"""test"""
- haiku: Haiku | None = store.load_haiku(haiku_id)
- if haiku is None:
- # TODO: add "haiku not found" page
- raise KeyError("haiku not found")
+ is_default: bool = request.args.get("is_default") == "1"
+ haiku: Haiku = store.load_haiku(haiku_id)
context: dict = {
- "haiku": haiku
+ "haiku": haiku,
+ "is_default": is_default
}
return render_template(
diff --git a/senju/templates/haiku.html b/senju/templates/haiku.html
index 844dfff..c3d4382 100644
--- a/senju/templates/haiku.html
+++ b/senju/templates/haiku.html
@@ -11,6 +11,11 @@
{% endfor %}
+ {% if context.is_default %}
+
+ Note: No haikus have been found in the haiku store.
+
+ {% endif %}
Back to Home
From 157032525619e8805f78bd8f9a067f5748de4948 Mon Sep 17 00:00:00 2001
From: "Christoph J. Scherr"
Date: Sun, 23 Mar 2025 14:27:44 +0100
Subject: [PATCH 5/5] chore: fix flake8 warnings
Refs: OPS-65
---
senju/store_manager.py | 1 -
tests/test_store.py | 1 -
2 files changed, 2 deletions(-)
diff --git a/senju/store_manager.py b/senju/store_manager.py
index 1cf3bc5..493ad29 100644
--- a/senju/store_manager.py
+++ b/senju/store_manager.py
@@ -7,7 +7,6 @@ from typing import Optional
from tinydb import TinyDB
from tinydb.queries import QueryImpl
-from senju import haiku
from senju.haiku import DEFAULT_HAIKU, Haiku
DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json")
diff --git a/tests/test_store.py b/tests/test_store.py
index 35ac981..ac8aac4 100644
--- a/tests/test_store.py
+++ b/tests/test_store.py
@@ -2,7 +2,6 @@
# pytest fixtures to work
from __future__ import annotations
-from _pytest.mark.structures import store_mark
import pytest # noqa: F401
from senju.haiku import DEFAULT_HAIKU, Haiku