fix: Suppression d'un PROFILE si aucun PROFILE_ROLE n'y est associé [N3WTS-1]

This commit is contained in:
N3WT DE COMPET
2026-02-14 17:58:47 +01:00
parent 9dff32b388
commit 92c6a31740
4 changed files with 106 additions and 1 deletions

View File

@ -128,7 +128,21 @@ class TeacherDetailView(APIView):
return JsonResponse(teacher_serializer.errors, safe=False)
def delete(self, request, id):
return delete_object(Teacher, id, related_field='profile_role')
# Suppression du Teacher et du ProfileRole associé
teacher = getObject(_objectName=Teacher, _columnName='id', _value=id)
profile_role = getattr(teacher, 'profile_role', None)
profile = getattr(profile_role, 'profile', None) if profile_role else None
# Supprime le Teacher (ce qui supprime le ProfileRole via on_delete=models.CASCADE)
response = delete_object(Teacher, id, related_field='profile_role')
# Si un profile était associé, vérifier s'il reste des ProfileRole
if profile:
from Auth.models import ProfileRole # import local pour éviter les imports circulaires
if not ProfileRole.objects.filter(profile=profile).exists():
profile.delete()
return response
@method_decorator(csrf_protect, name='dispatch')
@method_decorator(ensure_csrf_cookie, name='dispatch')