inventory.py 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. from Modules.Inventory.inventory_model import inventory_contact, inventory_group, inventory_site
  2. import sqlalchemy.orm
  3. from sqlalchemy.ext.declarative import DeclarativeMeta
  4. import persistence
  5. import Model.model_manager as model_manager
  6. from Modules.Inventory.inventory_model import inventory_item
  7. from utility.app_logging import logger_name
  8. import logging
  9. logger = logging.getLogger(logger_name + ".INVENTORY")
  10. def __create_object(jsonString : str, objectType, validated_func = None) :
  11. with persistence.get_Session_Instance() as sess :
  12. Item = objectType()
  13. model_manager.setModelItemAttributesFromJson(Item,jsonString)
  14. if validated_func != None :
  15. if not validated_func() :
  16. sess.rollback()
  17. logger.error("validation function returned false when creating object, may not be compliant : " + str(Item.__dict__))
  18. raise ("validation function returned false when creating object, may not be compliant : " + str(Item.__dict__))
  19. sess.add(Item)
  20. sess.commit()
  21. sess.refresh(Item)
  22. logger.debug("object of type '" + str(type(Item)) + "' + created : " + str(Item.__dict__))
  23. return Item
  24. def __update_object(object_id : int ,jsonString : str, objectType, validated_func = None) :
  25. with persistence.get_Session_Instance() as sess :
  26. Item = objectType()
  27. Item = sess.query(objectType).filter(objectType.id == object_id).first()
  28. if not isinstance(Item,objectType):
  29. raise BaseException(" object not found with id")
  30. model_manager.setModelItemAttributesFromJson(Item,jsonString)
  31. Item.id = object_id #the id have to remain the same. TODO add an ignore variable to the set attribute function.
  32. if validated_func != None :
  33. if not validated_func() :
  34. sess.rollback()
  35. logger.error("validation function returned false when updating object, may not be compliant"+ str(Item.__dict__))
  36. raise ("validation function returned false when updating object, may not be compliant " + str(Item.__dict__))
  37. sess.commit()
  38. sess.refresh(Item)
  39. logger.debug("object of type '" + str(type(Item)) + "' + updated : " + str(Item.__dict__))
  40. return Item
  41. def __delete_object(object_id : int, objectType, validated_func = None) :
  42. with persistence.get_Session_Instance() as sess :
  43. Item = objectType()
  44. Item = sess.query(objectType).filter(objectType.id == object_id).first()
  45. sess.refresh(Item)
  46. if not isinstance(Item,objectType):
  47. raise BaseException(" object not found with id")
  48. item_dict = Item.__dict__
  49. item_id = Item.id
  50. sess.delete(Item)
  51. if validated_func != None :
  52. if not validated_func() :
  53. sess.rollback()
  54. logger.error("validation function returned false when deleting object, may not be compliant" + str(Item.__dict__))
  55. raise ("validation function returned false when deleting object, may not be compliant" + str(Item.__dict__))
  56. sess.commit()
  57. logger.debug("object of type '" + str(type(objectType)) + "' + deleted : " + str(item_dict))
  58. return item_id
  59. def __getItem(id : int ,objectype) :
  60. with persistence.get_Session_Instance() as sess :
  61. Item = sess.query(objectype).filter(objectype.id == id).first()
  62. if not isinstance(Item,objectype):
  63. raise Exception("object not found with this id")
  64. return Item
  65. #Items basic method
  66. def inventory_item_create(jsonString : str) :
  67. Item = __create_object(jsonString,inventory_item)
  68. #do some things here
  69. return Item
  70. def inventory_item_update(inventory_id : int, jsonString : str) :
  71. Item = __update_object(inventory_id,jsonString,inventory_item)
  72. return Item
  73. def inventory_item_delete(inventory_id : int, confirm = False):
  74. return __delete_object(inventory_id,inventory_item)
  75. def get_inventory_item(inventory_id : int) :
  76. return __getItem(inventory_id,inventory_item)
  77. def get_all_inventory_items() :
  78. with persistence.get_Session_Instance() as sess :
  79. Items = sess.query(inventory_item).order_by(inventory_item.id)
  80. return Items
  81. #todo get item per page
  82. #Sites basic method
  83. def inventory_site_create(jsonString) :
  84. Item = __create_object(jsonString,inventory_site)
  85. return Item
  86. def inventory_site_update(site_id,jsonString) :
  87. Item = __update_object(site_id,jsonString,inventory_site)
  88. return Item
  89. def inventory_site_delete(site_id, confirm = False):
  90. return __delete_object(site_id,inventory_site)
  91. def get_all_inventory_sites () :
  92. with persistence.get_Session_Instance() as sess :
  93. Sites = sess.query(inventory_site).order_by(inventory_site.id)
  94. return Sites
  95. def get_inventory_site(site_id) :
  96. return __getItem(site_id,inventory_site)
  97. #Contact basic method
  98. def inventory_contact_create(jsonString) :
  99. Item = __create_object(jsonString,inventory_contact)
  100. return Item
  101. def inventory_contact_update(contact_id,jsonString) :
  102. Item = __update_object(contact_id,jsonString,inventory_contact)
  103. return Item
  104. def inventory_contact_delete(contact_id, confirm = False):
  105. return __delete_object(contact_id,inventory_contact)
  106. def get_all_inventory_contacts() :
  107. with persistence.get_Session_Instance() as sess :
  108. Sites = sess.query(inventory_contact).order_by(inventory_contact.id)
  109. return Sites
  110. def get_inventory_contact(contact_id) :
  111. return __getItem(contact_id,inventory_contact)
  112. #Group basic method
  113. def inventory_group_create(jsonString) :
  114. group = __create_object(jsonString,inventory_group)
  115. return group
  116. def inventory_group_update(group_id,jsonString) :
  117. group = __update_object(group_id,jsonString,inventory_group)
  118. return group
  119. def inventory_group_add_item(group_id,inventory_id) :
  120. with persistence.get_Session_Instance() as sess :
  121. group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  122. Item = sess.query(inventory_item).filter(inventory_item.id == inventory_id).first()
  123. Item.group_id = group.id
  124. sess.commit()
  125. def inventory_group_delete_item(group_id,inventory_id) :
  126. with persistence.get_Session_Instance() as sess :
  127. group : inventory_group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  128. Item : inventory_item = sess.query(inventory_item).filter(inventory_item.id == inventory_id).first()
  129. if group_id == Item.group_id :
  130. Item.group_id = None
  131. query : sqlalchemy.orm.Query = sess.query(inventory_item).filter(inventory_item.group_id == group.id)
  132. if query.count() == 0 :
  133. sess.delete(group) # if group is orphan
  134. sess.commit()
  135. def inventory_group_delete(group_id) :
  136. with persistence.get_Session_Instance() as sess :
  137. Items = sess.query(inventory_item).filter(inventory_item.group_id == group_id)
  138. for item in Items :
  139. item.group_id = None
  140. sess.commit()
  141. group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  142. sess.delete(group)
  143. sess.commit()
  144. return group_id
  145. def get_all_inventory_groups() :
  146. with persistence.get_Session_Instance() as sess :
  147. groups = sess.query(inventory_group).order_by(inventory_group.id)
  148. return groups
  149. def get_inventory_group(group_id) :
  150. return __getItem(group_id,inventory_group)
  151. ## AGREGATION METHODS
  152. ## method a bit smart to have processed data
  153. def get_inventory_group_items(group_id) :
  154. with persistence.get_Session_Instance() as sess :
  155. group : inventory_group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  156. return sess.query(inventory_item).filter(inventory_item.group_id == group.id)
  157. def get_inventory_site_items(site_id) :
  158. with persistence.get_Session_Instance() as sess :
  159. site : inventory_site = sess.query(inventory_site).filter(inventory_site.id == site_id).first()
  160. return sess.query(inventory_item).filter(inventory_item.site_id == site.id)
  161. def get_inventory_contact_items(contact_id) :
  162. with persistence.get_Session_Instance() as sess :
  163. contact : inventory_contact = sess.query(inventory_contact).filter(inventory_contact.id == contact_id).first()
  164. return sess.query(inventory_item).filter(inventory_item.contact_id == contact.id)
  165. def get_inventory_items_by_tag(str : str) :
  166. #TODO get all tags
  167. raise Exception("not implemented yet")
  168. with persistence.get_Session_Instance() as sess :
  169. return sess.query(inventory_contact).filter(inventory_contact.tags.contains(str))