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, Student, RegistrationForm from Auth.models import ProfileRole from N3wtSchool import bdd import Subscriptions.util as util class GuardianView(APIView): """ Gestion des responsables légaux. """ @swagger_auto_schema( operation_description="Récupère le dernier ID de responsable légal créé", operation_summary="Récupèrer le dernier ID de responsable légal créé", responses={ 200: openapi.Response( description="Dernier ID du responsable légal", schema=openapi.Schema( type=openapi.TYPE_OBJECT, properties={ 'lastid': openapi.Schema( type=openapi.TYPE_INTEGER, description="Dernier ID créé" ) } ) ) } ) 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) # Vérifier s'il y a d'autres guardians associés au student other_guardians = student.guardians.exclude(id=guardian_id) if not other_guardians.exists(): return JsonResponse( {"error": "Impossible de dissocier ce responsable car il n'en existe aucun autre rattaché à l'élève."}, status=status.HTTP_400_BAD_REQUEST ) # Supprimer la relation entre le student et le guardian student.guardians.remove(guardian) # Mettre à jour le responsable principal (par exemple, le premier autre guardian) new_main_guardian = other_guardians.first() if new_main_guardian: # Logique pour définir le nouveau responsable principal print(f"Le guardian {new_main_guardian} devient le responsable principal.") isGuardianDeleted = False # Vérifier si le guardian n'est plus associé à aucun élève if guardian.student_set.count() == 0: # Utilise la relation ManyToMany inverse print(f'Le guardian {guardian} n\'est plus rattaché à aucun élève : on le supprime') isGuardianDeleted = True # Vérifier si le guardian a un ProfileRole associé if guardian.profile_role: print(f'Suppression du ProfileRole associé au guardian {guardian}') guardian.profile_role.delete() # Vérifier si le Profile n'a plus de ProfileRole associés profile = guardian.profile_role.profile if not ProfileRole.objects.filter(profile=profile).exists(): print(f'Le profile {profile} n\'a plus de rôle associé : on le supprime') profile.delete() # Supprimer le guardian 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}.", "isGuardianDeleted": isGuardianDeleted }, 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 )