product.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. import sqlmodel
  2. from .exception import validator_exception
  3. from .validator import name_validator
  4. from .validator import description_validator
  5. from .validator import barcode_validator
  6. from .validator import author_validator
  7. class product(sqlmodel.SQLModel, table = True):
  8. """
  9. This class is product model in database. Product is item, which could be
  10. shared.
  11. """
  12. """ Name of the table to use in foreign keys. """
  13. __tablename__: str = "product"
  14. """ ID of the product in the database. """
  15. id: int | None = sqlmodel.Field(default = None, primary_key = True)
  16. """ Name of the product, or title of the book. """
  17. name: str | None = sqlmodel.Field(
  18. default = None,
  19. index = True,
  20. unique = True
  21. )
  22. """ Description of the product, provided by creator. """
  23. description: str | None = sqlmodel.Field(default = None)
  24. """ Author of the product. """
  25. author: str | None = sqlmodel.Field(default = None)
  26. """ Count of instances this products in stock. """
  27. stock_count: int = sqlmodel.Field(default = 0)
  28. """ This is barcode (EAN) of the product. """
  29. barcode: str | None = sqlmodel.Field(
  30. default = None,
  31. index = True,
  32. unique = True
  33. )
  34. reservations: list["reservation"] = sqlmodel.Relationship(
  35. back_populates = "target"
  36. )
  37. @property
  38. def on_stock(self) -> int:
  39. current = self.stock_count - len(self.reservations)
  40. if current < 0:
  41. return 0
  42. return current
  43. def save_copy(self) -> object:
  44. """
  45. This return clone of the product, which is not connect with
  46. database.
  47. Returns:
  48. (product): Clone of the product, which is not database connected
  49. """
  50. result = product()
  51. result.name = self.name
  52. result.description = self.description
  53. result.author = self.author
  54. result.stock_count = self.stock_count
  55. result.barcode = self.barcode
  56. return result
  57. def restore_copy(self, target: object) -> object:
  58. """
  59. This restore content of the product, from the save copy.
  60. Parameters:
  61. target (product): Target product to restore
  62. Returns:
  63. (product): Return product itself
  64. """
  65. self.name = target.name
  66. self.description = target.description
  67. self.author = target.author
  68. self.stock_count = target.stock_count
  69. self.barcode = target.barcode
  70. return self
  71. @property
  72. def in_database(self) -> bool:
  73. """ This return True when product exists in database. """
  74. return self.id is not None
  75. @property
  76. def avairable(self) -> bool:
  77. """ This check that item is avairable on stock. """
  78. return self.stock_count > 0
  79. def __str__(self) -> str:
  80. """
  81. This dump product to string, which is helpfull when debug.
  82. Returns:
  83. (str): Product as string dump
  84. """
  85. content = str(self.name) + " "
  86. if self.in_database:
  87. content = content + "#" + str(self.id)
  88. content = content + "\n"
  89. content = content + "Description: " + str(self.description) + "\n"
  90. content = content + "Author: " + str(self.author) + "\n"
  91. content = content + "Barcode (EAN): " + str(self.barcode) + "\n"
  92. content = content + "In stock: " + str(self.stock_count) + "\n"
  93. return content
  94. @property
  95. def ready(self) -> bool:
  96. """ Check that product is ready to insert into database. """
  97. if self.name is None or self.description is None:
  98. return False
  99. if self.barcode is None or self.author is None:
  100. return False
  101. return True
  102. class product_factory:
  103. """
  104. This is builder of the product. It helps to handle validation of the
  105. product params, and modify exists product with validators.
  106. """
  107. def __init__(self, target: product | None = None) -> None:
  108. """
  109. This create new builder. When get already created item, it work on
  110. given once. When receive None, create new product to work on.
  111. Parameters:
  112. target (product | None): Item to work on (default: None)
  113. """
  114. if target is not None:
  115. self.__target = target
  116. return
  117. self.__target = product()
  118. @property
  119. def ready(self) -> bool:
  120. """ It return True when target is ready to insert into database. """
  121. return self.__target.ready
  122. @property
  123. def result(self) -> product:
  124. """ It return ready product, or raise Exception if not Ready yet. """
  125. if not self.__target.ready:
  126. raise Exception("Product in builder is not ready yet.")
  127. return self.__target
  128. @property
  129. def name(self) -> str | None:
  130. """ It return name of the product. """
  131. return self.__target.name
  132. @name.setter
  133. def name(self, target: str) -> None:
  134. """ It set name of the product, and validating it. """
  135. if name_validator(target).invalid:
  136. raise validator_exception("product.name")
  137. self.__target.name = target
  138. @property
  139. def description(self) -> str | None:
  140. """ This return product description. """
  141. return self.__target.description
  142. def add_to_stock(self) -> None:
  143. """
  144. This add one item to stock.
  145. """
  146. self.__target.stock_count += 1
  147. def get_from_stock(self) -> bool:
  148. """
  149. This get single item from stock.
  150. Returns:
  151. (bool): True when get success, false when not avairable
  152. """
  153. if not self.__target.avairable:
  154. return False
  155. self.__target.stock_count -= 1
  156. return True
  157. @description.setter
  158. def description(self, target: str) -> None:
  159. """ This set description of the product, and validate it."""
  160. if description_validator(target).invalid:
  161. raise validator_exception("product.description")
  162. self.__target.description = target
  163. @property
  164. def barcode(self) -> str | None:
  165. """ It return barcode of building product. """
  166. return self.__target.barcode
  167. @barcode.setter
  168. def barcode(self, target: str) -> None:
  169. """ This set new barcode, and validate it. """
  170. if barcode_validator(target).invalid:
  171. raise validator_exception("product.barcode")
  172. self.__target.barcode = target
  173. @property
  174. def author(self) -> str | None:
  175. """ This return author of the product. """
  176. return self.__target.author
  177. @author.setter
  178. def author(self, target: str) -> None:
  179. """ This validate author of the product, and set it. """
  180. if author_validator(target).invalid:
  181. raise validator_exception("product.author")
  182. self.__target.author = target
  183. @property
  184. def stock_count(self) -> int:
  185. """ This return how much product is in stock. """
  186. return self.__target.stock_count
  187. @stock_count.setter
  188. def stock_count(self, target: int) -> None:
  189. """ This set stock count of the product, it mut be positive number. """
  190. if target < 0:
  191. raise validator_exception("product.stock_count")
  192. self.__target.stock_count = target