Sith/pedagogy/api.py

42 lines
1.4 KiB
Python
Raw Permalink Normal View History

2024-07-18 18:23:30 +00:00
from typing import Annotated
from annotated_types import Ge
from django.conf import settings
from ninja import Query
2024-07-22 16:40:32 +00:00
from ninja_extra import ControllerBase, api_controller, paginate, route
2024-07-18 18:23:30 +00:00
from ninja_extra.exceptions import NotFound
2024-07-22 16:40:32 +00:00
from ninja_extra.pagination import PageNumberPaginationExtra, PaginatedResponseSchema
2024-07-18 18:23:30 +00:00
from core.api_permissions import IsInGroup, IsRoot, IsSubscriber
from pedagogy.models import UV
from pedagogy.schemas import SimpleUvSchema, UvFilterSchema, UvSchema
from pedagogy.utbm_api import find_uv
2024-07-22 17:12:03 +00:00
@api_controller("/uv")
2024-07-18 18:23:30 +00:00
class UvController(ControllerBase):
@route.get(
"/{year}/{code}",
permissions=[IsRoot | IsInGroup(settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)],
url_name="fetch_uv_from_utbm",
response=UvSchema,
)
def fetch_from_utbm_api(
self, year: Annotated[int, Ge(2010)], code: str, lang: Query[str] = "fr"
):
"""Fetch UV data from the UTBM API and returns it after some parsing."""
res = find_uv(lang, year, code)
if res is None:
raise NotFound
return res
2024-07-22 16:40:32 +00:00
@route.get(
2024-07-22 17:12:03 +00:00
"",
response=PaginatedResponseSchema[SimpleUvSchema],
url_name="fetch_uvs",
permissions=[IsSubscriber | IsInGroup(settings.SITH_GROUP_PEDAGOGY_ADMIN_ID)],
2024-07-22 16:40:32 +00:00
)
@paginate(PageNumberPaginationExtra, page_size=100)
2024-07-18 18:23:30 +00:00
def fetch_uv_list(self, search: Query[UvFilterSchema]):
2024-07-22 16:40:32 +00:00
return search.filter(UV.objects.all())