Browse Source

normalize json return

ash 2 năm trước cách đây
mục cha
commit
b8d47796a6

+ 28 - 29
Backend/Sources/Modules/Inventory/inventory_view.py

@@ -9,7 +9,7 @@ import View.view_manager as view
 import Modules.Inventory.inventory as inventory
 
 
-from flask import Blueprint
+from flask import Blueprint, jsonify
 inventory_blueprint = Blueprint('Inventory', __name__,url_prefix='/api/inventory')
 
 
@@ -72,35 +72,35 @@ def declare_route() :
         for item in inventory.get_all_inventory_items() :
             ret.append(item)
 
-        return view.JsonStringToResponse(json.dumps(ret, cls=model_manager.ComplexEncoder))
+        return jsonify(ret)
 
     @inventory_blueprint.route('/item/<int:itemID>',methods =  ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_item(itemID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_item(itemID)))
+        return jsonify(inventory.get_inventory_item(itemID))
 
 
     @inventory_blueprint.route('/item/<int:itemID>',methods =  ['UPDATE','DELETE'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def update_or_delete_item(itemID : int) :
         if flask.request.method == 'UPDATE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_item_update(itemID,json.dumps(flask.request.json))))
+            return jsonify(inventory.inventory_item_update(itemID,json.dumps(flask.request.json)))
 
         if flask.request.method == 'DELETE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_item_delete(itemID)))
+            return jsonify(inventory.inventory_item_delete(itemID))
 
 
     @inventory_blueprint.route('/item',methods = ['POST'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def create_item() :
 
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_item_create(json.dumps(flask.request.json))))
+        return jsonify(inventory.inventory_item_create(json.dumps(flask.request.json)))
 
 
     @inventory_blueprint.route('/item/template',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_item_template() :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_item()))
+        return jsonify(inventory.inventory_item())
 
 
 
@@ -113,34 +113,34 @@ def declare_route() :
     def get_all_sites() :
         ret = list()
         for item in inventory.get_all_inventory_sites() :
-            ret.append(model_manager.ModelObjectToJsonString(item))
+            ret.append(item)
 
         return flask.jsonify(ret)
 
     @inventory_blueprint.route('/site/<int:siteID>',methods =  ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_site(siteID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_site(siteID)))
+        return jsonify(inventory.get_inventory_site(siteID))
 
 
     @inventory_blueprint.route('/site/<int:siteID>',methods =  ['UPDATE','DELETE'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def update_or_delete_site(siteID : int) :
         if flask.request.method == 'UPDATE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_site_update(siteID,json.dumps(flask.request.json))))
+            return jsonify(inventory.inventory_site_update(siteID,json.dumps(flask.request.json)))
 
         if flask.request.method == 'DELETE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_site_delete(siteID)))
+            return jsonify(inventory.inventory_site_delete(siteID))
 
     @inventory_blueprint.route('/site',methods = ['POST'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def create_site() :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_site_create(json.dumps(flask.request.json))))
+        return jsonify(inventory.inventory_site_create(json.dumps(flask.request.json)))
 
     @inventory_blueprint.route('/site/template',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def get_site_template() :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_site()))
+        return jsonify(inventory.inventory_site())
 
 #############################################
 ########           contacts          ########
@@ -159,29 +159,29 @@ def declare_route() :
     @inventory_blueprint.route('/contact/<int:contactID>',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_contact(contactID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_contact(contactID)))
+        return jsonify(inventory.get_inventory_contact(contactID))
 
     @inventory_blueprint.route('/contact/<int:contactID>',methods = ['UPDATE','DELETE'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def update_or_delete_contact(contactID : int) :
         if flask.request.method == 'UPDATE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_contact_update(contactID,json.dumps(flask.request.json))))
+            return jsonify(inventory.inventory_contact_update(contactID,json.dumps(flask.request.json)))
 
         if flask.request.method == 'DELETE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_contact_delete(contactID)))
+            return jsonify(inventory.inventory_contact_delete(contactID))
 
 
     @inventory_blueprint.route('/contact',methods = ['POST'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def create_contact() :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_contact_create(json.dumps(flask.request.json))))
+        return jsonify(inventory.inventory_contact_create(json.dumps(flask.request.json)))
 
 
     @inventory_blueprint.route('/contact/template',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def get_contact_template() :
 
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_contact()))
+        return jsonify(inventory.inventory_contact())
 
 #############################################
 ########           groups            ########
@@ -199,37 +199,36 @@ def declare_route() :
     @inventory_blueprint.route('/group/<int:groupID>',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_group(groupID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_group(groupID)))
+        return jsonify(inventory.get_inventory_group(groupID))
 
     @inventory_blueprint.route('/group/<int:groupID>',methods = ['UPDATE','DELETE'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def update_or_delete_group(groupID : int) :
         if flask.request.method == 'UPDATE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_group_update(groupID,json.dumps(flask.request.json))))
+            return jsonify(inventory.inventory_group_update(groupID,json.dumps(flask.request.json)))
 
         if flask.request.method == 'DELETE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_group_delete(groupID)))
+            return jsonify(inventory.inventory_group_delete(groupID))
 
     @inventory_blueprint.route('/group',methods = ['POST'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def create_group() :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_group_create(json.dumps(flask.request.json))))
+        return jsonify(inventory.inventory_group_create(json.dumps(flask.request.json)))
 
 
     @inventory_blueprint.route('/group/<int:groupID>/item/<int:itemID>',methods = ['POST','DELETE'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def manage_group_members(groupID : int,itemID : int) :
         if flask.request.method == 'POST':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_group_add_item(groupID,itemID)))
+            return jsonify(inventory.inventory_group_add_item(groupID,itemID))
         if flask.request.method == 'DELETE':
-            return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_group_delete_item(groupID,itemID)))
-
+            return jsonify(inventory.inventory_group_delete_item(groupID,itemID))
 
     @inventory_blueprint.route('/group/template',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
     def get_group_template() :
 
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.inventory_group()))
+        return jsonify(inventory.inventory_group())
 
 
 
@@ -243,16 +242,16 @@ def declare_route() :
     @inventory_blueprint.route('/group/<int:groupID>/items',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_group_items(groupID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_group_items(groupID)))
+        return jsonify(inventory.get_inventory_group_items(groupID))
 
     @inventory_blueprint.route('/contact/<int:contactID>/items',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_contact_items(contactID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_contact_items(contactID)))
+        return jsonify(inventory.get_inventory_contact_items(contactID))
 
     @inventory_blueprint.route('/site/<int:siteID>/items',methods = ['GET'])
     @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
     def get_site_items(siteID : int) :
-        return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(inventory.get_inventory_site_items(siteID)))
+        return jsonify(inventory.get_inventory_site_items(siteID))
 
 

+ 2 - 2
Backend/Sources/View/view_manager.py

@@ -27,8 +27,8 @@ __app__.secret_key = "aseqzdwxc"
 __app__.permanent_session_lifetime = timedelta(minutes=2)
 __app__.logger = logger
 __app__.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
-flask_cors.CORS(__app__)
-logging.getLogger('flask_cors').level = logging.DEBUG
+
+__app__.json_encoder = model_manager.ComplexEncoder
 
 limiter = Limiter(__app__,key_func=get_remote_address,default_limits=["100 per minute"])
 limiter.logger = logger