inventory_view.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import profile
  2. from Model.isp_model import user_account
  3. import View.view_privilege as view_privilege
  4. from utility.privilege_manager import *
  5. import persistence
  6. import Model.isp_model
  7. import Model.model_manager as model_manager
  8. import json
  9. import flask
  10. import View.view_manager as view
  11. import Modules.Inventory.inventory as inventory
  12. from flask import Blueprint
  13. inventory_blueprint = Blueprint('Inventory', __name__,url_prefix='/inventory')
  14. inventory_read_only_role = Privilege_Role(name="read_only")
  15. inventory_admin_role = Privilege_Role(name="admin")
  16. def init():
  17. inventory_privilege_domain = Privilege_Domain("inventory",description="privilege domain for inventory")
  18. view_privilege.manager.register_domain(inventory_privilege_domain)
  19. inventory_privilege_domain.add_role(inventory_read_only_role)
  20. inventory_privilege_domain.add_role(inventory_admin_role)
  21. inventory_privilege_domain.include_role(inventory_admin_role,inventory_read_only_role)
  22. declare_route()
  23. view.register_blueprint(inventory_blueprint)
  24. """
  25. # INVENTORY
  26. inventory/items - GET -> get all items
  27. inventory/item - POST create items
  28. inventory/item/<item_id> - GET get item by id
  29. inventory/item/<item_id> - UPDATE item by id
  30. inventory/item/<item_id> - DELETE item by id
  31. inventory/sites - GET -> get all site
  32. inventory/site - POST create site
  33. inventory/site/<site_id> - GET get site by id
  34. inventory/site/<site_id> - UPDATE site by id
  35. inventory/site/<site_id> - DELETE site by id
  36. inventory/contacts - GET -> get all contact
  37. inventory/contact - POST create contact
  38. inventory/contact/<contact_id> - GET get contact by id
  39. inventory/contact/<contact_id> - UPDATE contact by id
  40. inventory/contact/<contact_id> - DELETE contact by id
  41. inventory/groups - GET -> get all groups with items array id
  42. inventory/group - POST create group with items ids
  43. inventory/group/<contact_id> - GET get group by id
  44. inventory/group/<contact_id>/item/<item_id> - POST add item to group
  45. inventory/group/<contact_id>/item/<item_id> - DELETE delete item to group
  46. inventory/group/<contact_id> - DELETE group by id
  47. """
  48. def declare_route() :
  49. #the order of decorator are important
  50. @inventory_blueprint.route('/items',methods = ['GET'])
  51. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  52. def get_all_items() :
  53. ret = list()
  54. for item in inventory.get_all_inventory_items() :
  55. ret.append(model_manager.ModelObjectToJsonString(item))
  56. return flask.jsonify(ret)
  57. @inventory_blueprint.route('/sites',methods = ['GET'])
  58. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  59. def get_all_sites() :
  60. ret = list()
  61. for item in inventory.get_all_inventory_sites() :
  62. ret.append(model_manager.ModelObjectToJsonString(item))
  63. return flask.jsonify(ret)
  64. @inventory_blueprint.route('/contacts',methods = ['GET'])
  65. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  66. def get_all_contacts() :
  67. ret = list()
  68. for item in inventory.get_all_inventory_contacts() :
  69. ret.append(model_manager.ModelObjectToJsonString(item))
  70. return flask.jsonify(ret)
  71. @inventory_blueprint.route('/groups',methods = ['GET'])
  72. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  73. def get_all_groups() :
  74. ret = list()
  75. for item in inventory.get_all_inventory_groups() :
  76. ret.append(model_manager.ModelObjectToJsonString(item))
  77. return flask.jsonify(ret)
  78. @inventory_blueprint.route('/item/<int:itemID>',methods = ['GET'])
  79. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  80. def get_item(itemID : int) :
  81. return model_manager.ModelObjectToJsonString(inventory.get_inventory_item(itemID))
  82. @inventory_blueprint.route('/site/<int:siteID>',methods = ['GET'])
  83. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  84. def get_site(siteID : int) :
  85. return model_manager.ModelObjectToJsonString(inventory.get_inventory_site(siteID))
  86. @inventory_blueprint.route('/contact/<int:contactID>',methods = ['GET'])
  87. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  88. def get_contact(contactID : int) :
  89. return model_manager.ModelObjectToJsonString(inventory.get_inventory_contact(contactID))
  90. @inventory_blueprint.route('/group/<int:groupID>',methods = ['GET'])
  91. @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
  92. def get_group(groupID : int) :
  93. return model_manager.ModelObjectToJsonString(inventory.get_inventory_group(groupID))