mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
guardian/student + ajout de la possibilité de créer un guardian pour un student + tri chrologique
99 lines
3.7 KiB
Python
99 lines
3.7 KiB
Python
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
|
|
from Auth.models import ProfileRole
|
|
from N3wtSchool import bdd
|
|
|
|
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()
|
|
|
|
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
|
|
)
|