From 27e183c7ab9f27ff6e9015dbb68e0ea6fa2f5da7 Mon Sep 17 00:00:00 2001 From: NaNoMelo Date: Mon, 14 Oct 2024 00:48:05 +0200 Subject: [PATCH] functionnal api --- counter/api.py | 31 +++++++++++++------------------ counter/schemas.py | 18 ++++++++---------- 2 files changed, 21 insertions(+), 28 deletions(-) diff --git a/counter/api.py b/counter/api.py index 9c255518..c6879945 100644 --- a/counter/api.py +++ b/counter/api.py @@ -19,7 +19,6 @@ from ninja_extra.pagination import PageNumberPaginationExtra from ninja_extra.schemas import PaginatedResponseSchema from core.api_permissions import CanView, IsOldSubscriber, IsRoot -from core.models import User from counter.models import Counter, Permanency from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySchema @@ -28,32 +27,28 @@ from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySch class CounterController(ControllerBase): @route.get("", response=list[CounterSchema], permissions=[IsRoot]) def fetch_all(self): - return Counter.objects.annotate_is_open() + return Counter.objects.all() @route.get("{counter_id}/", response=CounterSchema, permissions=[CanView]) def fetch_one(self, counter_id: int): - return self.get_object_or_exception( - Counter.objects.annotate_is_open(), pk=counter_id - ) + return self.get_object_or_exception(Counter.objects.all(), pk=counter_id) @route.get("bar/", response=list[CounterSchema], permissions=[CanView]) 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: self.check_object_permissions(c) return counters - + + @api_controller("/permanency") class PermanencyController(ControllerBase): - @route.get("", response=PaginatedResponseSchema[PermanencySchema], permissions=[IsOldSubscriber]) + @route.get( + "", + response=PaginatedResponseSchema[PermanencySchema], + permissions=[IsOldSubscriber], + exclude_none=True, + ) @paginate(PageNumberPaginationExtra, page_size=100) - def fetch_permanancies(self, filters:Query[PermanencyFilterSchema]): - user: User = self.context.request.user - 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 \ No newline at end of file + def fetch_permanancies(self, filters: Query[PermanencyFilterSchema]): + return filters.filter(Permanency.objects.all()).distinct().order_by("-start") diff --git a/counter/schemas.py b/counter/schemas.py index e7a0aac9..0304aadc 100644 --- a/counter/schemas.py +++ b/counter/schemas.py @@ -3,26 +3,24 @@ from datetime import datetime from ninja import FilterSchema, ModelSchema from pydantic import Field -from core.models import User -from core.schemas import SimpleUserSchema from counter.models import Counter, Permanency class CounterSchema(ModelSchema): - barmen_list: list[SimpleUserSchema] - is_open: bool - class Meta: model = Counter - fields = ["id", "name", "type", "club", "products"] - + fields = ["id", "name", "type"] + + class PermanencySchema(ModelSchema): + counter: CounterSchema + class Meta: model = Permanency - fields = ["user", "counter", "start", "end", "activity"] - + fields = ["start", "end"] + + class PermanencyFilterSchema(FilterSchema): start_date: datetime | None = Field(None, q="start__gte") 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")