Add extension code

This commit is contained in:
Luke Murphy 2020-03-01 01:38:43 +00:00
parent 27c62e722f
commit 054bf7bc9c
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
1 changed files with 29 additions and 0 deletions

View File

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