Files
Fulfillment-Backend/schemas/product.py
2024-05-11 01:10:54 +03:00

100 lines
2.1 KiB
Python

from typing import List
from schemas.barcode import BarcodeTemplateSchema
from schemas.base import CustomModelCamel, PaginationInfoSchema, OkMessageSchema
from pydantic import field_validator
from models import ProductBarcode
# region Entities
class ProductSchema(CustomModelCamel):
id: int
name: str
article: str
client_id: int
barcodes: list[str]
barcode_template: BarcodeTemplateSchema | None = None
# Attributes
brand: str | None = None
color: str | None = None
composition: str | None = None
size: str | None = None
additional_info: str | None = None
@field_validator('barcodes', mode="before")
def barcodes_to_list(cls, v):
if isinstance(v, list) and all([type(barcode) is ProductBarcode for barcode in v]):
return [barcode.barcode for barcode in v]
return v
# endregion
# region Requests
class ProductCreateRequest(CustomModelCamel):
name: str
article: str
client_id: int
barcodes: List[str]
barcode_template: BarcodeTemplateSchema | None = None
class ProductDeleteRequest(CustomModelCamel):
product_id: int
class ProductUpdateRequest(CustomModelCamel):
product: ProductSchema
class ProductAddBarcodeRequest(CustomModelCamel):
product_id: int
barcode: str
class ProductDeleteBarcodeRequest(CustomModelCamel):
product_id: int
barcode: str
class ProductGenerateBarcodeRequest(CustomModelCamel):
product_id: int
# endregion
# region Responses
class ProductCreateResponse(OkMessageSchema):
product_id: int | None = None
class ProductGetResponse(CustomModelCamel):
products: List[ProductSchema]
pagination_info: PaginationInfoSchema
class ProductDeleteResponse(OkMessageSchema):
pass
class ProductUpdateResponse(OkMessageSchema):
pass
class ProductAddBarcodeResponse(OkMessageSchema):
pass
class ProductDeleteBarcodeResponse(OkMessageSchema):
pass
class ProductGenerateBarcodeResponse(OkMessageSchema):
barcode: str
class ProductExistsBarcodeResponse(CustomModelCamel):
exists: bool
# endregion