2024-12-16 18:46:34 +00:00
|
|
|
from typing import Annotated, Self
|
2024-10-19 22:18:53 +00:00
|
|
|
|
|
|
|
from annotated_types import MinLen
|
2024-12-13 22:58:25 +00:00
|
|
|
from django.urls import reverse
|
2024-12-16 18:46:34 +00:00
|
|
|
from ninja import Field, FilterSchema, ModelSchema, Schema
|
|
|
|
from pydantic import model_validator
|
2024-07-18 18:23:30 +00:00
|
|
|
|
2024-12-13 22:58:25 +00:00
|
|
|
from club.schemas import ClubSchema
|
|
|
|
from core.schemas import GroupSchema, SimpleUserSchema
|
|
|
|
from counter.models import Counter, Product, ProductType
|
2024-07-18 18:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CounterSchema(ModelSchema):
|
|
|
|
barmen_list: list[SimpleUserSchema]
|
|
|
|
is_open: bool
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Counter
|
|
|
|
fields = ["id", "name", "type", "club", "products"]
|
2024-10-19 22:18:53 +00:00
|
|
|
|
|
|
|
|
|
|
|
class CounterFilterSchema(FilterSchema):
|
|
|
|
search: Annotated[str, MinLen(1)] = Field(None, q="name__icontains")
|
|
|
|
|
|
|
|
|
|
|
|
class SimplifiedCounterSchema(ModelSchema):
|
|
|
|
class Meta:
|
|
|
|
model = Counter
|
|
|
|
fields = ["id", "name"]
|
|
|
|
|
|
|
|
|
2024-12-13 22:58:25 +00:00
|
|
|
class ProductTypeSchema(ModelSchema):
|
2024-12-16 18:46:34 +00:00
|
|
|
class Meta:
|
|
|
|
model = ProductType
|
|
|
|
fields = ["id", "name", "description", "comment", "icon", "order"]
|
|
|
|
|
|
|
|
url: str
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def resolve_url(obj: ProductType) -> str:
|
2024-12-18 11:16:24 +00:00
|
|
|
return reverse("counter:product_type_edit", kwargs={"type_id": obj.id})
|
2024-12-16 18:46:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SimpleProductTypeSchema(ModelSchema):
|
2024-12-13 22:58:25 +00:00
|
|
|
class Meta:
|
|
|
|
model = ProductType
|
|
|
|
fields = ["id", "name"]
|
|
|
|
|
|
|
|
|
2024-12-16 18:46:34 +00:00
|
|
|
class ReorderProductTypeSchema(Schema):
|
|
|
|
below: int | None = None
|
|
|
|
above: int | None = None
|
|
|
|
|
|
|
|
@model_validator(mode="after")
|
|
|
|
def validate_exclusive(self) -> Self:
|
|
|
|
if self.below is None and self.above is None:
|
|
|
|
raise ValueError("Either 'below' or 'above' must be set.")
|
|
|
|
if self.below is not None and self.above is not None:
|
|
|
|
raise ValueError("Only one of 'below' or 'above' must be set.")
|
|
|
|
return self
|
|
|
|
|
|
|
|
|
2024-12-13 22:58:25 +00:00
|
|
|
class SimpleProductSchema(ModelSchema):
|
2024-10-19 22:18:53 +00:00
|
|
|
class Meta:
|
|
|
|
model = Product
|
|
|
|
fields = ["id", "name", "code"]
|
2024-12-13 22:58:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ProductSchema(ModelSchema):
|
|
|
|
class Meta:
|
|
|
|
model = Product
|
|
|
|
fields = [
|
|
|
|
"id",
|
|
|
|
"name",
|
|
|
|
"code",
|
|
|
|
"description",
|
|
|
|
"purchase_price",
|
|
|
|
"selling_price",
|
|
|
|
"icon",
|
|
|
|
"limit_age",
|
|
|
|
"archived",
|
|
|
|
]
|
|
|
|
|
|
|
|
buying_groups: list[GroupSchema]
|
|
|
|
club: ClubSchema
|
2024-12-16 18:46:34 +00:00
|
|
|
product_type: SimpleProductTypeSchema | None
|
2024-12-13 22:58:25 +00:00
|
|
|
url: str
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def resolve_url(obj: Product) -> str:
|
|
|
|
return reverse("counter:product_edit", kwargs={"product_id": obj.id})
|
|
|
|
|
|
|
|
|
|
|
|
class ProductFilterSchema(FilterSchema):
|
|
|
|
search: Annotated[str, MinLen(1)] | None = Field(
|
|
|
|
None, q=["name__icontains", "code__icontains"]
|
|
|
|
)
|
|
|
|
is_archived: bool | None = Field(None, q="archived")
|
|
|
|
buying_groups: set[int] | None = Field(None, q="buying_groups__in")
|
|
|
|
product_type: set[int] | None = Field(None, q="product_type__in")
|