From f8af192fc11ecd7749ea37a4209366849b26f7a5 Mon Sep 17 00:00:00 2001 From: Stefano Pigozzi Date: Sat, 1 Jun 2019 20:59:50 +0200 Subject: [PATCH] OMFG IT WORKS --- royalnet/royalgamesweb.py | 4 ++-- royalnet/web/__init__.py | 3 ++- royalnet/web/alchemyhandler.py | 21 +++++++++++++++++++++ royalnet/web/blueprints/__init__.py | 4 ++++ royalnet/web/blueprints/helloworld.py | 17 ++--------------- royalnet/web/blueprints/testing.py | 13 +++++++++++++ royalnet/web/flaskserver.py | 5 ++--- 7 files changed, 46 insertions(+), 21 deletions(-) create mode 100644 royalnet/web/alchemyhandler.py create mode 100644 royalnet/web/blueprints/testing.py diff --git a/royalnet/royalgamesweb.py b/royalnet/royalgamesweb.py index 23aebb00..3600844f 100644 --- a/royalnet/royalgamesweb.py +++ b/royalnet/royalgamesweb.py @@ -1,6 +1,6 @@ import os from .web import create_app -from .web.blueprints import helloworld +from .web.blueprints import helloworld, testing class TestConfig: @@ -8,7 +8,7 @@ class TestConfig: REQUIRED_TABLES = set() -app = create_app(TestConfig, [helloworld.bp]) +app = create_app(TestConfig, [helloworld, testing]) if __name__ == "__main__": diff --git a/royalnet/web/__init__.py b/royalnet/web/__init__.py index d3899afd..35cb06a7 100644 --- a/royalnet/web/__init__.py +++ b/royalnet/web/__init__.py @@ -1,4 +1,5 @@ from .flaskserver import create_app from .royalprint import Royalprint +from . import blueprints -__all__ = ["create_app", "Royalprint"] +__all__ = ["create_app", "Royalprint", "blueprints"] diff --git a/royalnet/web/alchemyhandler.py b/royalnet/web/alchemyhandler.py new file mode 100644 index 00000000..6cf2aa8e --- /dev/null +++ b/royalnet/web/alchemyhandler.py @@ -0,0 +1,21 @@ +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 e69de29b..24f425d8 100644 --- a/royalnet/web/blueprints/__init__.py +++ b/royalnet/web/blueprints/__init__.py @@ -0,0 +1,4 @@ +from .helloworld import bp as helloworld +from .testing import bp as testing + +__all__ = ["helloworld", "testing"] diff --git a/royalnet/web/blueprints/helloworld.py b/royalnet/web/blueprints/helloworld.py index fa5e6607..cd8e77aa 100644 --- a/royalnet/web/blueprints/helloworld.py +++ b/royalnet/web/blueprints/helloworld.py @@ -1,24 +1,11 @@ import flask as f from .. import Royalprint -from ...database.tables import Royal -bp = Royalprint("helloworld", __name__, url_prefix="/helloworld", required_tables={Royal}) +bp = Royalprint("helloworld", __name__, url_prefix="/helloworld") @bp.route("/") def helloworld(): - royals = f.g.alchemy_session.query(f.g.alchemy.Royal).all() - return repr(royals) + return "Hello world!" - -@bp.before_request -def before_request(): - f.g.alchemy_session = f.g.alchemy.Session() - - -@bp.after_request -def after_request(): - alchemy_session = f.g.pop("alchemy_session", None) - if alchemy_session is not None: - alchemy_session.close() diff --git a/royalnet/web/blueprints/testing.py b/royalnet/web/blueprints/testing.py new file mode 100644 index 00000000..989b95af --- /dev/null +++ b/royalnet/web/blueprints/testing.py @@ -0,0 +1,13 @@ +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 listroyals(): + from ..alchemyhandler import alchemy, alchemy_session + royals = alchemy_session.query(alchemy.Royal).all() + return f'' diff --git a/royalnet/web/flaskserver.py b/royalnet/web/flaskserver.py index 2f9cbc4a..ed22c245 100644 --- a/royalnet/web/flaskserver.py +++ b/royalnet/web/flaskserver.py @@ -9,8 +9,7 @@ def create_app(config_obj: typing.Type, blueprints: typing.List[Royalprint]): app.config.from_object(config_obj) required_tables = set() for blueprint in blueprints: - required_tables.union(blueprint.required_tables) + required_tables = required_tables.union(blueprint.required_tables) app.register_blueprint(blueprint) - with app.app_context(): - f.g.alchemy = Alchemy(app.config["DB_PATH"], required_tables) + app.config["ALCHEMY"] = Alchemy(app.config["DB_PATH"], required_tables) return app