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 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")

View File

@ -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")