2024-07-18 18:23:30 +00:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
#
|
2024-09-22 23:37:25 +00:00
|
|
|
# You can find the source code of the website at https://github.com/ae-utbm/sith
|
2024-07-18 18:23:30 +00:00
|
|
|
#
|
|
|
|
# LICENSED UNDER THE GNU GENERAL PUBLIC LICENSE VERSION 3 (GPLv3)
|
2024-09-23 08:25:27 +00:00
|
|
|
# SEE : https://raw.githubusercontent.com/ae-utbm/sith/master/LICENSE
|
2024-07-18 18:23:30 +00:00
|
|
|
# OR WITHIN THE LOCAL FILE "LICENSE"
|
|
|
|
#
|
|
|
|
#
|
2024-10-19 22:18:53 +00:00
|
|
|
from typing import Annotated
|
2024-07-18 18:23:30 +00:00
|
|
|
|
2024-10-19 22:18:53 +00:00
|
|
|
from annotated_types import MinLen
|
|
|
|
from django.db.models import Q
|
|
|
|
from ninja import Query
|
|
|
|
from ninja_extra import ControllerBase, api_controller, paginate, route
|
|
|
|
from ninja_extra.pagination import PageNumberPaginationExtra
|
2024-10-15 19:41:18 +00:00
|
|
|
from ninja_extra.permissions import IsAuthenticated
|
2024-10-19 22:18:53 +00:00
|
|
|
from ninja_extra.schemas import PaginatedResponseSchema
|
|
|
|
|
2024-10-15 19:41:18 +00:00
|
|
|
from core.api_permissions import CanAccessLookup, CanView, IsRoot
|
2024-10-12 15:30:19 +00:00
|
|
|
from counter.models import Counter, Permanency, Product
|
2024-10-19 22:18:53 +00:00
|
|
|
from counter.schemas import (
|
|
|
|
CounterFilterSchema,
|
|
|
|
CounterSchema,
|
2024-10-12 15:30:19 +00:00
|
|
|
PermanencyFilterSchema,
|
|
|
|
PermanencySchema,
|
2024-10-19 22:18:53 +00:00
|
|
|
ProductSchema,
|
|
|
|
SimplifiedCounterSchema,
|
|
|
|
)
|
2024-07-18 18:23:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
@api_controller("/counter")
|
|
|
|
class CounterController(ControllerBase):
|
|
|
|
@route.get("", response=list[CounterSchema], permissions=[IsRoot])
|
|
|
|
def fetch_all(self):
|
2024-10-13 22:48:05 +00:00
|
|
|
return Counter.objects.all()
|
2024-07-18 18:23:30 +00:00
|
|
|
|
|
|
|
@route.get("{counter_id}/", response=CounterSchema, permissions=[CanView])
|
|
|
|
def fetch_one(self, counter_id: int):
|
2024-10-13 22:48:05 +00:00
|
|
|
return self.get_object_or_exception(Counter.objects.all(), pk=counter_id)
|
2024-07-18 18:23:30 +00:00
|
|
|
|
|
|
|
@route.get("bar/", response=list[CounterSchema], permissions=[CanView])
|
|
|
|
def fetch_bars(self):
|
2024-10-13 22:48:05 +00:00
|
|
|
counters = list(Counter.objects.all().filter(type="BAR"))
|
2024-07-18 18:23:30 +00:00
|
|
|
for c in counters:
|
|
|
|
self.check_object_permissions(c)
|
|
|
|
return counters
|
2024-10-19 22:18:53 +00:00
|
|
|
|
2024-10-15 19:41:18 +00:00
|
|
|
@route.get(
|
2024-10-19 22:18:53 +00:00
|
|
|
"/search",
|
|
|
|
response=PaginatedResponseSchema[SimplifiedCounterSchema],
|
|
|
|
permissions=[CanAccessLookup],
|
|
|
|
)
|
|
|
|
@paginate(PageNumberPaginationExtra, page_size=50)
|
|
|
|
def search_counter(self, filters: Query[CounterFilterSchema]):
|
|
|
|
return filters.filter(Counter.objects.all())
|
|
|
|
|
|
|
|
|
|
|
|
@api_controller("/product")
|
|
|
|
class ProductController(ControllerBase):
|
|
|
|
@route.get(
|
|
|
|
"/search",
|
|
|
|
response=PaginatedResponseSchema[ProductSchema],
|
|
|
|
permissions=[CanAccessLookup],
|
|
|
|
)
|
|
|
|
@paginate(PageNumberPaginationExtra, page_size=50)
|
|
|
|
def search_products(self, search: Annotated[str, MinLen(1)]):
|
|
|
|
return (
|
|
|
|
Product.objects.filter(
|
|
|
|
Q(name__icontains=search) | Q(code__icontains=search)
|
|
|
|
)
|
|
|
|
.filter(archived=False)
|
|
|
|
.values()
|
|
|
|
)
|
2024-10-12 15:30:19 +00:00
|
|
|
|
2024-10-15 19:41:18 +00:00
|
|
|
|
2024-10-12 15:30:19 +00:00
|
|
|
@api_controller("/permanency")
|
|
|
|
class PermanencyController(ControllerBase):
|
|
|
|
@route.get(
|
|
|
|
"",
|
|
|
|
response=PaginatedResponseSchema[PermanencySchema],
|
2024-10-15 19:41:18 +00:00
|
|
|
permissions=[IsAuthenticated],
|
2024-10-13 22:48:05 +00:00
|
|
|
exclude_none=True,
|
2024-10-12 15:30:19 +00:00
|
|
|
)
|
|
|
|
@paginate(PageNumberPaginationExtra, page_size=100)
|
|
|
|
def fetch_permanancies(self, filters: Query[PermanencyFilterSchema]):
|
2024-10-13 22:48:05 +00:00
|
|
|
return filters.filter(Permanency.objects.all()).distinct().order_by("-start")
|