feat: Champ de recherche de l'élève [#16]

This commit is contained in:
N3WT DE COMPET
2025-05-20 20:22:58 +02:00
parent 56c223f3cc
commit eb7805e54e
7 changed files with 181 additions and 6 deletions

View File

@ -10,7 +10,7 @@ from .registration_file_views import (
RegistrationParentFileTemplateView
)
from .registration_file_group_views import RegistrationFileGroupView, RegistrationFileGroupSimpleView, get_registration_files_by_group
from .student_views import StudentView, StudentListView, ChildrenListView
from .student_views import StudentView, StudentListView, ChildrenListView, search_students
from .guardian_views import GuardianView, DissociateGuardianView
from .absences_views import AbsenceManagementDetailView, AbsenceManagementListCreateView
from .student_competencies_views import StudentCompetencyListCreateView, StudentCompetencySimpleView
@ -42,5 +42,6 @@ __all__ = [
'AbsenceManagementDetailView',
'AbsenceManagementListCreateView',
'StudentCompetencyListCreateView',
'StudentCompetencySimpleView'
'StudentCompetencySimpleView',
'search_students'
]

View File

@ -3,6 +3,7 @@ from rest_framework.views import APIView
from rest_framework import status
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from django.db.models import Q
from Subscriptions.serializers import StudentByRFCreationSerializer, RegistrationFormByParentSerializer, StudentSerializer
from Subscriptions.models import Student, RegistrationForm
@ -115,3 +116,37 @@ class ChildrenListView(APIView):
).distinct()
students_serializer = RegistrationFormByParentSerializer(students, many=True)
return JsonResponse(students_serializer.data, safe=False)
def search_students(request):
"""
API pour rechercher des étudiants en fonction d'un terme de recherche (nom/prénom) et d'un établissement.
"""
query = request.GET.get('q', '').strip()
establishment_id = request.GET.get('establishment_id', None)
if not query:
return JsonResponse([], safe=False)
if not establishment_id:
return JsonResponse({'error': 'establishment_id est requis'}, safe=False, status=status.HTTP_400_BAD_REQUEST)
# Recherche sur Student (nom ou prénom) et filtrage par établissement via RegistrationForm
students = Student.objects.filter(
Q(last_name__icontains=query) | Q(first_name__icontains=query),
registrationform__establishment_id=establishment_id
).distinct()
# Sérialisation simple (adapte selon ton besoin)
results = [
{
'id': student.id,
'first_name': student.first_name,
'last_name': student.last_name,
'level': getattr(student.level, 'name', ''),
'associated_class_name': student.associated_class.atmosphere_name if student.associated_class else '',
'photo': student.photo.url if student.photo else None,
}
for student in students
]
return JsonResponse(results, safe=False)