From 97858b6edfaaee44077761663d2ade3b84e6d3d7 Mon Sep 17 00:00:00 2001
From: neodarz <neodarz@neodarz.net>
Date: Mon, 8 Jul 2019 16:45:04 +0200
Subject: Add datas field of uobject schema

---
 umosapi/api.py            | 13 ++++++++++++-
 umosapi/app_db/uobject.py |  9 +++++++--
 2 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/umosapi/api.py b/umosapi/api.py
index 0b48079..ef83c18 100644
--- a/umosapi/api.py
+++ b/umosapi/api.py
@@ -28,11 +28,22 @@ api = Api(
 
 uobject = UObject(app)
 
+
+class fieldsDict(fields.Raw):
+    __schema_type__ = ["Dict"]
+    __schema_example__ = {"key": "token"}
+
+
 user_model = api.model('UObject', {
     'name': fields.String(
         required=True,
         description='Name of the uobject',
         example='Player'
+    ),
+    'datas': fieldsDict(
+        required=False,
+        description='Datas of the uobject in JSON format',
+        example={"key": "token"}
     )
 })
 
@@ -53,7 +64,7 @@ class Register(Resource):
     def post(self):
         """ Register new uobject """
         args = request.get_json(force=True)
-        status = uobject.register(args.get('name'))
+        status = uobject.register(args.get('name'), args.get('datas'))
 
         return loads('{"msg": "'+status['msg']+'"}'), status['code']
 
diff --git a/umosapi/app_db/uobject.py b/umosapi/app_db/uobject.py
index 175ac52..3d29bf6 100644
--- a/umosapi/app_db/uobject.py
+++ b/umosapi/app_db/uobject.py
@@ -12,20 +12,25 @@ class UObject(object):
         mongo = db.connection()
         return list(mongo.db.objects.find({}))
 
-    def register(self, name):
+    def register(self, name, datas):
         db = MongoDB(self.app)
         mongo = db.connection()
         error = {}
 
         if not name:
             error = {'msg': 'Object name is required.', 'code': 400}
+        elif type(datas) != dict:
+            error = {
+                "msg": "Object datas type is not dict (JSON).",
+                "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})
+            mongo.db.objects.insert({"name": name, "datas": datas})
             return {'msg': 'Object {} added.'.format(name), 'code': 201}
         return error
 
-- 
cgit v1.2.1