product.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. def save_copy(self) -> object:
  35. """
  36. This return clone of the product, which is not connect with
  37. database.
  38. Returns:
  39. (product): Clone of the product, which is not database connected
  40. """
  41. result = product()
  42. result.name = self.name
  43. result.description = self.description
  44. result.author = self.author
  45. result.stock_count = self.stock_count
  46. result.barcode = self.barcode
  47. return result
  48. def restore_copy(self, target: object) -> object:
  49. """
  50. This restore content of the product, from the save copy.
  51. Parameters:
  52. target (product): Target product to restore
  53. Returns:
  54. (product): Return product itself
  55. """
  56. self.name = target.name
  57. self.description = target.description
  58. self.author = target.author
  59. self.stock_count = target.stock_count
  60. self.barcode = target.barcode
  61. return self
  62. @property
  63. def in_database(self) -> bool:
  64. """ This return True when product exists in database. """
  65. return self.id is not None
  66. @property
  67. def avairable(self) -> bool:
  68. """ This check that item is avairable on stock. """
  69. return self.stock_count > 0
  70. def __str__(self) -> str:
  71. """
  72. This dump product to string, which is helpfull when debug.
  73. Returns:
  74. (str): Product as string dump
  75. """
  76. content = str(self.name) + " "
  77. if self.in_database:
  78. content = content + "#" + str(self.id)
  79. content = content + "\n"
  80. content = content + "Description: " + str(self.description) + "\n"
  81. content = content + "Author: " + str(self.author) + "\n"
  82. content = content + "Barcode (EAN): " + str(self.barcode) + "\n"
  83. content = content + "In stock: " + str(self.stock_count) + "\n"
  84. return content
  85. @property
  86. def ready(self) -> bool:
  87. """ Check that product is ready to insert into database. """
  88. if self.name is None or self.description is None:
  89. return False
  90. if self.barcode is None or self.author is None:
  91. return False
  92. return True
  93. class product_factory:
  94. """
  95. This is builder of the product. It helps to handle validation of the
  96. product params, and modify exists product with validators.
  97. """
  98. def __init__(self, target: product | None = None) -> None:
  99. """
  100. This create new builder. When get already created item, it work on
  101. given once. When receive None, create new product to work on.
  102. Parameters:
  103. target (product | None): Item to work on (default: None)
  104. """
  105. if target is not None:
  106. self.__target = target
  107. return
  108. self.__target = product()
  109. @property
  110. def ready(self) -> bool:
  111. """ It return True when target is ready to insert into database. """
  112. return self.__target.ready
  113. @property
  114. def result(self) -> product:
  115. """ It return ready product, or raise Exception if not Ready yet. """
  116. if not self.__target.ready:
  117. raise Exception("Product in builder is not ready yet.")
  118. return self.__target
  119. @property
  120. def name(self) -> str | None:
  121. """ It return name of the product. """
  122. return self.__target.name
  123. @name.setter
  124. def name(self, target: str) -> None:
  125. """ It set name of the product, and validating it. """
  126. if name_validator(target).invalid:
  127. raise validator_exception("product.name")
  128. self.__target.name = target
  129. @property
  130. def description(self) -> str | None:
  131. """ This return product description. """
  132. return self.__target.description
  133. def add_to_stock(self) -> None:
  134. """
  135. This add one item to stock.
  136. """
  137. self.__target.stock_count += 1
  138. def get_from_stock(self) -> bool:
  139. """
  140. This get single item from stock.
  141. Returns:
  142. (bool): True when get success, false when not avairable
  143. """
  144. if not self.__target.avairable:
  145. return False
  146. self.__target.stock_count -= 1
  147. return True
  148. @description.setter
  149. def description(self, target: str) -> None:
  150. """ This set description of the product, and validate it."""
  151. if description_validator(target).invalid:
  152. raise validator_exception("product.description")
  153. self.__target.description = target
  154. @property
  155. def barcode(self) -> str | None:
  156. """ It return barcode of building product. """
  157. return self.__target.barcode
  158. @barcode.setter
  159. def barcode(self, target: str) -> None:
  160. """ This set new barcode, and validate it. """
  161. if barcode_validator(target).invalid:
  162. raise validator_exception("product.barcode")
  163. self.__target.barcode = target
  164. @property
  165. def author(self) -> str | None:
  166. """ This return author of the product. """
  167. return self.__target.author
  168. @author.setter
  169. def author(self, target: str) -> None:
  170. """ This validate author of the product, and set it. """
  171. if author_validator(target).invalid:
  172. raise validator_exception("product.author")
  173. self.__target.author = target
  174. @property
  175. def stock_count(self) -> int:
  176. """ This return how much product is in stock. """
  177. return self.__target.stock_count
  178. @stock_count.setter
  179. def stock_count(self, target: int) -> None:
  180. """ This set stock count of the product, it mut be positive number. """
  181. if target < 0:
  182. raise validator_exception("product.stock_count")
  183. self.__target.stock_count = target