functionnal api

This commit is contained in:
NaNoMelo 2024-10-14 00:48:05 +02:00 committed by imperosol
parent e30a6e8e6e
commit 71d155613f
2 changed files with 10 additions and 24 deletions

View File

@ -22,7 +22,6 @@ from ninja_extra.pagination import PageNumberPaginationExtra
from ninja_extra.schemas import PaginatedResponseSchema from ninja_extra.schemas import PaginatedResponseSchema
from core.api_permissions import CanAccessLookup, CanView, IsOldSubscriber, IsRoot from core.api_permissions import CanAccessLookup, CanView, IsOldSubscriber, IsRoot
from core.models import User
from counter.models import Counter, Permanency, Product from counter.models import Counter, Permanency, Product
from counter.schemas import ( from counter.schemas import (
CounterFilterSchema, CounterFilterSchema,
@ -38,17 +37,15 @@ from counter.schemas import (
class CounterController(ControllerBase): class CounterController(ControllerBase):
@route.get("", response=list[CounterSchema], permissions=[IsRoot]) @route.get("", response=list[CounterSchema], permissions=[IsRoot])
def fetch_all(self): def fetch_all(self):
return Counter.objects.annotate_is_open() return Counter.objects.all()
@route.get("{counter_id}/", response=CounterSchema, permissions=[CanView]) @route.get("{counter_id}/", response=CounterSchema, permissions=[CanView])
def fetch_one(self, counter_id: int): def fetch_one(self, counter_id: int):
return self.get_object_or_exception( return self.get_object_or_exception(Counter.objects.all(), pk=counter_id)
Counter.objects.annotate_is_open(), pk=counter_id
)
@route.get("bar/", response=list[CounterSchema], permissions=[CanView]) @route.get("bar/", response=list[CounterSchema], permissions=[CanView])
def fetch_bars(self): def fetch_bars(self):
counters = list(Counter.objects.annotate_is_open().filter(type="BAR")) counters = list(Counter.objects.all().filter(type="BAR"))
for c in counters: for c in counters:
self.check_object_permissions(c) self.check_object_permissions(c)
return counters return counters
@ -80,22 +77,14 @@ class ProductController(ControllerBase):
.values() .values()
) )
@api_controller("/permanency") @api_controller("/permanency")
class PermanencyController(ControllerBase): class PermanencyController(ControllerBase):
@route.get( @route.get(
"", "",
response=PaginatedResponseSchema[PermanencySchema], response=PaginatedResponseSchema[PermanencySchema],
permissions=[IsOldSubscriber], permissions=[IsOldSubscriber],
exclude_none=True,
) )
@paginate(PageNumberPaginationExtra, page_size=100) @paginate(PageNumberPaginationExtra, page_size=100)
def fetch_permanancies(self, filters: Query[PermanencyFilterSchema]): def fetch_permanancies(self, filters: Query[PermanencyFilterSchema]):
user: User = self.context.request.user return filters.filter(Permanency.objects.all()).distinct().order_by("-start")
if not user.is_root:
filters.barmen = None
resp = filters.filter(Permanency.objects.values()).distinct()
breakpoint()
if not user.is_root:
for perm in resp:
perm.user = None
return resp

View File

@ -5,29 +5,26 @@ from annotated_types import MinLen
from ninja import Field, FilterSchema, ModelSchema from ninja import Field, FilterSchema, ModelSchema
from pydantic import Field from pydantic import Field
from core.schemas import SimpleUserSchema
from counter.models import Counter, Permanency, Product from counter.models import Counter, Permanency, Product
class CounterSchema(ModelSchema): class CounterSchema(ModelSchema):
barmen_list: list[SimpleUserSchema]
is_open: bool
class Meta: class Meta:
model = Counter model = Counter
fields = ["id", "name", "type", "club", "products"] fields = ["id", "name", "type"]
class PermanencySchema(ModelSchema): class PermanencySchema(ModelSchema):
counter: CounterSchema
class Meta: class Meta:
model = Permanency model = Permanency
fields = ["user", "counter", "start", "end", "activity"] fields = ["start", "end"]
class PermanencyFilterSchema(FilterSchema): class PermanencyFilterSchema(FilterSchema):
start_date: datetime | None = Field(None, q="start__gte") start_date: datetime | None = Field(None, q="start__gte")
end_date: datetime | None = Field(None, q="end__lte") end_date: datetime | None = Field(None, q="end__lte")
barmen: set[int] | None = Field(None, q="user_id__in")
counter: set[int] | None = Field(None, q="counter_id__in") counter: set[int] | None = Field(None, q="counter_id__in")