api route to search products with detailed infos.

This commit is contained in:
imperosol
2024-12-13 23:58:25 +01:00
parent e680124d7b
commit 6c8a6008d5
6 changed files with 144 additions and 23 deletions

View File

@ -1,10 +1,12 @@
from typing import Annotated
from annotated_types import MinLen
from django.urls import reverse
from ninja import Field, FilterSchema, ModelSchema
from core.schemas import SimpleUserSchema
from counter.models import Counter, Product
from club.schemas import ClubSchema
from core.schemas import GroupSchema, SimpleUserSchema
from counter.models import Counter, Product, ProductType
class CounterSchema(ModelSchema):
@ -26,7 +28,47 @@ class SimplifiedCounterSchema(ModelSchema):
fields = ["id", "name"]
class ProductSchema(ModelSchema):
class ProductTypeSchema(ModelSchema):
class Meta:
model = ProductType
fields = ["id", "name"]
class SimpleProductSchema(ModelSchema):
class Meta:
model = Product
fields = ["id", "name", "code"]
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
product_type: ProductTypeSchema | None
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")