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/ - GET get item by id inventory/item/ - UPDATE item by id inventory/item/ - DELETE item by id inventory/sites - GET -> get all site inventory/site - POST create site inventory/site/ - GET get site by id inventory/site/ - UPDATE site by id inventory/site/ - DELETE site by id inventory/contacts - GET -> get all contact inventory/contact - POST create contact inventory/contact/ - GET get contact by id inventory/contact/ - UPDATE contact by id inventory/contact/ - DELETE contact by id inventory/groups - GET -> get all groups with items array id inventory/group - POST create group with items ids inventory/group/ - GET get group by id inventory/group//item/ - POST add item to group inventory/group//item/ - DELETE delete item to group inventory/group/ - 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/',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/',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/',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/',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/',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/',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/',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/',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//item/',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//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//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//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)))