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/uobject.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/uobject.py')
-rw-r--r-- | umosapi/app_db/uobject.py | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py new file mode 100644 index 0000000..175ac52 --- /dev/null +++ b/umosapi/app_db/uobject.py @@ -0,0 +1,48 @@ +from flask import current_app + +from .db import MongoDB + + +class UObject(object): + def __init__(self, app=None): + self.app = app + + def all(self): + db = MongoDB(self.app) + mongo = db.connection() + return list(mongo.db.objects.find({})) + + def register(self, name): + db = MongoDB(self.app) + mongo = db.connection() + error = {} + + if not name: + error = {'msg': 'Object name is required.', 'code': 400} + elif len(list(mongo.db.objects.find({"name": name}))) > 0: + error = { + 'msg': 'Object {} is already registered.'.format(name), + 'code': 409 + } + if not error: + mongo.db.objects.insert({"name": name}) + return {'msg': 'Object {} added.'.format(name), 'code': 201} + return error + + def remove(self, name): + db = MongoDB(self.app) + mongo = db.connection() + error = {} + + if not name: + error = {'msg': 'Object name is required', 'code': 400} + elif len(list(mongo.db.objects.find({"name": name}))) == 0: + error = { + 'msg': "User {} not exist. So it's good".format(name), + 'code': 404 + } + + if not error: + mongo.db.objects.remove({'name': name}) + return {'msg': 'Object {} deleted'.format(name), 'code': 200} + return error |