67 lines
1.7 KiB
Python
67 lines
1.7 KiB
Python
import uuid
|
|
|
|
from datetime import date, datetime
|
|
from decimal import Decimal
|
|
from typing import Optional
|
|
|
|
from pydantic import BaseModel, Field, validator
|
|
|
|
from . import to_camel
|
|
from .menu_category import MenuCategoryLink
|
|
from .sale_category import SaleCategoryLink
|
|
|
|
|
|
class ProductIn(BaseModel):
|
|
name: str = Field(..., min_length=1)
|
|
units: str
|
|
menu_category: MenuCategoryLink = Field(...)
|
|
sale_category: SaleCategoryLink = Field(...)
|
|
price: Decimal # = Field(ge=0, multiple_of=0.01, default=0)
|
|
has_happy_hour: bool
|
|
is_not_available: bool
|
|
quantity: Decimal = Field(ge=0, multiple_of=0.01, default=0)
|
|
is_active: bool
|
|
sort_order: int
|
|
|
|
class Config:
|
|
anystr_strip_whitespace = True
|
|
alias_generator = to_camel
|
|
|
|
|
|
class Product(ProductIn):
|
|
id_: uuid.UUID
|
|
version_id: uuid.UUID
|
|
valid_from: Optional[date]
|
|
valid_till: Optional[date]
|
|
|
|
class Config:
|
|
anystr_strip_whitespace = True
|
|
alias_generator = to_camel
|
|
json_encoders = {date: lambda v: v.strftime("%d-%b-%Y")}
|
|
|
|
@validator("valid_from", pre=True)
|
|
def parse_valid_from(cls, value):
|
|
if value is None:
|
|
return None
|
|
if isinstance(value, date):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|
|
|
|
@validator("valid_till", pre=True)
|
|
def parse_valid_till(cls, value):
|
|
if value is None:
|
|
return None
|
|
if isinstance(value, date):
|
|
return value
|
|
return datetime.strptime(value, "%d-%b-%Y").date()
|
|
|
|
|
|
class ProductBlank(ProductIn):
|
|
name: str
|
|
menu_category: Optional[MenuCategoryLink]
|
|
sale_category: Optional[SaleCategoryLink]
|
|
|
|
class Config:
|
|
anystr_strip_whitespace = True
|
|
alias_generator = to_camel
|