import uuid from datetime import date, datetime from decimal import Decimal from typing import List, Optional from pydantic import validator from pydantic.main import BaseModel from . import to_camel from .cost_centre import CostCentreLink from .product import ProductLink from .user_link import UserLink class ClosingStockItem(BaseModel): id_: Optional[uuid.UUID] product: ProductLink group: str quantity: Decimal amount: Decimal physical: Decimal cost_centre: Optional[CostCentreLink] class Config: anystr_strip_whitespace = True alias_generator = to_camel class ClosingStock(BaseModel): date_: date cost_centre: Optional[CostCentreLink] items: List[ClosingStockItem] creation_date: Optional[datetime] last_edit_date: Optional[datetime] user: Optional[UserLink] posted: bool poster: Optional[UserLink] class Config: anystr_strip_whitespace = True alias_generator = to_camel json_encoders = {date: lambda v: v.strftime("%d-%b-%Y"), datetime: lambda v: v.strftime("%d-%b-%Y %H:%M")} @validator("date_", pre=True) def parse_date(cls, value): if isinstance(value, date): return value return datetime.strptime(value, "%d-%b-%Y").date() @validator("creation_date", pre=True) def parse_creation_date(cls, value): if value is None: return None if isinstance(value, datetime): return value return datetime.strptime(value, "%d-%b-%Y %H:%M") @validator("last_edit_date", pre=True) def parse_last_edit_date(cls, value): if value is None: return None if isinstance(value, datetime): return value return datetime.strptime(value, "%d-%b-%Y %H:%M")