mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
feat: Champ de recherche de l'élève [#16]
This commit is contained in:
@ -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)
|
||||
Reference in New Issue
Block a user