product.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. """ ID of the product in the database. """
  13. id: int | None = sqlmodel.Field(default = None, primary_key = True)
  14. """ Name of the product, or title of the book. """
  15. name: str | None = sqlmodel.Field(default = None, index = True)
  16. """ Description of the product, provided by creator. """
  17. description: str | None = sqlmodel.Field(default = None)
  18. """ Author of the product. """
  19. author: str | None = sqlmodel.Field(default = None)
  20. """ Link to image of the product. """
  21. image: str | None = sqlmodel.Field(default = None)
  22. """ Count of instances this products in stock. """
  23. stock_count: int = sqlmodel.Field(default = 0)
  24. """ This is barcode (EAN) of the product. """
  25. barcode: str | None = sqlmodel.Field(
  26. default = None,
  27. index = True,
  28. unique = True
  29. )
  30. @property
  31. def in_database(self) -> bool:
  32. """ This return True when product exists in database. """
  33. return self.id is not None
  34. @property
  35. def avairable(self) -> bool:
  36. """ This check that item is avairable on stock. """
  37. return self.stock_count > 0
  38. def __str__(self) -> str:
  39. """
  40. This dump product to string, which is helpfull when debug.
  41. Returns:
  42. (str): Product as string dump
  43. """
  44. content = str(self.name) + " "
  45. if self.in_database:
  46. content = content + "#" + str(self.id)
  47. content = content + "\n"
  48. content = content + "Description: " + str(self.description) + "\n"
  49. content = content + "Author: " + str(self.author) + "\n"
  50. content = content + "Barcode (EAN): " + str(self.barcode) + "\n"
  51. content = content + "Image (link): " + str(self.image) + "\n"
  52. content = content + "In stock: " + str(self.stock_count) + "\n"
  53. return content
  54. @property
  55. def ready(self) -> bool:
  56. """ Check that product is ready to insert into database. """
  57. if self.name is None or self.description is None:
  58. return False
  59. if self.barcode is None or self.author is None:
  60. return False
  61. return True
  62. class product_factory:
  63. """
  64. This is builder of the product. It helps to handle validation of the
  65. product params, and modify exists product with validators.
  66. """
  67. def __init__(self, target: product | None = None) -> None:
  68. """
  69. This create new builder. When get already created item, it work on
  70. given once. When receive None, create new product to work on.
  71. Parameters:
  72. target (product | None): Item to work on (default: None)
  73. """
  74. if target is not None:
  75. self.__target = target
  76. return
  77. self.__target = product()
  78. @property
  79. def ready(self) -> bool:
  80. """ It return True when target is ready to insert into database. """
  81. return self.__target.ready
  82. @property
  83. def result(self) -> product:
  84. """ It return ready product, or raise Exception if not Ready yet. """
  85. if not self.__target.ready:
  86. raise Exception("Product in builder is not ready yet.")
  87. return self.__target
  88. @property
  89. def name(self) -> str | None:
  90. """ It return name of the product. """
  91. return self.__target.name
  92. @name.setter
  93. def name(self, target: str) -> None:
  94. """ It set name of the product, and validating it. """
  95. if name_validator(target).invalid:
  96. raise validator_exception("product.name")
  97. self.__target.name = target
  98. @property
  99. def description(self) -> str | None:
  100. """ This return product description. """
  101. return self.__target.description
  102. def add_to_stock(self) -> None:
  103. """
  104. This add one item to stock.
  105. """
  106. self.__target.stock_count += 1
  107. def get_from_stock(self) -> bool:
  108. """
  109. This get single item from stock.
  110. Returns:
  111. (bool): True when get success, false when not avairable
  112. """
  113. if not self.__target.avairable:
  114. return False
  115. self.__target.stock_count -= 1
  116. return True
  117. @description.setter
  118. def description(self, target: str) -> None:
  119. """ This set description of the product, and validate it."""
  120. if description_validator(target).invalid:
  121. raise validator_exception("product.description")
  122. self.__target.description = target
  123. @property
  124. def barcode(self) -> str | None:
  125. """ It return barcode of building product. """
  126. return self.__target.barcode
  127. @barcode.setter
  128. def barcode(self, target: str) -> None:
  129. """ This set new barcode, and validate it. """
  130. if barcode_validator(target).invalid:
  131. raise validator_exception("product.barcode")
  132. self.__target.barcode = target
  133. @property
  134. def author(self) -> str | None:
  135. """ This return author of the product. """
  136. return self.__target.author
  137. @author.setter
  138. def author(self, target: str) -> None:
  139. """ This validate author of the product, and set it. """
  140. if author_validator(target).invalid:
  141. raise validator_exception("product.author")
  142. self.__target.author = target
  143. @property
  144. def image(self) -> str | None:
  145. """ This return image link of the product. """
  146. return self.__target.image
  147. @image.setter
  148. def image(self, target: str | None) -> None:
  149. """ This set image link of the product. """
  150. self.__target.image = target
  151. @property
  152. def stock_count(self) -> int:
  153. """ This return how much product is in stock. """
  154. return self.__target.stock_count
  155. @stock_count.setter
  156. def stock_count(self, target: int) -> None:
  157. """ This set stock count of the product, it mut be positive number. """
  158. if target < 0:
  159. raise validator_exception("product.stock_count")
  160. self.__target.stock_count = target