functionnal api

This commit is contained in:
NaNoMelo 2024-10-14 00:48:05 +02:00
parent 51b3ce03a1
commit 27e183c7ab
2 changed files with 21 additions and 28 deletions

View File

@ -19,7 +19,6 @@ from ninja_extra.pagination import PageNumberPaginationExtra
from ninja_extra.schemas import PaginatedResponseSchema from ninja_extra.schemas import PaginatedResponseSchema
from core.api_permissions import CanView, IsOldSubscriber, IsRoot from core.api_permissions import CanView, IsOldSubscriber, IsRoot
from core.models import User
from counter.models import Counter, Permanency from counter.models import Counter, Permanency
from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySchema from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySchema
@ -28,32 +27,28 @@ from counter.schemas import CounterSchema, PermanencyFilterSchema, PermanencySch
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
@api_controller("/permanency") @api_controller("/permanency")
class PermanencyController(ControllerBase): 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) @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

@ -3,26 +3,24 @@ from datetime import datetime
from ninja import FilterSchema, ModelSchema from ninja import FilterSchema, ModelSchema
from pydantic import Field from pydantic import Field
from core.models import User
from core.schemas import SimpleUserSchema
from counter.models import Counter, Permanency from counter.models import Counter, Permanency
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")