mirror of
https://github.com/senju1337/senju.git
synced 2025-12-23 23:39:27 +00:00
Merge branch 'devel' into docs/OPS-68
This commit is contained in:
commit
827f770d35
6 changed files with 53 additions and 10 deletions
|
|
@ -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"])
|
||||
|
|
|
|||
|
|
@ -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/<int:haiku_id>")
|
||||
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",
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -11,6 +11,11 @@
|
|||
{% endfor %}
|
||||
</p>
|
||||
</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') }}"
|
||||
class=" inline-block bg-violet-600 hover:bg-violet-700 text-white font-bold py-2 px-4 rounded-lg">
|
||||
Back to Home
|
||||
|
|
|
|||
|
|
@ -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() {
|
|||
});
|
||||
}
|
||||
});
|
||||
</script>
|
||||
{% endblock %}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue