KankenOnline/kanken_online/search.py
2024-10-21 11:14:25 +01:00

73 lines
2.0 KiB
Python

from flask import Blueprint, render_template, request
from sqlalchemy import select
from kanken_online.api import Goon, Kanji, Kanon, Kanyoon, Kun, Soon, Toon
from kanken_online.database import get_jiten_db
blueprint = Blueprint("search", __name__, url_prefix="/search")
def search_results(args: dict):
class renderable:
def render(self):
return "ok"
results = [renderable(), renderable()] # Do something with args
jiten = get_jiten_db()
query = select(Kanji).where(
Kanji.character == args["keywords"]
).where(
Kanji.stroke_count.between((args["stroke_min"] or 0), (args["stroke_max"] or 100))
)
if args["radical"]:
query = query.where(
Kanji.radical == args["radical"]
)
if args["kanon"]:
query = query.where(
Kanji.kanon.any(Kanon.reading.in_([args["kanon"]]))
)
if args["goon"]:
query = query.where(
Kanji.goon.any(Goon.reading.in_([args["goon"]]))
)
if args["soon"]:
query = query.where(
Kanji.soon.any(Soon.reading.in_([args["soon"]]))
)
if args["toon"]:
query = query.where(
Kanji.toon.any(Toon.reading.in_([args["toon"]]))
)
if args["kanyoon"]:
query = query.where(
Kanji.kanyoon.any(Kanyoon.reading.in_([args["kanyoon"]]))
)
if args["kun"]:
query = query.where(
Kanji.kun.any(Kun.reading.in_([args["kun"]]))
)
# if args["onkun"]:
# query = query.where(
# Kanji.kun.any(Kun.reading.in_([args["kun"]]))
# )
results = [row[0] for row in jiten.execute(query).all()]
# print(list(map(Kanji.to_json, results)))
return render_template("search/search_results.html", results=results, value=args["keywords"])
@blueprint.route("/")
def search_page():
if request.args:
return search_results(request.args)
return render_template("search/search.html")