KankenOnline/kanken_online/__init__.py

109 lines
3.4 KiB
Python
Raw Normal View History

2024-10-09 22:44:40 +00:00
import os
from flask import Flask, redirect, render_template, g, request, session
2024-10-09 22:44:40 +00:00
from pathlib import Path
from .auth import login_required
DATABASE_NAME = "kanken_online.sqlite"
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)
)
if test_config is None:
app.config.from_pyfile("config.py", silent=True)
else:
app.config.from_mapping(test_config)
# Ensure instance path exists
os.makedirs(app.instance_path, exist_ok=True)
@app.route("/hello")
def hello():
return "Hello, World!"
@app.route("/")
def index():
return render_template("index.html")
2024-10-14 15:20:28 +00:00
@app.route("/about")
def about_page():
return render_template("about.html")
def update_settings(form):
session["language"] = form["language"]
session["theme"] = form["theme"]
return redirect("/options")
2024-10-14 15:20:28 +00:00
@app.route("/options", methods=["GET", "POST"])
2024-10-09 22:44:40 +00:00
@login_required
def options():
if request.method == "GET":
return render_template("options.html")
else:
return update_settings(request.form)
2024-10-14 10:07:47 +00:00
@app.route("/kanji/<kanji>")
def kanji_page(kanji: str):
# TODO use database to get 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 = "会意。广(げん)(いえ)と、𣏟(はい)(あさ)とから成り、屋下であさの繊維をはぎとる、ひいて「あさ」の意を表す。"
return render_template("kanji.html", kanji=kanji)
2024-10-14 10:28:53 +00:00
@app.route("/kotoba/<kotoba>")
def kotoba_page(kotoba: str):
return render_template("kotoba.html", kotoba=kotoba)
2024-10-09 22:44:40 +00:00
2024-10-15 15:52:22 +00:00
@app.route("/translations", methods=["GET", "POST"])
def strings_translation():
if request.method == "GET":
strings = {}
for language, language_data in lang.LANGUAGES.items():
strings[language] = {
string: translation for string, translation in language_data.items()
}
return render_template("translations.html", strings=strings)
else:
# TODO
print(request.form)
return redirect("/translations")
2024-10-09 22:44:40 +00:00
from . import database
database.initialize_app(app)
2024-10-14 15:34:31 +00:00
from . import auth, api, forum, search, indices
2024-10-09 22:44:40 +00:00
app.register_blueprint(auth.blueprint)
app.register_blueprint(api.blueprint)
2024-10-14 10:54:07 +00:00
app.register_blueprint(forum.blueprint)
2024-10-14 11:15:47 +00:00
app.register_blueprint(search.blueprint)
2024-10-14 15:34:31 +00:00
app.register_blueprint(indices.blueprint)
2024-10-09 22:44:40 +00:00
2024-10-14 10:49:16 +00:00
from . import lang
# def use_english(text_id: str):
# return lang.localize(text_id, lang.JAPANESE)
app.jinja_env.globals.update(localize=lang.localize)
2024-10-14 10:49:16 +00:00
2024-10-09 22:44:40 +00:00
return app