Add ability to fetch kanji from database
This commit is contained in:
parent
5e4f73d057
commit
5e8d64544b
@ -3,16 +3,19 @@ import os
|
|||||||
from flask import Flask, redirect, render_template, g, request, session, url_for
|
from flask import Flask, redirect, render_template, g, request, session, url_for
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
|
from kanken_online.api import get_kanji_by_character
|
||||||
from kanken_online.database import get_database
|
from kanken_online.database import get_database
|
||||||
from .auth import login_required
|
from .auth import login_required
|
||||||
|
|
||||||
|
|
||||||
DATABASE_NAME = "kanken_online.sqlite"
|
DATABASE_NAME = "kanken_online.sqlite"
|
||||||
|
JITEN_DB_NAME = "kanken.db"
|
||||||
def create_app(test_config=None):
|
def create_app(test_config=None):
|
||||||
app = Flask(__name__, instance_relative_config=True)
|
app = Flask(__name__, instance_relative_config=True)
|
||||||
app.config.from_mapping(
|
app.config.from_mapping(
|
||||||
SECRET_KEY="dev",
|
SECRET_KEY="dev",
|
||||||
DATABASE=str(Path(app.instance_path) / DATABASE_NAME)
|
DATABASE=str(Path(app.instance_path) / DATABASE_NAME),
|
||||||
|
JITEN_DB=str(Path(app.instance_path) / JITEN_DB_NAME)
|
||||||
)
|
)
|
||||||
|
|
||||||
if test_config is None:
|
if test_config is None:
|
||||||
@ -66,28 +69,29 @@ def create_app(test_config=None):
|
|||||||
|
|
||||||
@app.route("/kanji/<kanji>")
|
@app.route("/kanji/<kanji>")
|
||||||
def kanji_page(kanji: str):
|
def kanji_page(kanji: str):
|
||||||
# TODO use database to get kanji
|
kanji_obj = get_kanji_by_character(kanji)
|
||||||
|
|
||||||
class Kanji():
|
class Kanji():
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Highly tentative testing data
|
out = Kanji()
|
||||||
kanji = Kanji()
|
out.character = kanji_obj.character
|
||||||
kanji.character = "麻"
|
out.is_joyo = "常用" if kanji_obj.level not in ["1", "準1"] else "表外"
|
||||||
kanji.is_joyo = "joyo kanji"
|
out.level = kanji_obj.level
|
||||||
kanji.level = "pre-2"
|
out.strokes = kanji_obj.stroke_count
|
||||||
kanji.strokes = 11
|
out.radical = kanji_obj.radical
|
||||||
kanji.radical = "麻"
|
out.added_strokes = kanji_obj.radical_added_stroke_count
|
||||||
kanji.added_strokes = 0
|
out.goon = " ・ ".join(str(obj.reading) for obj in kanji_obj.goon)
|
||||||
kanji.goon = "マ"
|
out.kanon = ""
|
||||||
kanji.kanon = "バ"
|
out.toon = ""
|
||||||
kanji.toon = ""
|
out.soon = ""
|
||||||
kanji.soon = ""
|
out.kanyoon = ""
|
||||||
kanji.kanyoon = ""
|
out.kun = ""
|
||||||
kanji.kun = "あさ, しびれる"
|
out.meanings = kanji_obj.meanings
|
||||||
kanji.meanings = "①あさ。クワ科の一年草。また、あさ類の総称。「亜麻」「乱麻」 ②しびれる。しびれ。「麻酔」「麻痺(マヒ)」類痲(マ)"
|
out.glyph_origin = kanji_obj.glyph_origin
|
||||||
kanji.glyph_origin = "会意。广(げん)(いえ)と、𣏟(はい)(あさ)とから成り、屋下であさの繊維をはぎとる、ひいて「あさ」の意を表す。"
|
|
||||||
|
|
||||||
return render_template("kanji.html", kanji=kanji)
|
|
||||||
|
return render_template("kanji.html", kanji=out)
|
||||||
|
|
||||||
@app.route("/kotoba/<kotoba>")
|
@app.route("/kotoba/<kotoba>")
|
||||||
def kotoba_page(kotoba: str):
|
def kotoba_page(kotoba: str):
|
||||||
|
@ -4,7 +4,7 @@ from flask import Blueprint, jsonify
|
|||||||
import jsonpickle
|
import jsonpickle
|
||||||
from sqlalchemy import create_engine, select
|
from sqlalchemy import create_engine, select
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from .database import get_database
|
from .database import get_database, get_jiten_db
|
||||||
|
|
||||||
blueprint = Blueprint("api", __name__, url_prefix="/api")
|
blueprint = Blueprint("api", __name__, url_prefix="/api")
|
||||||
|
|
||||||
@ -125,24 +125,29 @@ class Kanji(Base):
|
|||||||
|
|
||||||
@blueprint.route("/id/<int:kanji_id>")
|
@blueprint.route("/id/<int:kanji_id>")
|
||||||
def kanji_by_id(kanji_id: int):
|
def kanji_by_id(kanji_id: int):
|
||||||
engine = create_engine("sqlite:///kanken_online/kanken.db")
|
session = get_jiten_db()
|
||||||
with Session(engine) as session:
|
query = select(Kanji).where(Kanji.id == kanji_id)
|
||||||
query = select(Kanji).where(Kanji.id == kanji_id)
|
item = session.execute(query).first()
|
||||||
item = session.execute(query).first()
|
if item is None:
|
||||||
if item is None:
|
return "Invalid ID", 404
|
||||||
return "Invalid ID", 404
|
|
||||||
|
kanji = item[0]
|
||||||
kanji = item[0]
|
return kanji.to_json()
|
||||||
return kanji.to_json()
|
|
||||||
|
def get_kanji_by_character(kanji: str) -> Kanji:
|
||||||
|
session = get_jiten_db()
|
||||||
|
query = select(Kanji).where(Kanji.character == kanji)
|
||||||
|
item = session.execute(query).first()
|
||||||
|
if item is None:
|
||||||
|
return "Invalid kanji", 404
|
||||||
|
|
||||||
|
kanji_obj: Kanji = item[0]
|
||||||
|
return kanji_obj
|
||||||
|
|
||||||
@blueprint.route("/kanji/<kanji>")
|
@blueprint.route("/kanji/<kanji>")
|
||||||
def kanji_by_character(kanji: str):
|
def kanji_by_character(kanji: str):
|
||||||
engine = create_engine("sqlite:///kanken_online/kanken.db")
|
kanji_obj = get_kanji_by_character(kanji)
|
||||||
with Session(engine) as session:
|
return kanji_obj.to_json()
|
||||||
query = select(Kanji).where(Kanji.character == kanji)
|
|
||||||
item = session.execute(query).first()
|
def get_kanji_generic():
|
||||||
if item is None:
|
pass
|
||||||
return "Invalid kanji", 404
|
|
||||||
|
|
||||||
kanji_obj = item[0]
|
|
||||||
return kanji_obj.to_json()
|
|
@ -2,6 +2,9 @@ import sqlite3
|
|||||||
from typing import IO
|
from typing import IO
|
||||||
import click
|
import click
|
||||||
from flask import Flask, current_app, g
|
from flask import Flask, current_app, g
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
|
||||||
def get_database():
|
def get_database():
|
||||||
if "db" not in g:
|
if "db" not in g:
|
||||||
@ -13,6 +16,14 @@ def get_database():
|
|||||||
|
|
||||||
return g.db
|
return g.db
|
||||||
|
|
||||||
|
def get_jiten_db():
|
||||||
|
if "jiten_db" not in g:
|
||||||
|
engine = create_engine(f"sqlite:///{current_app.config['JITEN_DB']}")
|
||||||
|
g.jiten_db = Session(engine)
|
||||||
|
print("Session is", g.jiten_db)
|
||||||
|
|
||||||
|
return g.jiten_db
|
||||||
|
|
||||||
def initialize_database():
|
def initialize_database():
|
||||||
db = get_database()
|
db = get_database()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user