- ProductOption and ProductPreferenceChoice gain sub_choices (JSON Text column)
for nested inline choices shown when the parent is selected
- ProductPreferenceSet gains default_choice_id and shared_subset (set-level
sub-choice group shown for all choices that don't disable it)
- Product gains sort_order column; list endpoint orders by sort_order
- New PUT /products/reorder endpoint for drag-and-drop ordering
- DELETE /products/{id} now accepts ?hard=true for permanent deletion (blocked
if product appears in any past order)
- Schemas updated with model_validators to parse stored JSON back to typed objects
- Add python-multipart to requirements (needed for file upload form parsing)
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
235 lines
6.6 KiB
Python
235 lines
6.6 KiB
Python
import json
|
|
from pydantic import BaseModel, model_validator, field_validator
|
|
from typing import Optional, List, Any
|
|
|
|
|
|
class CategoryCreate(BaseModel):
|
|
name: str
|
|
color: Optional[str] = None
|
|
sort_order: int = 0
|
|
|
|
|
|
class CategoryUpdate(BaseModel):
|
|
name: Optional[str] = None
|
|
color: Optional[str] = None
|
|
sort_order: Optional[int] = None
|
|
|
|
|
|
class CategoryOut(BaseModel):
|
|
id: int
|
|
name: str
|
|
color: Optional[str] = None
|
|
sort_order: int = 0
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
|
|
class CategoryReorderItem(BaseModel):
|
|
id: int
|
|
sort_order: int
|
|
|
|
|
|
# ── Options ──────────────────────────────────────────────────────────────────
|
|
|
|
class OptionSubChoice(BaseModel):
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
is_default: bool = False
|
|
|
|
|
|
class ProductOptionBase(BaseModel):
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
|
|
|
|
class ProductOptionCreate(ProductOptionBase):
|
|
sub_choices: List[OptionSubChoice] = []
|
|
|
|
|
|
class ProductOptionOut(ProductOptionBase):
|
|
id: int
|
|
product_id: int
|
|
sub_choices: List[OptionSubChoice] = []
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
@model_validator(mode='before')
|
|
@classmethod
|
|
def parse_option_sub_choices(cls, data: Any) -> Any:
|
|
if hasattr(data, 'sub_choices'):
|
|
raw = data.sub_choices
|
|
parsed = json.loads(raw) if isinstance(raw, str) else []
|
|
return {
|
|
'id': data.id,
|
|
'product_id': data.product_id,
|
|
'name': data.name,
|
|
'extra_cost': data.extra_cost,
|
|
'sub_choices': parsed,
|
|
}
|
|
return data
|
|
|
|
|
|
# ── Ingredients ───────────────────────────────────────────────────────────────
|
|
|
|
class ProductIngredientBase(BaseModel):
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
|
|
|
|
class ProductIngredientCreate(ProductIngredientBase):
|
|
pass
|
|
|
|
|
|
class ProductIngredientOut(ProductIngredientBase):
|
|
id: int
|
|
product_id: int
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
|
|
# ── Sub-choices (nested under a preference choice) ────────────────────────────
|
|
|
|
class SubChoice(BaseModel):
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
is_default: bool = False
|
|
|
|
|
|
# ── Shared subset (set-level, shown for all non-disabling choices) ─────────────
|
|
|
|
class SharedSubsetChoice(BaseModel):
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
is_default: bool = False
|
|
|
|
|
|
class SharedSubset(BaseModel):
|
|
name: str
|
|
choices: List[SharedSubsetChoice] = []
|
|
|
|
|
|
# ── Preferences ───────────────────────────────────────────────────────────────
|
|
|
|
class PreferenceChoiceCreate(BaseModel):
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
sub_choices: List[SubChoice] = []
|
|
disables_subset: bool = False
|
|
|
|
|
|
class PreferenceChoiceOut(BaseModel):
|
|
id: int
|
|
set_id: int
|
|
name: str
|
|
extra_cost: float = 0.0
|
|
sub_choices: List[SubChoice] = []
|
|
disables_subset: bool = False
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
@model_validator(mode='before')
|
|
@classmethod
|
|
def parse_sub_choices(cls, data: Any) -> Any:
|
|
if hasattr(data, 'sub_choices'):
|
|
raw = data.sub_choices
|
|
if isinstance(raw, str):
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except Exception:
|
|
parsed = []
|
|
else:
|
|
parsed = []
|
|
return {
|
|
'id': data.id,
|
|
'set_id': data.set_id,
|
|
'name': data.name,
|
|
'extra_cost': data.extra_cost,
|
|
'sub_choices': parsed,
|
|
'disables_subset': data.disables_subset or False,
|
|
}
|
|
return data
|
|
|
|
|
|
class PreferenceSetCreate(BaseModel):
|
|
name: str
|
|
choices: List[PreferenceChoiceCreate] = []
|
|
default_choice_index: Optional[int] = None # index into choices (0-based)
|
|
shared_subset: Optional[SharedSubset] = None
|
|
|
|
|
|
class PreferenceSetOut(BaseModel):
|
|
id: int
|
|
product_id: int
|
|
name: str
|
|
choices: List[PreferenceChoiceOut] = []
|
|
default_choice_id: Optional[int] = None
|
|
shared_subset: Optional[SharedSubset] = None
|
|
|
|
model_config = {"from_attributes": True}
|
|
|
|
@model_validator(mode='before')
|
|
@classmethod
|
|
def parse_shared_subset(cls, data: Any) -> Any:
|
|
if hasattr(data, 'shared_subset'):
|
|
raw = data.shared_subset
|
|
if isinstance(raw, str):
|
|
try:
|
|
parsed = json.loads(raw)
|
|
except Exception:
|
|
parsed = None
|
|
else:
|
|
parsed = None
|
|
return {
|
|
'id': data.id,
|
|
'product_id': data.product_id,
|
|
'name': data.name,
|
|
'choices': list(data.choices),
|
|
'default_choice_id': data.default_choice_id,
|
|
'shared_subset': parsed,
|
|
}
|
|
return data
|
|
|
|
|
|
# ── Products ──────────────────────────────────────────────────────────────────
|
|
|
|
class ProductBase(BaseModel):
|
|
name: str
|
|
category_id: Optional[int] = None
|
|
base_price: float
|
|
is_available: bool = True
|
|
printer_zone_id: Optional[int] = None
|
|
sort_order: int = 0
|
|
|
|
|
|
class ProductCreate(ProductBase):
|
|
options: List[ProductOptionCreate] = []
|
|
ingredients: List[ProductIngredientCreate] = []
|
|
preference_sets: List[PreferenceSetCreate] = []
|
|
|
|
|
|
class ProductUpdate(BaseModel):
|
|
name: Optional[str] = None
|
|
category_id: Optional[int] = None
|
|
base_price: Optional[float] = None
|
|
is_available: Optional[bool] = None
|
|
printer_zone_id: Optional[int] = None
|
|
sort_order: Optional[int] = None
|
|
options: Optional[List[ProductOptionCreate]] = None
|
|
ingredients: Optional[List[ProductIngredientCreate]] = None
|
|
preference_sets: Optional[List[PreferenceSetCreate]] = None
|
|
|
|
|
|
class ProductReorderItem(BaseModel):
|
|
id: int
|
|
sort_order: int
|
|
|
|
|
|
class ProductOut(ProductBase):
|
|
id: int
|
|
options: List[ProductOptionOut] = []
|
|
ingredients: List[ProductIngredientOut] = []
|
|
preference_sets: List[PreferenceSetOut] = []
|
|
image_url: Optional[str] = None
|
|
|
|
model_config = {"from_attributes": True}
|