diff --git a/kanken_online/__init__.py b/kanken_online/__init__.py index 19d34be..41a614c 100644 --- a/kanken_online/__init__.py +++ b/kanken_online/__init__.py @@ -3,16 +3,19 @@ import os from flask import Flask, redirect, render_template, g, request, session, url_for from pathlib import Path +from kanken_online.api import get_kanji_by_character from kanken_online.database import get_database from .auth import login_required DATABASE_NAME = "kanken_online.sqlite" +JITEN_DB_NAME = "kanken.db" def create_app(test_config=None): app = Flask(__name__, instance_relative_config=True) app.config.from_mapping( 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: @@ -66,28 +69,29 @@ def create_app(test_config=None): @app.route("/kanji/") def kanji_page(kanji: str): - # TODO use database to get kanji + kanji_obj = get_kanji_by_character(kanji) + class Kanji(): pass - # Highly tentative testing data - kanji = Kanji() - kanji.character = "麻" - kanji.is_joyo = "joyo kanji" - kanji.level = "pre-2" - kanji.strokes = 11 - kanji.radical = "麻" - kanji.added_strokes = 0 - kanji.goon = "マ" - kanji.kanon = "バ" - kanji.toon = "" - kanji.soon = "" - kanji.kanyoon = "" - kanji.kun = "あさ, しびれる" - kanji.meanings = "①あさ。クワ科の一年草。また、あさ類の総称。「亜麻」「乱麻」 ②しびれる。しびれ。「麻酔」「麻痺(マヒ)」類痲(マ)" - kanji.glyph_origin = "会意。广(げん)(いえ)と、𣏟(はい)(あさ)とから成り、屋下であさの繊維をはぎとる、ひいて「あさ」の意を表す。" + out = Kanji() + out.character = kanji_obj.character + out.is_joyo = "常用" if kanji_obj.level not in ["1", "準1"] else "表外" + out.level = kanji_obj.level + out.strokes = kanji_obj.stroke_count + out.radical = kanji_obj.radical + out.added_strokes = kanji_obj.radical_added_stroke_count + out.goon = " ・ ".join(str(obj.reading) for obj in kanji_obj.goon) + out.kanon = "" + out.toon = "" + out.soon = "" + out.kanyoon = "" + out.kun = "" + out.meanings = kanji_obj.meanings + out.glyph_origin = kanji_obj.glyph_origin - return render_template("kanji.html", kanji=kanji) + + return render_template("kanji.html", kanji=out) @app.route("/kotoba/") def kotoba_page(kotoba: str): diff --git a/kanken_online/api.py b/kanken_online/api.py index dd21e50..8f20809 100644 --- a/kanken_online/api.py +++ b/kanken_online/api.py @@ -4,7 +4,7 @@ from flask import Blueprint, jsonify import jsonpickle from sqlalchemy import create_engine, select 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") @@ -125,24 +125,29 @@ class Kanji(Base): @blueprint.route("/id/") def kanji_by_id(kanji_id: int): - engine = create_engine("sqlite:///kanken_online/kanken.db") - with Session(engine) as session: - query = select(Kanji).where(Kanji.id == kanji_id) - item = session.execute(query).first() - if item is None: - return "Invalid ID", 404 - - kanji = item[0] - return kanji.to_json() + session = get_jiten_db() + query = select(Kanji).where(Kanji.id == kanji_id) + item = session.execute(query).first() + if item is None: + return "Invalid ID", 404 + + kanji = item[0] + 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/") def kanji_by_character(kanji: str): - engine = create_engine("sqlite:///kanken_online/kanken.db") - with Session(engine) as session: - query = select(Kanji).where(Kanji.character == kanji) - item = session.execute(query).first() - if item is None: - return "Invalid kanji", 404 - - kanji_obj = item[0] - return kanji_obj.to_json() + kanji_obj = get_kanji_by_character(kanji) + return kanji_obj.to_json() + +def get_kanji_generic(): + pass \ No newline at end of file diff --git a/kanken_online/database.py b/kanken_online/database.py index ebbfb89..0b0b44d 100644 --- a/kanken_online/database.py +++ b/kanken_online/database.py @@ -2,6 +2,9 @@ import sqlite3 from typing import IO import click from flask import Flask, current_app, g +from sqlalchemy import create_engine +from sqlalchemy.orm import Session + def get_database(): if "db" not in g: @@ -13,6 +16,14 @@ def get_database(): 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(): db = get_database()