From 71d155613f10344fdda0ff9d73cb0fa0a346e937 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 | 23 ++++++----------------- counter/schemas.py | 11 ++++------- 2 files changed, 10 insertions(+), 24 deletions(-) diff --git a/counter/api.py b/counter/api.py index 42b370d0..11698124 100644 --- a/counter/api.py +++ b/counter/api.py @@ -22,7 +22,6 @@ from ninja_extra.pagination import PageNumberPaginationExtra from ninja_extra.schemas import PaginatedResponseSchema from core.api_permissions import CanAccessLookup, CanView, IsOldSubscriber, IsRoot -from core.models import User from counter.models import Counter, Permanency, Product from counter.schemas import ( CounterFilterSchema, @@ -38,22 +37,20 @@ from counter.schemas import ( 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 - @route.get( +@route.get( "/search", response=PaginatedResponseSchema[SimplifiedCounterSchema], permissions=[CanAccessLookup], @@ -80,22 +77,14 @@ class ProductController(ControllerBase): .values() ) - @api_controller("/permanency") class PermanencyController(ControllerBase): @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 + return filters.filter(Permanency.objects.all()).distinct().order_by("-start") diff --git a/counter/schemas.py b/counter/schemas.py index 687de32b..c86b8705 100644 --- a/counter/schemas.py +++ b/counter/schemas.py @@ -5,29 +5,26 @@ from annotated_types import MinLen from ninja import Field, FilterSchema, ModelSchema from pydantic import Field -from core.schemas import SimpleUserSchema from counter.models import Counter, Permanency, Product 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")