1
from __future__ import annotations
3
from typing import Optional, List, Any
5
from fastapi_filter.contrib.sqlalchemy import Filter
6
from pydantic import BaseModel, Field, model_validator
7
from pydantic.types import UUID4
9
from app.basic.product.models.product_models import Product, ProductType
10
from app.basic.uom.schemas import UomScheme
11
from core.schemas import BaseFilter
12
from core.schemas.basic_schemes import BasicModel
13
from core.schemas.list_schema import GenericListSchema
14
from core.schemas.timestamps import TimeStampScheme
17
class ProductBaseScheme(BasicModel):
18
vars: Optional[dict] = None
19
title: str = Field(title='Title', table=True, form=True)
20
description: Optional[str] = Field(default=None, title='Description', table=True, form=True)
21
external_number: Optional[str] = Field(default=None, title='External #', table=True, form=True)
22
product_type: ProductType = Field(default=ProductType.STORABLE, title='Type', table=True, form=True)
23
uom_id: UUID4 = Field(title='Uom', model='uom')
24
image_url: Optional[str] = Field(default=None, title='Image Url')
25
product_category_id: UUID4 = Field(title='Product Category', table=True, form=True, model='product_category')
26
barcode_list: list[str] = Field(default=None, title='Barcodes', table=True, form=True)
30
from_attributes = True
33
@model_validator(mode='before')
35
def check_card_number_omitted(cls, data: Any) -> Any:
36
if isinstance(data, dict):
37
barcode_list_str = data.get('barcode_list')
38
if barcode_list_str and isinstance(barcode_list_str, str):
40
barcode_list = barcode_list_str.split(',')
41
data['barcode_list'] = barcode_list
42
except Exception as ex:
46
class ProductUpdateScheme(ProductBaseScheme):
50
class ProductCreateScheme(ProductBaseScheme):
54
class ProductScheme(ProductCreateScheme, TimeStampScheme):
57
uom_rel: UomScheme = Field(title='Uom')
58
company_id: UUID4 = Field(title='Company ID', model='company')
62
class ProductFilter(BaseFilter):
63
title__in: Optional[List[str]] = Field(description="title", default=None)
66
populate_by_name = True
68
class Constants(Filter.Constants):
70
ordering_field_name = "order_by"
71
search_field_name = "search"
72
search_model_fields = ["title", "external_number"]
75
class ProductListSchema(GenericListSchema):
76
data: Optional[List['ProductScheme']]