inventory_view.py 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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
  9. inventory_blueprint = Blueprint('Inventory', __name__,url_prefix='/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 json.dumps(ret, cls=model_manager.ComplexEncoder)
  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 model_manager.ModelObjectToJsonString(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 model_manager.ModelObjectToJsonString(inventory.inventory_item_update(itemID,json.dumps(flask.request.json)))
  65. if flask.request.method == 'DELETE':
  66. return model_manager.ModelObjectToJsonString(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 model_manager.ModelObjectToJsonString(inventory.inventory_item_create(json.dumps(flask.request.json)))
  71. #############################################
  72. ######## sites ########
  73. #############################################
  74. @inventory_blueprint.route('/sites',methods = ['GET'])
  75. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  76. def get_all_sites() :
  77. ret = list()
  78. for item in inventory.get_all_inventory_sites() :
  79. ret.append(model_manager.ModelObjectToJsonString(item))
  80. return flask.jsonify(ret)
  81. @inventory_blueprint.route('/site/<int:siteID>',methods = ['GET'])
  82. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  83. def get_site(siteID : int) :
  84. return model_manager.ModelObjectToJsonString(inventory.get_inventory_site(siteID))
  85. @inventory_blueprint.route('/site/<int:siteID>',methods = ['UPDATE','DELETE'])
  86. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  87. def update_or_delete_site(siteID : int) :
  88. if flask.request.method == 'UPDATE':
  89. return model_manager.ModelObjectToJsonString(inventory.inventory_site_update(siteID,json.dumps(flask.request.json)))
  90. if flask.request.method == 'DELETE':
  91. return model_manager.ModelObjectToJsonString(inventory.inventory_site_delete(siteID))
  92. @inventory_blueprint.route('/site',methods = ['POST'])
  93. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  94. def create_site() :
  95. return model_manager.ModelObjectToJsonString(inventory.inventory_site_create(json.dumps(flask.request.json)))
  96. #############################################
  97. ######## contacts ########
  98. #############################################
  99. @inventory_blueprint.route('/contacts',methods = ['GET'])
  100. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  101. def get_all_contacts() :
  102. ret = list()
  103. for item in inventory.get_all_inventory_contacts() :
  104. ret.append(model_manager.ModelObjectToJsonString(item))
  105. return flask.jsonify(ret)
  106. @inventory_blueprint.route('/contact/<int:contactID>',methods = ['GET'])
  107. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  108. def get_contact(contactID : int) :
  109. return model_manager.ModelObjectToJsonString(inventory.get_inventory_contact(contactID))
  110. @inventory_blueprint.route('/contact/<int:contactID>',methods = ['UPDATE','DELETE'])
  111. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  112. def update_or_delete_contact(contactID : int) :
  113. if flask.request.method == 'UPDATE':
  114. return model_manager.ModelObjectToJsonString(inventory.inventory_contact_update(contactID,json.dumps(flask.request.json)))
  115. if flask.request.method == 'DELETE':
  116. return model_manager.ModelObjectToJsonString(inventory.inventory_contact_delete(contactID))
  117. @inventory_blueprint.route('/contact',methods = ['POST'])
  118. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  119. def create_contact() :
  120. return model_manager.ModelObjectToJsonString(inventory.inventory_contact_create(json.dumps(flask.request.json)))
  121. #############################################
  122. ######## groups ########
  123. #############################################
  124. @inventory_blueprint.route('/groups',methods = ['GET'])
  125. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  126. def get_all_groups() :
  127. ret = list()
  128. for item in inventory.get_all_inventory_groups() :
  129. ret.append(model_manager.ModelObjectToJsonString(item))
  130. return flask.jsonify(ret)
  131. @inventory_blueprint.route('/group/<int:groupID>',methods = ['GET'])
  132. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  133. def get_group(groupID : int) :
  134. return model_manager.ModelObjectToJsonString(inventory.get_inventory_group(groupID))
  135. @inventory_blueprint.route('/group/<int:groupID>',methods = ['UPDATE','DELETE'])
  136. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  137. def update_or_delete_group(groupID : int) :
  138. if flask.request.method == 'UPDATE':
  139. return model_manager.ModelObjectToJsonString(inventory.inventory_group_update(groupID,json.dumps(flask.request.json)))
  140. if flask.request.method == 'DELETE':
  141. return model_manager.ModelObjectToJsonString(inventory.inventory_group_delete(groupID))
  142. @inventory_blueprint.route('/group',methods = ['POST'])
  143. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  144. def create_group() :
  145. return model_manager.ModelObjectToJsonString(inventory.inventory_group_create(json.dumps(flask.request.json)))
  146. @inventory_blueprint.route('/group/<int:groupID>/item/<int:itemID>',methods = ['POST','DELETE'])
  147. @view_privilege.manager.require_authorization(required_role=inventory_admin_role,get_privilege_func=view.get_user_privilege)
  148. def manage_group_members(groupID : int,itemID : int) :
  149. if flask.request.method == 'POST':
  150. return model_manager.ModelObjectToJsonString(inventory.inventory_group_add_item(groupID,itemID))
  151. if flask.request.method == 'DELETE':
  152. return model_manager.ModelObjectToJsonString(inventory.inventory_group_delete_item(groupID,itemID))