Partially implement search

This commit is contained in:
Kiril Kovachev 2024-10-21 11:14:25 +01:00
parent f664cd1a08
commit 35dba4991b
2 changed files with 59 additions and 0 deletions

View File

@ -123,6 +123,9 @@ class Kanji(Base):
return out
def render(self):
return self.character
@blueprint.route("/id/<int:kanji_id>")
def kanji_by_id(kanji_id: int):
session = get_jiten_db()

View File

@ -1,4 +1,8 @@
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")
@ -7,6 +11,58 @@ def search_results(args: dict):
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("/")