aboutsummaryrefslogtreecommitdiff
path: root/umosapi/app_db/uobject.py
diff options
context:
space:
mode:
Diffstat (limited to 'umosapi/app_db/uobject.py')
-rw-r--r--umosapi/app_db/uobject.py61
1 files changed, 21 insertions, 40 deletions
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py
index 78b8bc8..cffd9ba 100644
--- a/umosapi/app_db/uobject.py
+++ b/umosapi/app_db/uobject.py
@@ -1,3 +1,4 @@
+import re
from flask import current_app
from bson.objectid import ObjectId
@@ -16,79 +17,59 @@ class UObject(object):
def register(self, datas):
db = MongoDB(self.app)
mongo = db.connection()
- error = {}
if type(datas) != dict:
error = {
- "msg": "UObject datas type is not dict (JSON).",
- "code": 400
+ "msg": "UObject datas type is not dict (JSON)."
}
+ return error, 400
- if not error:
- _id = mongo.db.uobjects.insert({"datas": datas})
- return {"_id": str(_id), "msg": "UObject added.", "code": 201}
- return error
+ _id = mongo.db.uobjects.insert({"datas": datas})
+ return {"_id": str(_id)}
def remove(self, _id):
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:
+ if not re.match('[0-9a-z]{24}', _id):
error = {
- "msg": "UObject {} not exist. So it's good.".format(_id),
- "code": 404
+ "msg": "UObject _id is must match following regex [0-9a-z]{24}"
}
+ return error, 400
+ if len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
+ return '', 404
- if not error:
- mongo.db.uobjects.remove({"_id": ObjectId(_id)})
- return {"msg": "UObject {} deleted".format(_id), "code": 200}
- return error
+ mongo.db.uobjects.remove({"_id": ObjectId(_id)})
+ return {"_id": _id}, 200
def update(self, _id, datas):
db = MongoDB(self.app)
mongo = db.connection()
- error = {}
if not _id:
- error = {"msg": "UObject _id is required", "code": 400}
+ error = {"msg": "UObject _id is required"}
+ return error, 400
elif len(list(mongo.db.uobjects.find({"_id": ObjectId(_id)}))) == 0:
error = {
- "msg": "UObject {} does not exist.".format(_id),
- "code": 404
+ "msg": "UObject {} does not exist.".format(_id)
}
+ return error, 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
+ mongo.db.uobjects.update_one(
+ {"_id": ObjectId(_id)}, {"$set": datas}
+ )
+ return {"msg": "UObject updated.", "_id": _id}
def eq(self, key, value):
db = MongoDB(self.app)
mongo = db.connection()
- datas = list(mongo.db.uobjects.find(
+ return list(mongo.db.uobjects.find(
{"datas.{}".format(key): {"$eq": value}}
))
- if datas:
- return {
- "msg": "UObjects found",
- "datas": datas,
- "code": 200
- }
- else:
- return {
- "msg": "UObjects not found",
- "datas": datas,
- "code": 404
- }
def reset(self):
db = MongoDB(self.app)