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) # Supprimer la relation entre le student et le guardian student.guardians.remove(guardian) if guardian.profile_role: guardian.profile_role.save() 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() # Récupérer le RegistrationForm associé au Student registerForm = bdd.getObject(RegistrationForm, "student__id", student_id) if registerForm: # Réinitialiser le statut en "Créé" registerForm.status = RegistrationForm.RegistrationFormStatus.RF_CREATED registerForm.save() # Supprimer le fichier et le dossier associés util.delete_registration_files(registerForm) 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 )