mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
feat: Ajout d'une fonction de dissociation entre un responsable et un
élève
This commit is contained in:
@ -2,7 +2,7 @@ from .register_form_views import RegisterFormView, RegisterFormWithIdView, send,
|
||||
from .registration_file_views import RegistrationTemplateMasterView, RegistrationTemplateMasterSimpleView, RegistrationTemplateView, RegistrationTemplateSimpleView
|
||||
from .registration_file_group_views import RegistrationFileGroupView, RegistrationFileGroupSimpleView, get_registration_files_by_group
|
||||
from .student_views import StudentView, StudentListView, ChildrenListView
|
||||
from .guardian_views import GuardianView
|
||||
from .guardian_views import GuardianView, DissociateGuardianView
|
||||
|
||||
__all__ = [
|
||||
'RegisterFormView',
|
||||
@ -22,4 +22,5 @@ __all__ = [
|
||||
'StudentListView',
|
||||
'ChildrenListView',
|
||||
'GuardianView',
|
||||
'DissociateGuardianView'
|
||||
]
|
||||
|
||||
@ -1,9 +1,10 @@
|
||||
from django.http.response import JsonResponse
|
||||
from rest_framework import status
|
||||
from rest_framework.views import APIView
|
||||
from drf_yasg.utils import swagger_auto_schema
|
||||
from drf_yasg import openapi
|
||||
|
||||
from Subscriptions.models import Guardian
|
||||
from Subscriptions.models import Guardian, Student
|
||||
from N3wtSchool import bdd
|
||||
|
||||
class GuardianView(APIView):
|
||||
@ -32,3 +33,43 @@ class GuardianView(APIView):
|
||||
def get(self, request):
|
||||
lastGuardian = bdd.getLastId(Guardian)
|
||||
return JsonResponse({"lastid":lastGuardian}, safe=False)
|
||||
|
||||
class DissociateGuardianView(APIView):
|
||||
"""
|
||||
Vue pour dissocier un Guardian d'un Student.
|
||||
"""
|
||||
|
||||
def put(self, request, student_id, guardian_id):
|
||||
try:
|
||||
# Récupérer l'étudiant
|
||||
student = Student.objects.get(id=student_id)
|
||||
|
||||
# Récupérer le guardian
|
||||
guardian = Guardian.objects.get(id=guardian_id)
|
||||
|
||||
# Supprimer la relation entre le student et le guardian
|
||||
student.guardians.remove(guardian)
|
||||
|
||||
# Vérifier si le guardian n'est plus associé à aucun élève
|
||||
if guardian.student_set.count() == 0: # Utilise la relation ManyToMany inverse
|
||||
guardian.delete()
|
||||
|
||||
return JsonResponse(
|
||||
{"message": f"Le guardian {guardian.last_name} {guardian.first_name} a été dissocié de l'étudiant {student.last_name} {student.first_name}."},
|
||||
status=status.HTTP_200_OK
|
||||
)
|
||||
except Student.DoesNotExist:
|
||||
return JsonResponse(
|
||||
{"error": "Étudiant non trouvé."},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except Guardian.DoesNotExist:
|
||||
return JsonResponse(
|
||||
{"error": "Guardian non trouvé."},
|
||||
status=status.HTTP_404_NOT_FOUND
|
||||
)
|
||||
except Exception as e:
|
||||
return JsonResponse(
|
||||
{"error": f"Une erreur est survenue : {str(e)}"},
|
||||
status=status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user