From 8e96cc0a07247a23deb1f9738f53ddb8073e2e36 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Thu, 13 Jun 2019 01:08:34 +0200 Subject: [PATCH] I somehow managed to get FLASK_DEBUG to work. Flask multithreading is weird. --- royalnet/database/alchemy.py | 2 +- royalnet/web/alchemyhandler.py | 21 ------------------- royalnet/web/blueprints/__init__.py | 4 +--- .../web/blueprints/helloworld/__init__.py | 10 --------- royalnet/web/blueprints/testing/__init__.py | 13 ------------ royalnet/web/blueprints/tglogin/__init__.py | 2 +- royalnet/web/blueprints/wikiview/__init__.py | 4 ++-- royalnet/web/flaskserver.py | 20 +++++++++++++++++- 8 files changed, 24 insertions(+), 52 deletions(-) delete mode 100644 royalnet/web/alchemyhandler.py delete mode 100644 royalnet/web/blueprints/helloworld/__init__.py delete mode 100644 royalnet/web/blueprints/testing/__init__.py diff --git a/royalnet/database/alchemy.py b/royalnet/database/alchemy.py index d2b86469..74a3a499 100644 --- a/royalnet/database/alchemy.py +++ b/royalnet/database/alchemy.py @@ -1,7 +1,7 @@ import typing from sqlalchemy import create_engine from sqlalchemy.ext.declarative import declarative_base -from sqlalchemy.orm import sessionmaker +from sqlalchemy.orm import sessionmaker, scoped_session from contextlib import contextmanager, asynccontextmanager from ..utils import asyncify # noinspection PyUnresolvedReferences diff --git a/royalnet/web/alchemyhandler.py b/royalnet/web/alchemyhandler.py deleted file mode 100644 index 6cf2aa8e..00000000 --- a/royalnet/web/alchemyhandler.py +++ /dev/null @@ -1,21 +0,0 @@ -import flask as f -from werkzeug.local import LocalProxy - - -alchemy = f.current_app.config["ALCHEMY"] - - -def get_alchemy_session(): - if "alchemy_session" not in f.g: - f.g.alchemy_session = alchemy.Session() - return f.g.alchemy_session - - -@f.current_app.teardown_appcontext -def teardown_alchemy_session(*_, **__): - _alchemy_session = f.g.pop("alchemy_session", None) - if _alchemy_session is not None: - _alchemy_session.close() - - -alchemy_session = LocalProxy(get_alchemy_session) diff --git a/royalnet/web/blueprints/__init__.py b/royalnet/web/blueprints/__init__.py index 2097b48f..dba30e4e 100644 --- a/royalnet/web/blueprints/__init__.py +++ b/royalnet/web/blueprints/__init__.py @@ -1,7 +1,5 @@ -from .helloworld import bp as helloworld -from .testing import bp as testing from .home import bp as home from .wikiview import bp as wikiview from .tglogin import bp as tglogin -__all__ = ["helloworld", "testing", "home", "wikiview", "tglogin"] +__all__ = ["home", "wikiview", "tglogin"] diff --git a/royalnet/web/blueprints/helloworld/__init__.py b/royalnet/web/blueprints/helloworld/__init__.py deleted file mode 100644 index 21228d40..00000000 --- a/royalnet/web/blueprints/helloworld/__init__.py +++ /dev/null @@ -1,10 +0,0 @@ -import flask as f -from ... import Royalprint - - -bp = Royalprint("helloworld", __name__, url_prefix="/helloworld") - - -@bp.route("/") -def helloworld_index(): - return "Hello world!" diff --git a/royalnet/web/blueprints/testing/__init__.py b/royalnet/web/blueprints/testing/__init__.py deleted file mode 100644 index 5f3fd3a5..00000000 --- a/royalnet/web/blueprints/testing/__init__.py +++ /dev/null @@ -1,13 +0,0 @@ -import flask as f -from ... import Royalprint -from ....database.tables import Royal - - -bp = Royalprint("testing", __name__, url_prefix="/testing", required_tables={Royal}) - - -@bp.route("/listroyals") -def testing_listroyals(): - from ...alchemyhandler import alchemy, alchemy_session - royals = alchemy_session.query(alchemy.Royal).all() - return f'' diff --git a/royalnet/web/blueprints/tglogin/__init__.py b/royalnet/web/blueprints/tglogin/__init__.py index 2fde4679..280fc838 100644 --- a/royalnet/web/blueprints/tglogin/__init__.py +++ b/royalnet/web/blueprints/tglogin/__init__.py @@ -18,7 +18,7 @@ def tglogin_index(): @bp.route("/done") def tglogin_done(): - from ...alchemyhandler import alchemy, alchemy_session + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] data_check_string = "" for field in sorted(list(f.request.args)): if field == "hash": diff --git a/royalnet/web/blueprints/wikiview/__init__.py b/royalnet/web/blueprints/wikiview/__init__.py index 436f308d..23790f0d 100644 --- a/royalnet/web/blueprints/wikiview/__init__.py +++ b/royalnet/web/blueprints/wikiview/__init__.py @@ -50,7 +50,7 @@ def prepare_page(page): @bp.route("/") def wikiview_index(): - from ...alchemyhandler import alchemy, alchemy_session + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] pages = sorted(alchemy_session.query(alchemy.WikiPage).all(), key=lambda page: page.title) return f.render_template("wikiview_index.html", pages=pages) @@ -58,7 +58,7 @@ def wikiview_index(): @bp.route("/", defaults={"title": ""}) @bp.route("//") def wikiview_by_id(page_id: uuid.UUID, title: str): - from ...alchemyhandler import alchemy, alchemy_session + alchemy, alchemy_session = f.current_app.config["ALCHEMY"], f.current_app.config["ALCHEMY_SESSION"] page = alchemy_session.query(alchemy.WikiPage).filter(alchemy.WikiPage.page_id == page_id).one_or_none() if page is None: return "No such page", 404 diff --git a/royalnet/web/flaskserver.py b/royalnet/web/flaskserver.py index 1e31804a..196f8954 100644 --- a/royalnet/web/flaskserver.py +++ b/royalnet/web/flaskserver.py @@ -1,6 +1,7 @@ import typing import flask as f import os +from sqlalchemy.orm import scoped_session from ..database import Alchemy from .royalprint import Royalprint @@ -9,9 +10,26 @@ def create_app(config_obj: typing.Type, blueprints: typing.List[Royalprint]): app = f.Flask(__name__) app.config.from_object(config_obj) app.secret_key = os.environ["SECRET_KEY"] + + @app.teardown_request + def teardown_alchemy_session(*_, **__): + alchemy_session = app.config["ALCHEMY_SESSION"] + if alchemy_session is not None: + alchemy_session.remove() + + # Load blueprints required_tables = set() for blueprint in blueprints: required_tables = required_tables.union(blueprint.required_tables) app.register_blueprint(blueprint) - app.config["ALCHEMY"] = Alchemy(app.config["DB_PATH"], required_tables) + + # Init Alchemy + # Seems like a dirty hack to me, but experiments are fun, right? + if len(required_tables) > 0: + alchemy = Alchemy(app.config["DB_PATH"], required_tables) + app.config["ALCHEMY"] = alchemy + app.config["ALCHEMY_SESSION"] = scoped_session(alchemy.Session) + else: + app.config["ALCHEMY"] = None + app.config["ALCHEMY_SESSION"] = None return app