diff options
author | neodarz <neodarz@neodarz.net> | 2019-07-08 15:46:39 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-07-08 15:50:24 +0200 |
commit | bcde164af6aeccc13ee77c0b5e505399994095d3 (patch) | |
tree | bb4bc6b2150d06f1cca935a8af637f95094dad3e /umosapi/app_db/db.py | |
download | umosapi-bcde164af6aeccc13ee77c0b5e505399994095d3.tar.xz umosapi-bcde164af6aeccc13ee77c0b5e505399994095d3.zip |
Initial commit
All code are mainly copied then adjusted from
https://git.neodarz.net/pro/liberationCenter.git/
Diffstat (limited to 'umosapi/app_db/db.py')
-rw-r--r-- | umosapi/app_db/db.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/umosapi/app_db/db.py b/umosapi/app_db/db.py new file mode 100644 index 0000000..70b8f7c --- /dev/null +++ b/umosapi/app_db/db.py @@ -0,0 +1,31 @@ +from flask import current_app, _app_ctx_stack +from flask_pymongo import PyMongo + + +class MongoDB(object): + def __init__(self, app=None): + self.app = app + if app is not None: + self.init_app(app) + + def init_app(self, app): + app.teardown_appcontext(self.teardown) + + def connect(self): + return PyMongo(self.app, current_app.config['MONGO_URI']) + + def teardown(self, exception): + ctx = _app_ctx_stack.top + if hasattr(ctx, 'mongo_db'): + ctx.mongo_db.cx.close() + + def connection(self): + ctx = _app_ctx_stack.top + if ctx is not None: + if not hasattr(ctx, 'mongodb'): + ctx.mongo_db = self.connect() + return ctx.mongo_db + + def set_up(self): + mongo = PyMongo(self.app) + mongo.cx.drop_database("umosapi") |