mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
feat: Mise à jour des Teacher
This commit is contained in:
@ -33,34 +33,49 @@ class TeacherSerializer(serializers.ModelSerializer):
|
||||
specialities = serializers.PrimaryKeyRelatedField(queryset=Speciality.objects.all(), many=True, required=False)
|
||||
specialities_details = serializers.SerializerMethodField()
|
||||
updated_date_formatted = serializers.SerializerMethodField()
|
||||
role_type = serializers.SerializerMethodField()
|
||||
associated_profile_email = serializers.SerializerMethodField()
|
||||
role_type_display = serializers.SerializerMethodField()
|
||||
role_type = serializers.IntegerField(write_only=True)
|
||||
associated_profile_email = serializers.EmailField(write_only=True)
|
||||
profile_role = serializers.PrimaryKeyRelatedField(queryset=ProfileRole.objects.all(), required=False)
|
||||
profile_role_data = ProfileRoleSerializer(write_only=True, required=False)
|
||||
associated_profile_email_display = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Teacher
|
||||
fields = '__all__'
|
||||
|
||||
def create_or_update_profile_role(self, profile, associated_profile_email, establishment_id, role_type):
|
||||
# Mettre à jour l'email du profil si nécessaire
|
||||
if profile.email != associated_profile_email:
|
||||
profile.email = associated_profile_email
|
||||
profile.username = associated_profile_email
|
||||
profile.save()
|
||||
|
||||
profile_role, created = ProfileRole.objects.update_or_create(
|
||||
profile=profile,
|
||||
establishment_id=establishment_id,
|
||||
defaults={'role_type': role_type, 'is_active': True}
|
||||
)
|
||||
|
||||
if not created:
|
||||
profile_role.role_type = role_type
|
||||
profile_role.establishment_id = establishment_id
|
||||
profile_role.save()
|
||||
|
||||
return profile_role
|
||||
|
||||
def create(self, validated_data):
|
||||
specialities_data = validated_data.pop('specialities', None)
|
||||
profile_role_data = validated_data.pop('profile_role_data', None)
|
||||
profile_role = validated_data.pop('profile_role', None)
|
||||
associated_profile_email = validated_data.pop('associated_profile_email')
|
||||
establishment_id = validated_data.pop('establishment')
|
||||
role_type = validated_data.pop('role_type')
|
||||
|
||||
if profile_role_data:
|
||||
establishment_id = profile_role_data.pop('establishment').id
|
||||
profile_id = profile_role_data.pop('profile').id
|
||||
profile_role_data['establishment'] = establishment_id
|
||||
profile_role_data['profile'] = profile_id
|
||||
profile, created = Profile.objects.get_or_create(
|
||||
email=associated_profile_email,
|
||||
defaults={'username': associated_profile_email}
|
||||
)
|
||||
|
||||
# Créer l'instance de ProfileRole
|
||||
profile_role_serializer = ProfileRoleSerializer(data=profile_role_data)
|
||||
profile_role_serializer.is_valid(raise_exception=True)
|
||||
profile_role = profile_role_serializer.save()
|
||||
elif profile_role:
|
||||
profile_role = ProfileRole.objects.get(id=profile_role.id)
|
||||
profile_role = self.create_or_update_profile_role(profile, associated_profile_email, establishment_id, role_type)
|
||||
|
||||
# Créer l'enseignant avec l'instance de ProfileRole
|
||||
teacher = Teacher.objects.create(profile_role=profile_role, **validated_data)
|
||||
if specialities_data:
|
||||
teacher.specialities.set(specialities_data)
|
||||
@ -69,21 +84,17 @@ class TeacherSerializer(serializers.ModelSerializer):
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
specialities_data = validated_data.pop('specialities', [])
|
||||
profile_role_data = validated_data.pop('profile_role_data', None)
|
||||
profile_role = validated_data.pop('profile_role', None)
|
||||
associated_profile_email = validated_data.pop('associated_profile_email', instance.profile_role.profile.email)
|
||||
establishment_id = validated_data.get('establishment', instance.profile_role.establishment.id)
|
||||
role_type = validated_data.get('role_type', instance.profile_role.role_type)
|
||||
|
||||
if profile_role_data:
|
||||
establishment_id = profile_role_data.pop('establishment').id
|
||||
profile_role_data['establishment'] = establishment_id
|
||||
profile_role_serializer = ProfileRoleSerializer(instance.profile_role, data=profile_role_data)
|
||||
profile_role_serializer.is_valid(raise_exception=True)
|
||||
profile_role_serializer.save()
|
||||
elif profile_role:
|
||||
instance.profile_role = ProfileRole.objects.get(id=profile_role.id)
|
||||
profile = instance.profile_role.profile
|
||||
|
||||
profile_role = self.create_or_update_profile_role(profile, associated_profile_email, establishment_id, role_type)
|
||||
instance.profile_role = profile_role
|
||||
|
||||
instance.last_name = validated_data.get('last_name', instance.last_name)
|
||||
instance.first_name = validated_data.get('first_name', instance.first_name)
|
||||
instance.email = validated_data.get('email', instance.email)
|
||||
instance.save()
|
||||
if specialities_data:
|
||||
instance.specialities.set(specialities_data)
|
||||
@ -95,12 +106,6 @@ class TeacherSerializer(serializers.ModelSerializer):
|
||||
local_time = utc_time.astimezone(local_tz)
|
||||
return local_time.strftime("%d-%m-%Y %H:%M")
|
||||
|
||||
def get_role_type(self, obj):
|
||||
profile_role = obj.profile_role
|
||||
if profile_role:
|
||||
return {'role_type': profile_role.role_type, 'establishment': profile_role.establishment.name}
|
||||
return None
|
||||
|
||||
def get_specialities_details(self, obj):
|
||||
return [{'id': speciality.id, 'name': speciality.name, 'color_code': speciality.color_code} for speciality in obj.specialities.all()]
|
||||
|
||||
@ -109,6 +114,14 @@ class TeacherSerializer(serializers.ModelSerializer):
|
||||
return obj.profile_role.profile.email
|
||||
return None
|
||||
|
||||
def get_role_type_display(self, obj):
|
||||
if obj.profile_role:
|
||||
return obj.profile_role.role_type
|
||||
return None
|
||||
|
||||
def get_associated_profile_email_display(self, obj):
|
||||
return self.get_associated_profile_email(obj)
|
||||
|
||||
class PlanningSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Planning
|
||||
|
||||
Reference in New Issue
Block a user