product.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import sqlmodel
  2. from .exception import validator_exception
  3. from .validator import name_validator
  4. from .validator import description_validator
  5. class product(sqlmodel.SQLModel, table = True):
  6. """
  7. This class is product model in database. Product is item, which could be
  8. shared.
  9. """
  10. """ ID of the product in the database. """
  11. id: int | None = sqlmodel.Field(default = None, primary_key = True)
  12. """ Name of the product, or title of the book. """
  13. name: str | None = sqlmodel.Field(default = None, index = True)
  14. """ Description of the product, provided by creator. """
  15. description: str | None = sqlmodel.Field(default = None)
  16. """ Author of the product. """
  17. author: str | None = sqlmodel.Field(default = None)
  18. """ Link to image of the product. """
  19. image: str | None = sqlmodel.Field(default = None)
  20. """ Count of instances this products in stock. """
  21. stock_count: int = sqlmodel.Field(default = 0)
  22. """ This is barcode (EAN) of the product. """
  23. badcode: str | None = sqlmodel.Field(
  24. default = None,
  25. index = True,
  26. unique = True
  27. )
  28. @property
  29. def in_database(self) -> bool:
  30. """ This return True when product exists in database. """
  31. return self.id is not None
  32. def __str__(self) -> str:
  33. """
  34. This dump product to string, which is helpfull when debug.
  35. Returns:
  36. (str): Product as string dump
  37. """
  38. content = str(self.name) + " " + str(self.id or "") + "\n"
  39. content = content + "Description: " + str(self.description) + "\n"
  40. content = content + "Author: " + str(self.author) + "\n"
  41. content = content + "Barcode (EAN): " + str(self.barcode) + "\n"
  42. content = content + "Image (link): " + str(self.image) + "\n"
  43. content = content + "In stock: " + str(self.stock_count) + "\n"
  44. return content
  45. @property
  46. def ready(self) -> bool:
  47. """ Check that product is ready to insert into database. """
  48. if name is None or description is None:
  49. return False
  50. if barcode is None or author is None:
  51. return False
  52. return True
  53. class product_builder:
  54. """
  55. This is builder of the product. It helps to handle validation of the
  56. product params, and modify exists product with validators.
  57. """
  58. def __init__(self, target: product | None = None) -> None:
  59. """
  60. This create new builder. When get already created item, it work on
  61. given once. When receive None, create new product to work on.
  62. Parameters:
  63. target (product | None): Item to work on (default: None)
  64. """
  65. if target is not None:
  66. self.__target = target
  67. return
  68. self.__target = product()
  69. @property
  70. def ready(self) -> bool:
  71. """ It return True when target is ready to insert into database. """
  72. return self.__target.ready
  73. @property
  74. def result(self) -> product:
  75. """ It return ready product, or raise Exception if not Ready yet. """
  76. if not self.__target.ready:
  77. raise Exception("Product in builder is not ready yet.")
  78. return self.__target
  79. @property
  80. def name(self) -> str | None:
  81. """ It return name of the product. """
  82. return self.__target.name
  83. @name.setter
  84. def name(self, target: str) -> None:
  85. """ It set name of the product, and validating it. """
  86. if name_validator(target).invalid:
  87. raise validator_exception("product.name")
  88. self.__target.name = target
  89. @property
  90. def description(self) -> str | None:
  91. """ This return product description. """
  92. return self.__target.description
  93. @description.setter
  94. def description(self, target: str) -> None:
  95. """ This set description of the product, and validate it."""
  96. if description_validator(target).invalid:
  97. raise validator_exception("product.description")
  98. self.__target.description = target
  99. @property
  100. def barcode(self) -> str | None:
  101. """ It return barcode of building product. """
  102. return self.__target.badcode
  103. @barcode.setter
  104. def barcode(self, target: str) -> None:
  105. """ This set new barcode, and validate it. """
  106. if barcode_validator(target).invalid:
  107. raise validator_exception("product.barcode")
  108. self.__target.badcode = barcode