mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-28 23:43:22 +00:00
fix: Suppression d'un profil uniquement s'il ne contient aucun guardian
rattaché à un élève qui n'en a pas d'autre
This commit is contained in:
@ -22,7 +22,7 @@ from .models import Profile, ProfileRole
|
||||
from rest_framework.decorators import action, api_view
|
||||
|
||||
from Auth.serializers import ProfileSerializer, ProfileRoleSerializer
|
||||
from Subscriptions.models import RegistrationForm
|
||||
from Subscriptions.models import RegistrationForm, Guardian
|
||||
import Subscriptions.mailManager as mailer
|
||||
import Subscriptions.util as util
|
||||
import logging
|
||||
@ -577,12 +577,41 @@ class ProfileRoleSimpleView(APIView):
|
||||
responses={200: 'Suppression réussie'}
|
||||
)
|
||||
def delete(self, request, id):
|
||||
profile_role = ProfileRole.objects.get(id=id)
|
||||
profile = profile_role.profile
|
||||
profile_role.delete()
|
||||
try:
|
||||
# Récupérer le ProfileRole
|
||||
profile_role = ProfileRole.objects.get(id=id)
|
||||
profile = profile_role.profile
|
||||
|
||||
# Vérifier si le profil n'a plus de rôles associés
|
||||
if not ProfileRole.objects.filter(profile=profile).exists():
|
||||
profile.delete()
|
||||
# Vérifier si le ProfileRole est de type PARENT
|
||||
if profile_role.role_type == ProfileRole.RoleType.PROFIL_PARENT:
|
||||
guardian = Guardian.objects.filter(profile_role=profile_role).first()
|
||||
if guardian:
|
||||
# Vérifier si ce Guardian est rattaché à des élèves
|
||||
for student in guardian.student_set.all():
|
||||
# Vérifier si l'élève n'a pas d'autres Guardians
|
||||
other_guardians = student.guardians.exclude(id=guardian.id)
|
||||
if not other_guardians.exists():
|
||||
return JsonResponse(
|
||||
{"error": f"Impossible de supprimer ce profil car l'élève {student.first_name} {student.last_name} n'aura plus de responsable légal."},
|
||||
status=status.HTTP_400_BAD_REQUEST
|
||||
)
|
||||
|
||||
return JsonResponse({'message': 'Suppression réussie'}, safe=False)
|
||||
# Supprimer le ProfileRole
|
||||
profile_role.delete()
|
||||
|
||||
# Vérifier si le profil n'a plus de rôles associés
|
||||
if not ProfileRole.objects.filter(profile=profile).exists():
|
||||
profile.delete()
|
||||
|
||||
return JsonResponse({'message': 'Suppression réussie'}, safe=False)
|
||||
|
||||
except ProfileRole.DoesNotExist:
|
||||
return JsonResponse(
|
||||
{"error": "ProfileRole 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