From 35dba4991bac69ddf1d5446ad90b00c3c755a88d Mon Sep 17 00:00:00 2001 From: Kiril Kovachev Date: Mon, 21 Oct 2024 11:14:25 +0100 Subject: [PATCH] Partially implement search --- kanken_online/api.py | 3 +++ kanken_online/search.py | 56 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/kanken_online/api.py b/kanken_online/api.py index 8f20809..be54d48 100644 --- a/kanken_online/api.py +++ b/kanken_online/api.py @@ -123,6 +123,9 @@ class Kanji(Base): return out + def render(self): + return self.character + @blueprint.route("/id/") def kanji_by_id(kanji_id: int): session = get_jiten_db() diff --git a/kanken_online/search.py b/kanken_online/search.py index 40bcf31..4b988ca 100644 --- a/kanken_online/search.py +++ b/kanken_online/search.py @@ -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("/")