Sith/counter/schemas.py

47 lines
1.2 KiB
Python
Raw Normal View History

2024-10-12 15:30:19 +00:00
from datetime import datetime
from typing import Annotated
from annotated_types import MinLen
from ninja import Field, FilterSchema, ModelSchema
2024-07-18 18:23:30 +00:00
2024-10-12 15:30:19 +00:00
from counter.models import Counter, Permanency, Product
2024-07-18 18:23:30 +00:00
class CounterSchema(ModelSchema):
class Meta:
model = Counter
2024-10-13 22:48:05 +00:00
fields = ["id", "name", "type"]
2024-10-12 15:30:19 +00:00
class PermanencySchema(ModelSchema):
2024-10-13 22:48:05 +00:00
counter: CounterSchema
2024-10-12 15:30:19 +00:00
class Meta:
model = Permanency
2024-10-13 22:48:05 +00:00
fields = ["start", "end"]
2024-10-12 15:30:19 +00:00
class PermanencyFilterSchema(FilterSchema):
2024-10-20 13:53:02 +00:00
start_after: datetime | None = Field(None, q="start__gte")
start_before: datetime | None = Field(None, q="start__lte")
end_after: datetime | None = Field(None, q="end__gte")
end_before: datetime | None = Field(None, q="end__lte")
took_place_after: datetime | None = Field(None, q=["start__gte", "end__gte"])
2024-10-12 15:30:19 +00:00
counter: set[int] | None = Field(None, q="counter_id__in")
class CounterFilterSchema(FilterSchema):
search: Annotated[str, MinLen(1)] = Field(None, q="name__icontains")
class SimplifiedCounterSchema(ModelSchema):
class Meta:
model = Counter
fields = ["id", "name"]
class ProductSchema(ModelSchema):
class Meta:
model = Product
fields = ["id", "name", "code"]