1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-23 19:44:20 +00:00

OMFG IT WORKS

This commit is contained in:
Steffo 2019-06-01 20:59:50 +02:00
parent 6eb2e35f45
commit f8af192fc1
7 changed files with 46 additions and 21 deletions

View file

@ -1,6 +1,6 @@
import os import os
from .web import create_app from .web import create_app
from .web.blueprints import helloworld from .web.blueprints import helloworld, testing
class TestConfig: class TestConfig:
@ -8,7 +8,7 @@ class TestConfig:
REQUIRED_TABLES = set() REQUIRED_TABLES = set()
app = create_app(TestConfig, [helloworld.bp]) app = create_app(TestConfig, [helloworld, testing])
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -1,4 +1,5 @@
from .flaskserver import create_app from .flaskserver import create_app
from .royalprint import Royalprint from .royalprint import Royalprint
from . import blueprints
__all__ = ["create_app", "Royalprint"] __all__ = ["create_app", "Royalprint", "blueprints"]

View file

@ -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)

View file

@ -0,0 +1,4 @@
from .helloworld import bp as helloworld
from .testing import bp as testing
__all__ = ["helloworld", "testing"]

View file

@ -1,24 +1,11 @@
import flask as f import flask as f
from .. import Royalprint 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("/") @bp.route("/")
def helloworld(): def helloworld():
royals = f.g.alchemy_session.query(f.g.alchemy.Royal).all() return "Hello world!"
return repr(royals)
@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()

View file

@ -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'<body><script type="text/plain" style="display: block;">{repr(royals)}</script></body>'

View file

@ -9,8 +9,7 @@ def create_app(config_obj: typing.Type, blueprints: typing.List[Royalprint]):
app.config.from_object(config_obj) app.config.from_object(config_obj)
required_tables = set() required_tables = set()
for blueprint in blueprints: for blueprint in blueprints:
required_tables.union(blueprint.required_tables) required_tables = required_tables.union(blueprint.required_tables)
app.register_blueprint(blueprint) app.register_blueprint(blueprint)
with app.app_context(): app.config["ALCHEMY"] = Alchemy(app.config["DB_PATH"], required_tables)
f.g.alchemy = Alchemy(app.config["DB_PATH"], required_tables)
return app return app