diff --git a/senju/haiku.py b/senju/haiku.py index 950da8b..0ff3b24 100644 --- a/senju/haiku.py +++ b/senju/haiku.py @@ -130,3 +130,7 @@ class Haiku: except json.JSONDecodeError: continue return haiku + + +DEFAULT_HAIKU: Haiku = Haiku(["Purple petals rise", "Defying fragile beauty", + "Fiercely breathing life"]) diff --git a/senju/main.py b/senju/main.py index 7f46035..e30df77 100644 --- a/senju/main.py +++ b/senju/main.py @@ -74,9 +74,8 @@ 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): @@ -97,8 +96,11 @@ def haiku_view(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( "haiku.html", diff --git a/senju/store_manager.py b/senju/store_manager.py index a7aa3c9..7e53fb0 100644 --- a/senju/store_manager.py +++ b/senju/store_manager.py @@ -51,11 +51,20 @@ from typing import Optional from tinydb import TinyDB from tinydb.queries import QueryImpl -from senju.haiku import Haiku +from senju.haiku import DEFAULT_HAIKU, Haiku DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json") +class BadStoreManagerFileError(Exception): + def __init__(self, msg: str, * args: object) -> None: + self.msg = msg + super().__init__(*args) + + def __str__(self) -> str: + return f"Store file is corrupted: {self.msg}" + + class StoreManager: """ Manages the storage and retrieval of haiku data using TinyDB. @@ -81,6 +90,11 @@ class StoreManager: :return: None """ self._db = TinyDB(path_to_db) + + try: + self._db = TinyDB(path_to_db) + except Exception as e: + raise BadStoreManagerFileError(f"{e}") self.logger = Logger(__name__) def _query(self, query: QueryImpl) -> list[dict]: @@ -132,9 +146,13 @@ class StoreManager: :return: A Haiku object if found, None otherwise. :rtype: 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 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 diff --git a/senju/templates/prompt.html b/senju/templates/prompt.html index 3ce5170..cf1aa73 100644 --- a/senju/templates/prompt.html +++ b/senju/templates/prompt.html @@ -40,9 +40,6 @@ document.getElementById("submit-btn").addEventListener("click", function() { if (userInput.trim() === "") { responseText.textContent = "Please enter a prompt!"; } - else if (userInput.length > 100) { - responseText.textContent = "Input must under 100 characters long!"; - } else if (userInput.trim() === "amogus") { responseText.textContent = "🤖 AI is thinking..."; responseBox.classList.remove("opacity-0"); @@ -73,4 +70,5 @@ document.getElementById("submit-btn").addEventListener("click", function() { }); } }); + {% endblock %} diff --git a/tests/test_store.py b/tests/test_store.py index dc770c7..ac8aac4 100644 --- a/tests/test_store.py +++ b/tests/test_store.py @@ -4,7 +4,7 @@ from __future__ import annotations import pytest # noqa: F401 -from senju.haiku import Haiku +from senju.haiku import DEFAULT_HAIKU, Haiku from senju.store_manager import StoreManager # noqa: F401 @@ -36,9 +36,25 @@ 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): 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.load_haiku(store.get_id_of_latest_haiku()) + 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.load_haiku(store.get_id_of_latest_haiku()) + assert haiku != DEFAULT_HAIKU + assert haiku == nonsense_test_haiku