Merge pull request #41 from senju1337/feat/OPS-65

load default haiku if no haikus exist yet
This commit is contained in:
Guts 2025-03-23 17:30:01 +01:00 committed by GitHub
commit b300987b1f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 12 deletions

View file

@ -59,3 +59,7 @@ class Haiku:
continue continue
return haiku return haiku
DEFAULT_HAIKU: Haiku = Haiku(["Purple petals rise", "Defying fragile beauty",
"Fiercely breathing life"])

View file

@ -24,20 +24,18 @@ def index_view():
def haiku_index_view(): def haiku_index_view():
haiku_id: int | None = store.get_id_of_latest_haiku() haiku_id: int | None = store.get_id_of_latest_haiku()
if haiku_id is None: if haiku_id is None:
# TODO: add "empty haiku list" error page haiku_id = 0
raise KeyError("no haiku exist yet") return redirect(url_for("haiku_view", haiku_id=haiku_id, is_default=1))
return redirect(url_for("haiku_view", haiku_id=haiku_id))
@app.route("/haiku/<int:haiku_id>") @app.route("/haiku/<int:haiku_id>")
def haiku_view(haiku_id): def haiku_view(haiku_id):
"""test""" """test"""
haiku: Haiku | None = store.load_haiku(haiku_id) is_default: bool = request.args.get("is_default") == "1"
if haiku is None: haiku: Haiku = store.load_haiku(haiku_id)
# TODO: add "haiku not found" page
raise KeyError("haiku not found")
context: dict = { context: dict = {
"haiku": haiku "haiku": haiku,
"is_default": is_default
} }
return render_template( return render_template(

View file

@ -7,7 +7,7 @@ from typing import Optional
from tinydb import TinyDB from tinydb import TinyDB
from tinydb.queries import QueryImpl 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") DEFAULT_DB_PATH: Path = Path("/var/lib/senju.json")
@ -34,10 +34,12 @@ class StoreManager:
def _save(self, data: dict) -> int: def _save(self, data: dict) -> int:
return self._db.insert(data) 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) raw_haiku: dict | None = self._load(key)
if raw_haiku is None: if raw_haiku is None:
return None return DEFAULT_HAIKU
h = Haiku(**raw_haiku) h = Haiku(**raw_haiku)
return h return h

View file

@ -11,6 +11,11 @@
{% endfor %} {% endfor %}
</p> </p>
</div> </div>
{% if context.is_default %}
<div class="mb-5">
<b>Note:</b> No haikus have been found in the haiku store.
</div>
{% endif %}
<a href="{{ url_for('index_view') }}" <a href="{{ url_for('index_view') }}"
class=" inline-block bg-violet-600 hover:bg-violet-700 text-white font-bold py-2 px-4 rounded-lg"> class=" inline-block bg-violet-600 hover:bg-violet-700 text-white font-bold py-2 px-4 rounded-lg">
Back to Home Back to Home

View file

@ -4,7 +4,7 @@ from __future__ import annotations
import pytest # noqa: F401 import pytest # noqa: F401
from senju.haiku import Haiku from senju.haiku import DEFAULT_HAIKU, Haiku
from senju.store_manager import StoreManager # noqa: F401 from senju.store_manager import StoreManager # noqa: F401
@ -36,9 +36,25 @@ def test_save_and_load_haiku(store_manager: StoreManager):
but should have" but should have"
assert h == h_loaded assert h == h_loaded
assert h != DEFAULT_HAIKU
def test_load_latest_with_empty_store(temp_data_dir): def test_load_latest_with_empty_store(temp_data_dir):
store = StoreManager(temp_data_dir / "empty_store.json") store = StoreManager(temp_data_dir / "empty_store.json")
h = store.get_id_of_latest_haiku() h = store.get_id_of_latest_haiku()
assert h is None 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