169 lines
3.9 KiB
Python
169 lines
3.9 KiB
Python
"""
|
|
Pydantic schemas for products API.
|
|
"""
|
|
from typing import Optional, List
|
|
from datetime import datetime
|
|
from decimal import Decimal
|
|
from ninja import Schema, FilterSchema
|
|
from ninja.orm import create_schema
|
|
|
|
|
|
class CategoryOut(Schema):
|
|
"""Category output schema."""
|
|
id: int
|
|
name: str
|
|
slug: str
|
|
description: Optional[str] = None
|
|
icon: Optional[str] = None
|
|
parent_id: Optional[int] = None
|
|
sort_order: int
|
|
created_at: datetime
|
|
|
|
|
|
class CategoryIn(Schema):
|
|
"""Category input schema."""
|
|
name: str
|
|
slug: str
|
|
description: Optional[str] = None
|
|
icon: Optional[str] = None
|
|
parent_id: Optional[int] = None
|
|
sort_order: int = 0
|
|
|
|
|
|
class WebsiteOut(Schema):
|
|
"""Website output schema."""
|
|
id: int
|
|
name: str
|
|
url: str
|
|
logo: Optional[str] = None
|
|
description: Optional[str] = None
|
|
category_id: int
|
|
rating: Decimal
|
|
is_verified: bool
|
|
sort_order: int
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
|
|
class WebsiteIn(Schema):
|
|
"""Website input schema."""
|
|
name: str
|
|
url: str
|
|
logo: Optional[str] = None
|
|
description: Optional[str] = None
|
|
category_id: int
|
|
rating: Decimal = Decimal("0")
|
|
is_verified: bool = False
|
|
sort_order: int = 0
|
|
|
|
|
|
class ProductPriceOut(Schema):
|
|
"""Product price output schema."""
|
|
id: int
|
|
product_id: int
|
|
website_id: int
|
|
website_name: Optional[str] = None
|
|
website_logo: Optional[str] = None
|
|
price: Decimal
|
|
original_price: Optional[Decimal] = None
|
|
currency: str
|
|
url: str
|
|
in_stock: bool
|
|
last_checked: datetime
|
|
|
|
|
|
class ProductOut(Schema):
|
|
"""Product output schema."""
|
|
id: int
|
|
name: str
|
|
description: Optional[str] = None
|
|
image: Optional[str] = None
|
|
images: List[str] = []
|
|
category_id: int
|
|
status: str = "approved"
|
|
submitted_by_id: Optional[int] = None
|
|
reject_reason: Optional[str] = None
|
|
reviewed_at: Optional[datetime] = None
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
|
|
class ProductWithPricesOut(ProductOut):
|
|
"""Product with prices output schema."""
|
|
prices: List[ProductPriceOut] = []
|
|
lowest_price: Optional[Decimal] = None
|
|
highest_price: Optional[Decimal] = None
|
|
|
|
|
|
class UploadImageOut(Schema):
|
|
"""Image upload output."""
|
|
url: str
|
|
|
|
|
|
class ProductIn(Schema):
|
|
"""Product input schema."""
|
|
name: str
|
|
description: Optional[str] = None
|
|
image: Optional[str] = None
|
|
images: Optional[List[str]] = None
|
|
category_id: int
|
|
|
|
|
|
class MyProductOut(Schema):
|
|
"""User's product output schema."""
|
|
id: int
|
|
name: str
|
|
description: Optional[str] = None
|
|
image: Optional[str] = None
|
|
images: List[str] = []
|
|
category_id: int
|
|
status: str
|
|
reject_reason: Optional[str] = None
|
|
reviewed_at: Optional[datetime] = None
|
|
created_at: datetime
|
|
updated_at: datetime
|
|
|
|
|
|
class ProductPriceIn(Schema):
|
|
"""Product price input schema."""
|
|
product_id: int
|
|
website_id: int
|
|
price: Decimal
|
|
original_price: Optional[Decimal] = None
|
|
currency: str = "CNY"
|
|
url: str
|
|
in_stock: bool = True
|
|
|
|
|
|
class ImportResultOut(Schema):
|
|
"""CSV import result."""
|
|
created_categories: int = 0
|
|
created_websites: int = 0
|
|
created_products: int = 0
|
|
created_prices: int = 0
|
|
updated_prices: int = 0
|
|
errors: List[str] = []
|
|
|
|
|
|
class ProductFilter(FilterSchema):
|
|
"""Product filter schema."""
|
|
category_id: Optional[int] = None
|
|
search: Optional[str] = None
|
|
min_price: Optional[Decimal] = None
|
|
max_price: Optional[Decimal] = None
|
|
sort_by: Optional[str] = None
|
|
|
|
|
|
class ProductSearchFilter(FilterSchema):
|
|
"""Product search filter schema."""
|
|
category_id: Optional[int] = None
|
|
min_price: Optional[Decimal] = None
|
|
max_price: Optional[Decimal] = None
|
|
sort_by: Optional[str] = None
|
|
|
|
|
|
class WebsiteFilter(FilterSchema):
|
|
"""Website filter schema."""
|
|
category_id: Optional[int] = None
|
|
is_verified: Optional[bool] = None
|