inventory.py 8.9 KB

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