Sith/counter/api.py
2024-10-14 01:05:29 +02:00

59 lines
2.2 KiB
Python

#
# Copyright 2024 AE UTBM
# ae@utbm.fr / ae.info@utbm.fr
#
# This file is part of the website of the UTBM Student Association (AE UTBM),
# https://ae.utbm.fr.
#
# You can find the source code of the website at https://github.com/ae-utbm/sith
#
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
# OR WITHIN THE LOCAL FILE "LICENSE"
#
#
from ninja import Query
from ninja_extra import ControllerBase, api_controller, paginate, route
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
@api_controller("/counter")
class CounterController(ControllerBase):
@route.get("", response=list[CounterSchema], permissions=[IsRoot])
def fetch_all(self):
return Counter.objects.annotate_is_open()
@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
)
@route.get("bar/", response=list[CounterSchema], permissions=[CanView])
def fetch_bars(self):
counters = list(Counter.objects.annotate_is_open().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])
@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