fix: Edition d'un teacher, champ email désactivé [N3WTS-1]

This commit is contained in:
N3WT DE COMPET
2026-02-15 15:47:51 +01:00
parent 92c6a31740
commit 176edc5c45
3 changed files with 105 additions and 113 deletions

View File

@ -60,6 +60,7 @@ class TeacherSerializer(serializers.ModelSerializer):
profile_role = serializers.PrimaryKeyRelatedField(queryset=ProfileRole.objects.all(), required=False)
profile_role_data = ProfileRoleSerializer(write_only=True, required=False)
associated_profile_email = serializers.SerializerMethodField()
profile = serializers.SerializerMethodField()
class Meta:
model = Teacher
@ -155,6 +156,12 @@ class TeacherSerializer(serializers.ModelSerializer):
return obj.profile_role.role_type
return None
def get_profile(self, obj):
# Retourne l'id du profile associé via profile_role
if obj.profile_role and obj.profile_role.profile:
return obj.profile_role.profile.id
return None
class PlanningSerializer(serializers.ModelSerializer):
class Meta:
model = Planning

View File

@ -118,11 +118,23 @@ class TeacherDetailView(APIView):
return JsonResponse(teacher_serializer.data, safe=False)
def put(self, request, id):
teacher_data=JSONParser().parse(request)
teacher_data = JSONParser().parse(request)
teacher = getObject(_objectName=Teacher, _columnName='id', _value=id)
# Récupérer l'ancien profile avant modification
old_profile_role = getattr(teacher, 'profile_role', None)
old_profile = getattr(old_profile_role, 'profile', None) if old_profile_role else None
teacher_serializer = TeacherSerializer(teacher, data=teacher_data)
if teacher_serializer.is_valid():
teacher_serializer.save()
# Après modification, vérifier si l'ancien profile n'a plus de ProfileRole
if old_profile:
from Auth.models import ProfileRole # import local pour éviter les imports circulaires
if not ProfileRole.objects.filter(profile=old_profile).exists():
old_profile.delete()
return JsonResponse(teacher_serializer.data, safe=False)
return JsonResponse(teacher_serializer.errors, safe=False)