123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265 |
- from Modules.Inventory.inventory_model import inventory_contact, inventory_group, inventory_site
- import sqlalchemy.orm
- from sqlalchemy.ext.declarative import DeclarativeMeta
- import persistence
- import Model.model_manager as model_manager
- from Modules.Inventory.inventory_model import inventory_item
- from utility.app_logging import logger_name
- import logging
- logger = logging.getLogger(logger_name + ".INVENTORY")
- def __create_object(jsonString : str, objectType, validated_func = None) :
- with persistence.get_Session_Instance() as sess :
- Item = objectType()
- model_manager.setModelItemAttributesFromJson(Item,jsonString)
- if validated_func != None :
- if not validated_func() :
- sess.rollback()
- logger.error("validation function returned false when creating object, may not be compliant : " + str(Item.__dict__))
- raise ("validation function returned false when creating object, may not be compliant : " + str(Item.__dict__))
- sess.add(Item)
- sess.commit()
- sess.refresh(Item)
- logger.debug("object of type '" + str(type(Item)) + "' + created : " + str(Item.__dict__))
- return Item
- def __update_object(object_id : int ,jsonString : str, objectType, validated_func = None) :
- with persistence.get_Session_Instance() as sess :
- Item = objectType()
- Item = sess.query(objectType).filter(objectType.id == object_id).first()
- if not isinstance(Item,objectType):
- raise BaseException(" object not found with id")
- model_manager.setModelItemAttributesFromJson(Item,jsonString)
- Item.id = object_id #the id have to remain the same. TODO add an ignore variable to the set attribute function.
- if validated_func != None :
- if not validated_func() :
- sess.rollback()
- logger.error("validation function returned false when updating object, may not be compliant"+ str(Item.__dict__))
- raise ("validation function returned false when updating object, may not be compliant " + str(Item.__dict__))
- sess.commit()
- sess.refresh(Item)
- logger.debug("object of type '" + str(type(Item)) + "' + updated : " + str(Item.__dict__))
- return Item
- def __delete_object(object_id : int, objectType, validated_func = None) :
- with persistence.get_Session_Instance() as sess :
- Item = objectType()
- Item = sess.query(objectType).filter(objectType.id == object_id).first()
- sess.refresh(Item)
- if not isinstance(Item,objectType):
- raise BaseException(" object not found with id")
- item_dict = Item.__dict__
- item_id = Item.id
- sess.delete(Item)
- if validated_func != None :
- if not validated_func() :
- sess.rollback()
- logger.error("validation function returned false when deleting object, may not be compliant" + str(Item.__dict__))
- raise ("validation function returned false when deleting object, may not be compliant" + str(Item.__dict__))
- sess.commit()
- logger.debug("object of type '" + str(type(objectType)) + "' + deleted : " + str(item_dict))
- return item_id
- def __getItem(id : int ,objectype) :
- with persistence.get_Session_Instance() as sess :
- Item = sess.query(objectype).filter(objectype.id == id).first()
- if not isinstance(Item,objectype):
- raise Exception("object not found with this id")
- return Item
- #Items basic method
- def inventory_item_create(jsonString : str) :
- Item = __create_object(jsonString,inventory_item)
- #do some things here
- return Item
- def inventory_item_update(inventory_id : int, jsonString : str) :
- Item = __update_object(inventory_id,jsonString,inventory_item)
- return Item
- def inventory_item_delete(inventory_id : int, confirm = False):
- return __delete_object(inventory_id,inventory_item)
- def get_inventory_item(inventory_id : int) :
- return __getItem(inventory_id,inventory_item)
- def get_all_inventory_items() :
- with persistence.get_Session_Instance() as sess :
- Items = sess.query(inventory_item).order_by(inventory_item.id)
- return Items
- #todo get item per page
- #Sites basic method
- def inventory_site_create(jsonString) :
- Item = __create_object(jsonString,inventory_site)
- return Item
- def inventory_site_update(site_id,jsonString) :
- Item = __update_object(site_id,jsonString,inventory_site)
- return Item
- def inventory_site_delete(site_id, confirm = False):
- return __delete_object(site_id,inventory_site)
- def get_all_inventory_sites () :
- with persistence.get_Session_Instance() as sess :
- Sites = sess.query(inventory_site).order_by(inventory_site.id)
- return Sites
- def get_inventory_site(site_id) :
- return __getItem(site_id,inventory_site)
- #Contact basic method
- def inventory_contact_create(jsonString) :
- Item = __create_object(jsonString,inventory_contact)
- return Item
- def inventory_contact_update(contact_id,jsonString) :
- Item = __update_object(contact_id,jsonString,inventory_contact)
- return Item
- def inventory_contact_delete(contact_id, confirm = False):
- return __delete_object(contact_id,inventory_contact)
- def get_all_inventory_contacts() :
- with persistence.get_Session_Instance() as sess :
- Sites = sess.query(inventory_contact).order_by(inventory_contact.id)
- return Sites
- def get_inventory_contact(contact_id) :
- return __getItem(contact_id,inventory_contact)
- #Group basic method
- def inventory_group_create(jsonString) :
- group = __create_object(jsonString,inventory_group)
- return group
- def inventory_group_update(group_id,jsonString) :
- group = __update_object(group_id,jsonString,inventory_group)
- return group
- def inventory_group_add_item(group_id,inventory_id) :
- with persistence.get_Session_Instance() as sess :
- group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
- Item = sess.query(inventory_item).filter(inventory_item.id == inventory_id).first()
- Item.group_id = group.id
- sess.commit()
- def inventory_group_delete_item(group_id,inventory_id) :
- with persistence.get_Session_Instance() as sess :
- group : inventory_group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
- Item : inventory_item = sess.query(inventory_item).filter(inventory_item.id == inventory_id).first()
- if group_id == Item.group_id :
- Item.group_id = None
- query : sqlalchemy.orm.Query = sess.query(inventory_item).filter(inventory_item.group_id == group.id)
- if query.count() == 0 :
- sess.delete(group) # if group is orphan
- sess.commit()
- def inventory_group_delete(group_id) :
- with persistence.get_Session_Instance() as sess :
- Items = sess.query(inventory_item).filter(inventory_item.group_id == group_id)
- for item in Items :
- item.group_id = None
- sess.commit()
- group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
- sess.delete(group)
- sess.commit()
- return group_id
- def get_all_inventory_groups() :
- with persistence.get_Session_Instance() as sess :
- groups = sess.query(inventory_group).order_by(inventory_group.id)
- return groups
- def get_inventory_group(group_id) :
- return __getItem(group_id,inventory_group)
- ## AGREGATION METHODS
- ## method a bit smart to have processed data
- def get_inventory_group_items(group_id) :
- with persistence.get_Session_Instance() as sess :
- group : inventory_group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
- return sess.query(inventory_item).filter(inventory_item.group_id == group.id)
- def get_inventory_site_items(site_id) :
- with persistence.get_Session_Instance() as sess :
- site : inventory_site = sess.query(inventory_site).filter(inventory_site.id == site_id).first()
- return sess.query(inventory_item).filter(inventory_item.site_id == site.id)
- def get_inventory_contact_items(contact_id) :
- with persistence.get_Session_Instance() as sess :
- contact : inventory_contact = sess.query(inventory_contact).filter(inventory_contact.id == contact_id).first()
- return sess.query(inventory_item).filter(inventory_item.contact_id == contact.id)
- def get_inventory_items_by_tag(str : str) :
- #TODO get all tags
- raise Exception("not implemented yet")
- with persistence.get_Session_Instance() as sess :
- return sess.query(inventory_contact).filter(inventory_contact.tags.contains(str))
|