senju/senju/main.py
2025-03-05 20:29:47 +01:00

62 lines
1.5 KiB
Python

from __future__ import annotations
from pathlib import Path
from flask import Flask, redirect, render_template, request, url_for
from senju.haiku import Haiku
from senju.store_manager import StoreManager
app = Flask(__name__)
store = StoreManager(Path("/tmp/store.db"))
@app.route("/")
def index_view():
return render_template("index.html", title="Senju")
@app.route("/haiku/")
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))
@app.route("/haiku/<int:haiku_id>")
def haiku_view(haiku_id):
haiku: Haiku | None = store.load_haiku(haiku_id)
if haiku is None:
# TODO: add "haiku not found" page
raise KeyError("haiku not found")
context: dict = {
"haiku": haiku
}
return render_template(
"haiku.html",
context=context,
title="Haiku of the Day")
@app.route("/prompt")
def prompt_view():
return render_template(
"prompt.html",
title="Haiku generation"
)
@app.route("/api/v1/haiku", methods=['POST'])
def generate_haiku():
if request.method == 'POST':
json_data = request.get_json()
prompt = json_data["prompt"]
haiku = Haiku.request_haiku(prompt)
id = store.save_haiku(haiku)
return str(id)
else:
return "Method not allowed", 405