|
@@ -3,6 +3,8 @@ import sqlmodel
|
|
|
from .exception import validator_exception
|
|
from .exception import validator_exception
|
|
|
from .validator import name_validator
|
|
from .validator import name_validator
|
|
|
from .validator import description_validator
|
|
from .validator import description_validator
|
|
|
|
|
+from .validator import barcode_validator
|
|
|
|
|
+from .validator import author_validator
|
|
|
|
|
|
|
|
class product(sqlmodel.SQLModel, table = True):
|
|
class product(sqlmodel.SQLModel, table = True):
|
|
|
"""
|
|
"""
|
|
@@ -29,7 +31,7 @@ class product(sqlmodel.SQLModel, table = True):
|
|
|
stock_count: int = sqlmodel.Field(default = 0)
|
|
stock_count: int = sqlmodel.Field(default = 0)
|
|
|
|
|
|
|
|
""" This is barcode (EAN) of the product. """
|
|
""" This is barcode (EAN) of the product. """
|
|
|
- badcode: str | None = sqlmodel.Field(
|
|
|
|
|
|
|
+ barcode: str | None = sqlmodel.Field(
|
|
|
default = None,
|
|
default = None,
|
|
|
index = True,
|
|
index = True,
|
|
|
unique = True
|
|
unique = True
|
|
@@ -41,6 +43,12 @@ class product(sqlmodel.SQLModel, table = True):
|
|
|
|
|
|
|
|
return self.id is not None
|
|
return self.id is not None
|
|
|
|
|
|
|
|
|
|
+ @property
|
|
|
|
|
+ def avairable(self) -> bool:
|
|
|
|
|
+ """ This check that item is avairable on stock. """
|
|
|
|
|
+
|
|
|
|
|
+ return self.stock_count > 0
|
|
|
|
|
+
|
|
|
def __str__(self) -> str:
|
|
def __str__(self) -> str:
|
|
|
"""
|
|
"""
|
|
|
This dump product to string, which is helpfull when debug.
|
|
This dump product to string, which is helpfull when debug.
|
|
@@ -49,7 +57,12 @@ class product(sqlmodel.SQLModel, table = True):
|
|
|
(str): Product as string dump
|
|
(str): Product as string dump
|
|
|
"""
|
|
"""
|
|
|
|
|
|
|
|
- content = str(self.name) + " " + str(self.id or "") + "\n"
|
|
|
|
|
|
|
+ content = str(self.name) + " "
|
|
|
|
|
+
|
|
|
|
|
+ if self.in_database:
|
|
|
|
|
+ content = content + "#" + str(self.id)
|
|
|
|
|
+
|
|
|
|
|
+ content = content + "\n"
|
|
|
content = content + "Description: " + str(self.description) + "\n"
|
|
content = content + "Description: " + str(self.description) + "\n"
|
|
|
content = content + "Author: " + str(self.author) + "\n"
|
|
content = content + "Author: " + str(self.author) + "\n"
|
|
|
content = content + "Barcode (EAN): " + str(self.barcode) + "\n"
|
|
content = content + "Barcode (EAN): " + str(self.barcode) + "\n"
|
|
@@ -62,10 +75,10 @@ class product(sqlmodel.SQLModel, table = True):
|
|
|
def ready(self) -> bool:
|
|
def ready(self) -> bool:
|
|
|
""" Check that product is ready to insert into database. """
|
|
""" Check that product is ready to insert into database. """
|
|
|
|
|
|
|
|
- if name is None or description is None:
|
|
|
|
|
|
|
+ if self.name is None or self.description is None:
|
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
- if barcode is None or author is None:
|
|
|
|
|
|
|
+ if self.barcode is None or self.author is None:
|
|
|
return False
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
return True
|
|
@@ -128,6 +141,27 @@ class product_builder:
|
|
|
|
|
|
|
|
return self.__target.description
|
|
return self.__target.description
|
|
|
|
|
|
|
|
|
|
+ def add_to_stock(self) -> None:
|
|
|
|
|
+ """
|
|
|
|
|
+ This add one item to stock.
|
|
|
|
|
+ """
|
|
|
|
|
+
|
|
|
|
|
+ self.__target.stock_count += 1
|
|
|
|
|
+
|
|
|
|
|
+ def get_from_stock(self) -> bool:
|
|
|
|
|
+ """
|
|
|
|
|
+ This get single item from stock.
|
|
|
|
|
+
|
|
|
|
|
+ Returns:
|
|
|
|
|
+ (bool): True when get success, false when not avairable
|
|
|
|
|
+ """
|
|
|
|
|
+
|
|
|
|
|
+ if not self.__target.avairable:
|
|
|
|
|
+ return False
|
|
|
|
|
+
|
|
|
|
|
+ self.__target.stock_count -= 1
|
|
|
|
|
+ return True
|
|
|
|
|
+
|
|
|
@description.setter
|
|
@description.setter
|
|
|
def description(self, target: str) -> None:
|
|
def description(self, target: str) -> None:
|
|
|
""" This set description of the product, and validate it."""
|
|
""" This set description of the product, and validate it."""
|
|
@@ -141,7 +175,7 @@ class product_builder:
|
|
|
def barcode(self) -> str | None:
|
|
def barcode(self) -> str | None:
|
|
|
""" It return barcode of building product. """
|
|
""" It return barcode of building product. """
|
|
|
|
|
|
|
|
- return self.__target.badcode
|
|
|
|
|
|
|
+ return self.__target.barcode
|
|
|
|
|
|
|
|
@barcode.setter
|
|
@barcode.setter
|
|
|
def barcode(self, target: str) -> None:
|
|
def barcode(self, target: str) -> None:
|
|
@@ -150,4 +184,46 @@ class product_builder:
|
|
|
if barcode_validator(target).invalid:
|
|
if barcode_validator(target).invalid:
|
|
|
raise validator_exception("product.barcode")
|
|
raise validator_exception("product.barcode")
|
|
|
|
|
|
|
|
- self.__target.badcode = barcode
|
|
|
|
|
|
|
+ self.__target.barcode = target
|
|
|
|
|
+
|
|
|
|
|
+ @property
|
|
|
|
|
+ def author(self) -> str | None:
|
|
|
|
|
+ """ This return author of the product. """
|
|
|
|
|
+
|
|
|
|
|
+ return self.__target.author
|
|
|
|
|
+
|
|
|
|
|
+ @author.setter
|
|
|
|
|
+ def author(self, target: str) -> None:
|
|
|
|
|
+ """ This validate author of the product, and set it. """
|
|
|
|
|
+
|
|
|
|
|
+ if author_validator(target).invalid:
|
|
|
|
|
+ raise validator_exception("product.author")
|
|
|
|
|
+
|
|
|
|
|
+ self.__target.author = target
|
|
|
|
|
+
|
|
|
|
|
+ @property
|
|
|
|
|
+ def image(self) -> str | None:
|
|
|
|
|
+ """ This return image link of the product. """
|
|
|
|
|
+
|
|
|
|
|
+ return self.__target.image
|
|
|
|
|
+
|
|
|
|
|
+ @image.setter
|
|
|
|
|
+ def image(self, target: str | None) -> None:
|
|
|
|
|
+ """ This set image link of the product. """
|
|
|
|
|
+
|
|
|
|
|
+ self.__target.image
|
|
|
|
|
+
|
|
|
|
|
+ @property
|
|
|
|
|
+ def stock_count(self) -> int:
|
|
|
|
|
+ """ This return how much product is in stock. """
|
|
|
|
|
+
|
|
|
|
|
+ return self.__target.stock_count
|
|
|
|
|
+
|
|
|
|
|
+ @stock_count.setter
|
|
|
|
|
+ def stock_count(self, target: int) -> None:
|
|
|
|
|
+ """ This set stock count of the product, it mut be positive number. """
|
|
|
|
|
+
|
|
|
|
|
+ if target < 0:
|
|
|
|
|
+ raise validator_exception("product.stock_count")
|
|
|
|
|
+
|
|
|
|
|
+ self.__target.stock_count = target
|