From 054bf7bc9cbfa651d290f558505aed88870db6dc Mon Sep 17 00:00:00 2001 From: Luke Murphy Date: Sun, 1 Mar 2020 01:38:43 +0000 Subject: [PATCH] Add extension code --- flask_calibrestekje/flask_calibrestekje.py | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 flask_calibrestekje/flask_calibrestekje.py diff --git a/flask_calibrestekje/flask_calibrestekje.py b/flask_calibrestekje/flask_calibrestekje.py new file mode 100644 index 0000000..9a7b798 --- /dev/null +++ b/flask_calibrestekje/flask_calibrestekje.py @@ -0,0 +1,29 @@ +from calibrestekje import init_session +from flask import _app_ctx_stack, current_app + + +class CalibreStekje(object): + def __init__(self, app=None): + self.app = app + if app is not None: + self.init_app(app) + + def init_app(self, app): + app.config.setdefault("CALIBRESTEKJE_SQLITE_URL", ":memory:") + app.teardown_appcontext(self.teardown) + + def connect(self): + return init_session(current_app.config["CALIBRESTEKJE_SQLITE_URL"]) + + def teardown(self, exception): + ctx = _app_ctx_stack.top + if hasattr(ctx, "calibrestekje_session"): + ctx.calibrestekje_session.close() + + @property + def session(self): + ctx = _app_ctx_stack.top + if ctx is not None: + if not hasattr(ctx, "calibrestekje_session"): + ctx.calibrestekje_session = self.connect() + return ctx.calibrestekje_session