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:
parent
6eb2e35f45
commit
f8af192fc1
7 changed files with 46 additions and 21 deletions
|
@ -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__":
|
||||
|
|
|
@ -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"]
|
||||
|
|
21
royalnet/web/alchemyhandler.py
Normal file
21
royalnet/web/alchemyhandler.py
Normal 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)
|
|
@ -0,0 +1,4 @@
|
|||
from .helloworld import bp as helloworld
|
||||
from .testing import bp as testing
|
||||
|
||||
__all__ = ["helloworld", "testing"]
|
|
@ -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()
|
||||
|
|
13
royalnet/web/blueprints/testing.py
Normal file
13
royalnet/web/blueprints/testing.py
Normal 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>'
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue