123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- import View.view_privilege as view_privilege
- from utility.privilege_manager import *
- import Model.model_manager as model_manager
- import json
- import flask
- import View.view_manager as view
- import Modules.Inventory.inventory as inventory
- from flask import Blueprint
- inventory_blueprint = Blueprint('Inventory', __name__,url_prefix='/api/inventory')
- inventory_read_only_role = Privilege_Role(name="Read Only")
- inventory_admin_role = Privilege_Role(name="Admin")
- def init():
- inventory_privilege_domain = Privilege_Domain("inventory",description="privilege domain for inventory")
- view_privilege.manager.register_domain(inventory_privilege_domain)
- inventory_privilege_domain.add_role(inventory_read_only_role)
- inventory_privilege_domain.add_role(inventory_admin_role)
- inventory_privilege_domain.include_role(inventory_admin_role,inventory_read_only_role)
- declare_route()
- view.register_blueprint(inventory_blueprint)
- """
- # INVENTORY
- inventory/items - GET -> get all items
- inventory/item - POST create items
- inventory/item/<item_id> - GET get item by id
- inventory/item/<item_id> - UPDATE item by id
- inventory/item/<item_id> - DELETE item by id
- inventory/sites - GET -> get all site
- inventory/site - POST create site
- inventory/site/<site_id> - GET get site by id
- inventory/site/<site_id> - UPDATE site by id
- inventory/site/<site_id> - DELETE site by id
- inventory/contacts - GET -> get all contact
- inventory/contact - POST create contact
- inventory/contact/<contact_id> - GET get contact by id
- inventory/contact/<contact_id> - UPDATE contact by id
- inventory/contact/<contact_id> - DELETE contact by id
- inventory/groups - GET -> get all groups with items array id
- inventory/group - POST create group with items ids
- inventory/group/<contact_id> - GET get group by id
- inventory/group/<contact_id>/item/<item_id> - POST add item to group
- inventory/group/<contact_id>/item/<item_id> - DELETE delete item to group
- inventory/group/<contact_id> - DELETE group by id
- """
- def declare_route() :
- #############################################
- ######## items ########
- #############################################
- #the order of decorator are important
- @inventory_blueprint.route('/items',methods = ['GET'])
- @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
- def get_all_items() :
- ret = list()
- for item in inventory.get_all_inventory_items() :
- ret.append(item)
- return view.JsonStringToResponse(json.dumps(ret, cls=model_manager.ComplexEncoder))
- @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)))
- @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))))
- if flask.request.method == 'DELETE':
- return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(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))))
- @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()))
- #############################################
- ######## sites ########
- #############################################
- @inventory_blueprint.route('/sites',methods = ['GET'])
- @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
- def get_all_sites() :
- ret = list()
- for item in inventory.get_all_inventory_sites() :
- ret.append(model_manager.ModelObjectToJsonString(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)))
- @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))))
- if flask.request.method == 'DELETE':
- return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(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))))
- @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()))
- #############################################
- ######## contacts ########
- #############################################
- @inventory_blueprint.route('/contacts',methods = ['GET'])
- @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
- def get_all_contacts() :
- ret = list()
- for item in inventory.get_all_inventory_contacts() :
- ret.append(model_manager.ModelObjectToJsonString(item))
- return flask.jsonify(ret)
- @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)))
- @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))))
- if flask.request.method == 'DELETE':
- return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(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))))
- @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()))
- #############################################
- ######## groups ########
- #############################################
- @inventory_blueprint.route('/groups',methods = ['GET'])
- @view_privilege.manager.require_authorization(required_role=inventory_read_only_role,get_privilege_func=view.get_user_privilege)
- def get_all_groups() :
- ret = list()
- for item in inventory.get_all_inventory_groups() :
- ret.append(model_manager.ModelObjectToJsonString(item))
- return flask.jsonify(ret)
- @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)))
- @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))))
- if flask.request.method == 'DELETE':
- return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(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))))
- @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)))
- if flask.request.method == 'DELETE':
- return view.JsonStringToResponse(model_manager.ModelObjectToJsonString(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()))
- #############################################
- ######## aggregation ########
- #############################################
- @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)))
- @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)))
- @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)))
|