diff options
-rw-r--r-- | umosapi/api.py | 11 | ||||
-rw-r--r-- | umosapi/app_db/uobject.py | 19 |
2 files changed, 30 insertions, 0 deletions
diff --git a/umosapi/api.py b/umosapi/api.py index f9e90f8..57ae00a 100644 --- a/umosapi/api.py +++ b/umosapi/api.py @@ -67,6 +67,17 @@ class Register(Resource): return {"msg": status['msg']}, status['code'] +@api.route("/objects/<_id>") +class Update(Resource): + @api.expect(user_model) + def patch(self, _id): + """ Edit an uobject. """ + args = request.get_json(force=True) + status = uobject.update(_id, args.get('datas')) + + return {"msg": status['msg']}, status['code'] + + @api.route('/objects/<_id>') @api.doc(params={'_id': '5d244cc13f3d46cb739912ae'}) class Remove(Resource): diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py index df07450..ebb7a9e 100644 --- a/umosapi/app_db/uobject.py +++ b/umosapi/app_db/uobject.py @@ -46,3 +46,22 @@ class UObject(object): mongo.db.uobjects.remove({"_id": ObjectId(_id)}) return {"msg": "UObject {} deleted".format(_id), "code": 200} return error + + def update(self, _id, datas): + db = MongoDB(self.app) + mongo = db.connection() + error = {} + + 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 {} does not exist.".format(_id), + "code": 404 + } + + datas = dict(("datas.{}".format(key), value) for (key, value) in datas.items()) + if not error: + mongo.db.uobjects.update_one({"_id": ObjectId(_id)}, {"$set": datas}) + return {"msg": "UObject updated.", "code": 200} + return error |