diff options
author | neodarz <neodarz@neodarz.net> | 2018-06-16 17:52:31 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2018-06-16 17:52:31 +0200 |
commit | 5bcc1ed9df4e7a6b8086cac3f25a466aee3bebcd (patch) | |
tree | f4e72ed3712a148a28a0c11323f1796f7499a336 /src/lb_app/app_db | |
parent | cf29b5d4324b03d826615cfbb7f7345c54da1762 (diff) | |
download | liberationCenter-5bcc1ed9df4e7a6b8086cac3f25a466aee3bebcd.tar.xz liberationCenter-5bcc1ed9df4e7a6b8086cac3f25a466aee3bebcd.zip |
Add first structure of the application
Diffstat (limited to '')
-rw-r--r-- | src/lb_app/app_db/__init__.py | 0 | ||||
-rw-r--r-- | src/lb_app/app_db/db.py | 31 | ||||
-rw-r--r-- | src/lb_app/app_db/user.py | 27 |
3 files changed, 58 insertions, 0 deletions
diff --git a/src/lb_app/app_db/__init__.py b/src/lb_app/app_db/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/lb_app/app_db/__init__.py diff --git a/src/lb_app/app_db/db.py b/src/lb_app/app_db/db.py new file mode 100644 index 0000000..005599b --- /dev/null +++ b/src/lb_app/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, 'mongo_db'): + ctx.mongo_db = self.connect() + return ctx.mongo_db + + def set_up(self): + mongo = PyMongo(self.app) + mongo.cx.drop_database("test_liberationCenter") diff --git a/src/lb_app/app_db/user.py b/src/lb_app/app_db/user.py new file mode 100644 index 0000000..1af957c --- /dev/null +++ b/src/lb_app/app_db/user.py @@ -0,0 +1,27 @@ +from flask import current_app + +from .db import MongoDB + +class User(object): + def __init__(self, app=None): + self.app = app + + def all(self): + db = MongoDB(self.app) + mongo = db.connection() + return list(mongo.db.users.find({})) + + def register(self, username): + db = MongoDB(self.app) + mongo = db.connection() + error = None + + if not username: + error = 'Username is required.' + elif len(list(mongo.db.users.find({"username": username}))) > 0: + error = 'User {} is already registered.'.format(username) + + if error is None: + mongo.db.users.insert({"username": username}) + return None + return error |