mirror of
https://github.com/senju1337/senju.git
synced 2025-12-24 07:39:29 +00:00
Merge pull request #45 from senju1337/feat/OPS-62
Feat/ops 62 Add initial Haiku of the day
This commit is contained in:
commit
48c6062516
3 changed files with 52 additions and 2 deletions
|
|
@ -33,6 +33,7 @@ Dependencies
|
||||||
* Flask: Core web application framework
|
* Flask: Core web application framework
|
||||||
* Haiku: Custom class for poem representation and generation
|
* Haiku: Custom class for poem representation and generation
|
||||||
* StoreManager: Database abstraction for persistence operations
|
* StoreManager: Database abstraction for persistence operations
|
||||||
|
* datetime: Datetime helper to facilitate Haiku of the day
|
||||||
|
|
||||||
Implementation
|
Implementation
|
||||||
--------------
|
--------------
|
||||||
|
|
@ -44,6 +45,8 @@ for the complete web interface are defined within this module.
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import random
|
||||||
|
from datetime import date
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from flask import (Flask, redirect, render_template, request,
|
from flask import (Flask, redirect, render_template, request,
|
||||||
|
|
@ -56,6 +59,9 @@ app = Flask(__name__)
|
||||||
|
|
||||||
store = StoreManager(Path("/tmp/store.db"))
|
store = StoreManager(Path("/tmp/store.db"))
|
||||||
|
|
||||||
|
stored_date = date.today()
|
||||||
|
random_number = 1
|
||||||
|
|
||||||
|
|
||||||
@app.route("/")
|
@app.route("/")
|
||||||
def index_view():
|
def index_view():
|
||||||
|
|
@ -65,7 +71,22 @@ def index_view():
|
||||||
:return: The index.html template with title "Senju".
|
:return: The index.html template with title "Senju".
|
||||||
:rtype: flask.Response
|
:rtype: flask.Response
|
||||||
"""
|
"""
|
||||||
return render_template("index.html", title="Senju")
|
global stored_date
|
||||||
|
global random_number
|
||||||
|
|
||||||
|
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")
|
||||||
|
context: dict = {
|
||||||
|
"haiku": haiku,
|
||||||
|
}
|
||||||
|
|
||||||
|
return render_template("index.html", context=context,
|
||||||
|
title="Haiku of the day")
|
||||||
|
|
||||||
|
|
||||||
@app.route("/haiku/")
|
@app.route("/haiku/")
|
||||||
|
|
|
||||||
|
|
@ -175,6 +175,15 @@ class StoreManager:
|
||||||
"""
|
"""
|
||||||
return self._save(data.__dict__)
|
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]:
|
def get_id_of_latest_haiku(self) -> Optional[int]:
|
||||||
"""
|
"""
|
||||||
Get the ID of the most recently added haiku.
|
Get the ID of the most recently added haiku.
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,24 @@
|
||||||
{% extends "base.html" %}
|
{% extends "base.html" %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1 class="text-center"></h1>
|
<div class="bg-violet-900 min-h-screen flex items-center justify-center text-white">
|
||||||
|
<div class="text-center">
|
||||||
|
<div class="bg-white text-gray-900 p-10 rounded-lg shadow-lg max-w-2xl mx-auto transform -translate-y-10">
|
||||||
|
<h1 class="text-4xl font-bold text-violet-700 mb-6">{{ title }}</h1>
|
||||||
|
<p class="text-2xl italic leading-relaxed text-left">
|
||||||
|
{% for line in context.haiku.lines %}
|
||||||
|
{{ line }}<br>
|
||||||
|
{% 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
|
||||||
|
</a>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue