inventory.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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(inventory_ids,jsonString) :
  117. group = __create_object(jsonString,inventory_group)
  118. with persistence.get_Session_Instance() as sess :
  119. try :
  120. for id in inventory_ids :
  121. Item = sess.query(inventory_item).filter(inventory_item.id == id).first()
  122. Item.group_id = group.id
  123. sess.commit()
  124. except :
  125. sess.rollback()
  126. raise "one of input ID is not a valid ID for the group"
  127. return group
  128. def inventory_group_add_item(group_id,inventory_id) :
  129. with persistence.get_Session_Instance() as sess :
  130. group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  131. Item = sess.query(inventory_item).filter(inventory_item.id == inventory_id).first()
  132. Item.group_id = group.id
  133. sess.commit()
  134. def inventory_group_delete_item(group_id,inventory_id) :
  135. with persistence.get_Session_Instance() as sess :
  136. group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  137. Item = sess.query(inventory_item).filter(inventory_item.id == inventory_id).first()
  138. if group_id == Item.group_id :
  139. Item.group_id = None
  140. #TODO delete group if orphan ? maybe configure in model
  141. sess.commit()
  142. def inventory_group_delete(group_id) :
  143. with persistence.get_Session_Instance() as sess :
  144. Items = sess.query(inventory_item).filter(inventory_item.group_id == group_id)
  145. for item in Items :
  146. item.group_id = None
  147. sess.commit()
  148. group = sess.query(inventory_group).filter(inventory_group.id == group_id).first()
  149. sess.delete(group)
  150. sess.commit()
  151. return group_id
  152. def get_all_inventory_groups() :
  153. with persistence.get_Session_Instance() as sess :
  154. groups = sess.query(inventory_group).order_by(inventory_group.id)
  155. return groups
  156. def get_inventory_group(group_id) :
  157. return __getItem(group_id,inventory_group)