inventory_view.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. import View.view_privilege as view_privilege
  2. from utility.privilege_manager import *
  3. import Model.model_manager as model_manager
  4. import json
  5. import flask
  6. import View.view_manager as view
  7. import Modules.Inventory.inventory as inventory
  8. from flask import Blueprint, jsonify
  9. inventory_blueprint = Blueprint('Inventory', __name__,url_prefix='/api/inventory')
  10. inventory_read_only_role = Privilege_Role(name="Read Only")
  11. inventory_admin_role = Privilege_Role(name="Admin")
  12. def init():
  13. inventory_privilege_domain = Privilege_Domain("inventory",description="privilege domain for inventory")
  14. view_privilege.manager.register_domain(inventory_privilege_domain)
  15. inventory_privilege_domain.add_role(inventory_read_only_role)
  16. inventory_privilege_domain.add_role(inventory_admin_role)
  17. inventory_privilege_domain.include_role(inventory_admin_role,inventory_read_only_role)
  18. declare_route()
  19. view.register_blueprint(inventory_blueprint)
  20. """
  21. # INVENTORY
  22. inventory/items - GET -> get all items
  23. inventory/item - POST create items
  24. inventory/item/<item_id> - GET get item by id
  25. inventory/item/<item_id> - UPDATE item by id
  26. inventory/item/<item_id> - DELETE item by id
  27. inventory/sites - GET -> get all site
  28. inventory/site - POST create site
  29. inventory/site/<site_id> - GET get site by id
  30. inventory/site/<site_id> - UPDATE site by id
  31. inventory/site/<site_id> - DELETE site by id
  32. inventory/contacts - GET -> get all contact
  33. inventory/contact - POST create contact
  34. inventory/contact/<contact_id> - GET get contact by id
  35. inventory/contact/<contact_id> - UPDATE contact by id
  36. inventory/contact/<contact_id> - DELETE contact by id
  37. inventory/groups - GET -> get all groups with items array id
  38. inventory/group - POST create group with items ids
  39. inventory/group/<contact_id> - GET get group by id
  40. inventory/group/<contact_id>/item/<item_id> - POST add item to group
  41. inventory/group/<contact_id>/item/<item_id> - DELETE delete item to group
  42. inventory/group/<contact_id> - DELETE group by id
  43. """
  44. def declare_route() :
  45. #############################################
  46. ######## items ########
  47. #############################################
  48. #the order of decorator are important
  49. @inventory_blueprint.route('/items',methods = ['GET'])
  50. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  51. def get_all_items() :
  52. ret = list()
  53. for item in inventory.get_all_inventory_items() :
  54. ret.append(item)
  55. return jsonify(ret)
  56. @inventory_blueprint.route('/item/<int:itemID>',methods = ['GET'])
  57. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  58. def get_item(itemID : int) :
  59. return jsonify(inventory.get_inventory_item(itemID))
  60. @inventory_blueprint.route('/item/<int:itemID>',methods = ['UPDATE','DELETE'])
  61. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  62. def update_or_delete_item(itemID : int) :
  63. if flask.request.method == 'UPDATE':
  64. return jsonify(inventory.inventory_item_update(itemID,json.dumps(flask.request.json)))
  65. if flask.request.method == 'DELETE':
  66. return jsonify(inventory.inventory_item_delete(itemID))
  67. @inventory_blueprint.route('/item',methods = ['POST'])
  68. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  69. def create_item() :
  70. return jsonify(inventory.inventory_item_create(json.dumps(flask.request.json)))
  71. @inventory_blueprint.route('/item/template',methods = ['GET'])
  72. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  73. def get_item_template() :
  74. return jsonify(inventory.inventory_item())
  75. #############################################
  76. ######## sites ########
  77. #############################################
  78. @inventory_blueprint.route('/sites',methods = ['GET'])
  79. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  80. def get_all_sites() :
  81. ret = list()
  82. for item in inventory.get_all_inventory_sites() :
  83. ret.append(item)
  84. return flask.jsonify(ret)
  85. @inventory_blueprint.route('/site/<int:siteID>',methods = ['GET'])
  86. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  87. def get_site(siteID : int) :
  88. return jsonify(inventory.get_inventory_site(siteID))
  89. @inventory_blueprint.route('/site/<int:siteID>',methods = ['UPDATE','DELETE'])
  90. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  91. def update_or_delete_site(siteID : int) :
  92. if flask.request.method == 'UPDATE':
  93. return jsonify(inventory.inventory_site_update(siteID,json.dumps(flask.request.json)))
  94. if flask.request.method == 'DELETE':
  95. return jsonify(inventory.inventory_site_delete(siteID))
  96. @inventory_blueprint.route('/site',methods = ['POST'])
  97. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  98. def create_site() :
  99. return jsonify(inventory.inventory_site_create(json.dumps(flask.request.json)))
  100. @inventory_blueprint.route('/site/template',methods = ['GET'])
  101. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  102. def get_site_template() :
  103. return jsonify(inventory.inventory_site())
  104. #############################################
  105. ######## contacts ########
  106. #############################################
  107. @inventory_blueprint.route('/contacts',methods = ['GET'])
  108. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  109. def get_all_contacts() :
  110. ret = list()
  111. for item in inventory.get_all_inventory_contacts() :
  112. ret.append(model_manager.ModelObjectToJsonString(item))
  113. return flask.jsonify(ret)
  114. @inventory_blueprint.route('/contact/<int:contactID>',methods = ['GET'])
  115. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  116. def get_contact(contactID : int) :
  117. return jsonify(inventory.get_inventory_contact(contactID))
  118. @inventory_blueprint.route('/contact/<int:contactID>',methods = ['UPDATE','DELETE'])
  119. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  120. def update_or_delete_contact(contactID : int) :
  121. if flask.request.method == 'UPDATE':
  122. return jsonify(inventory.inventory_contact_update(contactID,json.dumps(flask.request.json)))
  123. if flask.request.method == 'DELETE':
  124. return jsonify(inventory.inventory_contact_delete(contactID))
  125. @inventory_blueprint.route('/contact',methods = ['POST'])
  126. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  127. def create_contact() :
  128. return jsonify(inventory.inventory_contact_create(json.dumps(flask.request.json)))
  129. @inventory_blueprint.route('/contact/template',methods = ['GET'])
  130. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  131. def get_contact_template() :
  132. return jsonify(inventory.inventory_contact())
  133. #############################################
  134. ######## groups ########
  135. #############################################
  136. @inventory_blueprint.route('/groups',methods = ['GET'])
  137. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  138. def get_all_groups() :
  139. ret = list()
  140. for item in inventory.get_all_inventory_groups() :
  141. ret.append(model_manager.ModelObjectToJsonString(item))
  142. return flask.jsonify(ret)
  143. @inventory_blueprint.route('/group/<int:groupID>',methods = ['GET'])
  144. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  145. def get_group(groupID : int) :
  146. return jsonify(inventory.get_inventory_group(groupID))
  147. @inventory_blueprint.route('/group/<int:groupID>',methods = ['UPDATE','DELETE'])
  148. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  149. def update_or_delete_group(groupID : int) :
  150. if flask.request.method == 'UPDATE':
  151. return jsonify(inventory.inventory_group_update(groupID,json.dumps(flask.request.json)))
  152. if flask.request.method == 'DELETE':
  153. return jsonify(inventory.inventory_group_delete(groupID))
  154. @inventory_blueprint.route('/group',methods = ['POST'])
  155. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  156. def create_group() :
  157. return jsonify(inventory.inventory_group_create(json.dumps(flask.request.json)))
  158. @inventory_blueprint.route('/group/<int:groupID>/item/<int:itemID>',methods = ['POST','DELETE'])
  159. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  160. def manage_group_members(groupID : int,itemID : int) :
  161. if flask.request.method == 'POST':
  162. return jsonify(inventory.inventory_group_add_item(groupID,itemID))
  163. if flask.request.method == 'DELETE':
  164. return jsonify(inventory.inventory_group_delete_item(groupID,itemID))
  165. @inventory_blueprint.route('/group/template',methods = ['GET'])
  166. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  167. def get_group_template() :
  168. return jsonify(inventory.inventory_group())
  169. #############################################
  170. ######## aggregation ########
  171. #############################################
  172. @inventory_blueprint.route('/group/<int:groupID>/items',methods = ['GET'])
  173. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  174. def get_group_items(groupID : int) :
  175. return jsonify(inventory.get_inventory_group_items(groupID))
  176. @inventory_blueprint.route('/contact/<int:contactID>/items',methods = ['GET'])
  177. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  178. def get_contact_items(contactID : int) :
  179. return jsonify(inventory.get_inventory_contact_items(contactID))
  180. @inventory_blueprint.route('/site/<int:siteID>/items',methods = ['GET'])
  181. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  182. def get_site_items(siteID : int) :
  183. return jsonify(inventory.get_inventory_site_items(siteID))