diff options
Diffstat (limited to 'umosapi/app_db/uobject.py')
-rw-r--r-- | umosapi/app_db/uobject.py | 31 |
1 files changed, 13 insertions, 18 deletions
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py index 7f26935..df07450 100644 --- a/umosapi/app_db/uobject.py +++ b/umosapi/app_db/uobject.py @@ -1,4 +1,5 @@ from flask import current_app +from bson.objectid import ObjectId from .db import MongoDB @@ -12,42 +13,36 @@ class UObject(object): mongo = db.connection() return list(mongo.db.uobjects.find({})) - def register(self, name, datas): + def register(self, datas): db = MongoDB(self.app) mongo = db.connection() error = {} - if not name: - error = {"msg": "UObject name is required.", "code": 400} - elif type(datas) != dict: + if type(datas) != dict: error = { "msg": "UObject datas type is not dict (JSON).", "code": 400 } - elif len(list(mongo.db.uobjects.find({"name": name}))) > 0: - error = { - "msg": "UObject {} is already registered.".format(name), - "code": 409 - } + if not error: - mongo.db.uobjects.insert({"name": name, "datas": datas}) - return {"msg": "UObject {} added.".format(name), "code": 201} + _id = mongo.db.uobjects.insert({"datas": datas}) + return {"_id": str(_id), "msg": "UObject added.", "code": 201} return error - def remove(self, name): + def remove(self, _id): db = MongoDB(self.app) mongo = db.connection() error = {} - if not name: - error = {"msg": "UObject name is required", "code": 400} - elif len(list(mongo.db.uobjects.find({"name": name}))) == 0: + if not _id: + error = {"msg": "UObject _id is required", "code": 400} + elif len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0: error = { - 'msg': "UObject {} not exist. So it's good.".format(name), + 'msg': "UObject {} not exist. So it's good.".format(_id), "code": 404 } if not error: - mongo.db.uobjects.remove({"name": name}) - return {"msg": "UObject {} deleted".format(name), "code": 200} + mongo.db.uobjects.remove({"_id": ObjectId(_id)}) + return {"msg": "UObject {} deleted".format(_id), "code": 200} return error |