mirror of
https://github.com/ae-utbm/sith.git
synced 2025-11-03 02:23:05 +00:00
Compare commits
5 Commits
fix-poster
...
club_api_f
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
482468500b | ||
|
|
533e2ff271 | ||
|
|
8bd726e7ec | ||
|
|
0a5f589c2d | ||
|
|
55cd6d4916 |
14
club/api.py
14
club/api.py
@@ -1,7 +1,5 @@
|
||||
from typing import Annotated
|
||||
|
||||
from annotated_types import MinLen
|
||||
from django.db.models import Prefetch
|
||||
from ninja import Query
|
||||
from ninja.security import SessionAuth
|
||||
from ninja_extra import ControllerBase, api_controller, paginate, route
|
||||
from ninja_extra.pagination import PageNumberPaginationExtra
|
||||
@@ -10,7 +8,7 @@ from ninja_extra.schemas import PaginatedResponseSchema
|
||||
from api.auth import ApiKeyAuth
|
||||
from api.permissions import CanAccessLookup, HasPerm
|
||||
from club.models import Club, Membership
|
||||
from club.schemas import ClubSchema, SimpleClubSchema
|
||||
from club.schemas import ClubSchema, ClubSearchFilterSchema, SimpleClubSchema
|
||||
|
||||
|
||||
@api_controller("/club")
|
||||
@@ -23,8 +21,12 @@ class ClubController(ControllerBase):
|
||||
url_name="search_club",
|
||||
)
|
||||
@paginate(PageNumberPaginationExtra, page_size=50)
|
||||
def search_club(self, search: Annotated[str, MinLen(1)]):
|
||||
return Club.objects.filter(name__icontains=search).values()
|
||||
def search_club(
|
||||
self,
|
||||
filters: Query[ClubSearchFilterSchema],
|
||||
):
|
||||
clubs = Club.objects.all()
|
||||
return filters.filter(clubs)
|
||||
|
||||
@route.get(
|
||||
"/{int:club_id}",
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
from ninja import ModelSchema
|
||||
from typing import Annotated
|
||||
|
||||
from annotated_types import MinLen
|
||||
from django.db.models import Q
|
||||
from ninja import Field, FilterSchema, ModelSchema
|
||||
|
||||
from club.models import Club, Membership
|
||||
from core.schemas import SimpleUserSchema
|
||||
|
||||
|
||||
class ClubSearchFilterSchema(FilterSchema):
|
||||
search: Annotated[str, MinLen(1)] | None = Field(None, q="name__icontains")
|
||||
is_active: bool | None = None
|
||||
parent_id: int | None = None
|
||||
parent_name: str | None = Field(None, q="parent__name__icontains")
|
||||
exclude_ids: set[int] | None = None
|
||||
|
||||
def filter_exclude_ids(self, value: set[int] | None):
|
||||
if value is None:
|
||||
return Q()
|
||||
return ~Q(id__in=value)
|
||||
|
||||
|
||||
class SimpleClubSchema(ModelSchema):
|
||||
class Meta:
|
||||
model = Club
|
||||
|
||||
44
club/tests/test_api.py
Normal file
44
club/tests/test_api.py
Normal file
@@ -0,0 +1,44 @@
|
||||
from django.test import Client, TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from core.models import User
|
||||
|
||||
|
||||
class TestClubSearch(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
cls.url = reverse("api:search_club")
|
||||
cls.client = Client()
|
||||
cls.user = User.objects.get(username="root")
|
||||
|
||||
def test_inactive_club(self):
|
||||
self.client.force_login(self.user)
|
||||
response = self.client.get(self.url, {"is_active": False})
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
names = [item["name"] for item in data["results"]]
|
||||
assert "AE" not in names
|
||||
assert "Troll Penché" not in names
|
||||
|
||||
def test_excluded_id(self):
|
||||
self.client.force_login(self.user)
|
||||
response = self.client.get(self.url, {"exclude_ids": [1]})
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
names = [item["name"] for item in data["results"]]
|
||||
assert "AE" not in names
|
||||
|
||||
def test_club_search(self):
|
||||
self.client.force_login(self.user)
|
||||
response = self.client.get(self.url, {"search": "AE"})
|
||||
assert response.status_code == 200
|
||||
|
||||
data = response.json()
|
||||
names = [item["name"] for item in data["results"]]
|
||||
assert len(names) > 1
|
||||
|
||||
def test_anonymous_user_unauthorized(self):
|
||||
response = self.client.get(self.url)
|
||||
assert response.status_code == 401
|
||||
Reference in New Issue
Block a user