mirror of
https://git.v0id.ovh/n3wt-innov/n3wt-school.git
synced 2026-01-29 07:53:23 +00:00
feat: Gestion des rattachements de Guardian à des RF déjà existants
This commit is contained in:
@ -5,12 +5,17 @@ from Establishment.models import Establishment
|
||||
class ProfileSerializer(serializers.ModelSerializer):
|
||||
id = serializers.IntegerField(required=False)
|
||||
password = serializers.CharField(write_only=True)
|
||||
roles = serializers.SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = Profile
|
||||
fields = ['id', 'password', 'email', 'code', 'datePeremption', 'username']
|
||||
fields = ['id', 'password', 'email', 'code', 'datePeremption', 'username', 'roles']
|
||||
extra_kwargs = {'password': {'write_only': True}}
|
||||
|
||||
def get_roles(self, obj):
|
||||
roles = ProfileRole.objects.filter(profile=obj)
|
||||
return [{'role_type': role.role_type, 'establishment': role.establishment.id, 'is_active': role.is_active} for role in roles]
|
||||
|
||||
def create(self, validated_data):
|
||||
user = Profile(
|
||||
username=validated_data['username'],
|
||||
@ -37,70 +42,44 @@ class ProfileSerializer(serializers.ModelSerializer):
|
||||
def to_representation(self, instance):
|
||||
ret = super().to_representation(instance)
|
||||
ret['password'] = '********'
|
||||
ret['roles'] = self.get_roles(instance)
|
||||
return ret
|
||||
|
||||
class ProfileRoleSerializer(serializers.ModelSerializer):
|
||||
profile = ProfileSerializer()
|
||||
establishment = serializers.PrimaryKeyRelatedField(queryset=Establishment.objects.all())
|
||||
profile = serializers.PrimaryKeyRelatedField(queryset=Profile.objects.all(), required=False)
|
||||
profile_data = ProfileSerializer(write_only=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = ProfileRole
|
||||
fields = ['role_type', 'establishment', 'is_active', 'profile']
|
||||
fields = ['role_type', 'establishment', 'is_active', 'profile', 'profile_data']
|
||||
|
||||
def create(self, validated_data):
|
||||
profile_data = validated_data.pop('profile')
|
||||
profile_serializer = ProfileSerializer(data=profile_data)
|
||||
profile_serializer.is_valid(raise_exception=True)
|
||||
profile = profile_serializer.save()
|
||||
profile_data = validated_data.pop('profile_data', None)
|
||||
profile = validated_data.pop('profile', None)
|
||||
|
||||
if profile_data:
|
||||
profile_serializer = ProfileSerializer(data=profile_data)
|
||||
profile_serializer.is_valid(raise_exception=True)
|
||||
profile = profile_serializer.save()
|
||||
elif profile:
|
||||
profile = Profile.objects.get(id=profile.id)
|
||||
|
||||
profile_role = ProfileRole.objects.create(profile=profile, **validated_data)
|
||||
return profile_role
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
profile_data = validated_data.pop('profile')
|
||||
profile_serializer = ProfileSerializer(instance.profile, data=profile_data)
|
||||
profile_serializer.is_valid(raise_exception=True)
|
||||
profile = profile_serializer.save()
|
||||
profile_data = validated_data.pop('profile_data', None)
|
||||
profile = validated_data.pop('profile', None)
|
||||
|
||||
if profile_data:
|
||||
profile_serializer = ProfileSerializer(instance.profile, data=profile_data)
|
||||
profile_serializer.is_valid(raise_exception=True)
|
||||
profile = profile_serializer.save()
|
||||
elif profile:
|
||||
profile = Profile.objects.get(id=profile.id)
|
||||
|
||||
instance.role_type = validated_data.get('role_type', instance.role_type)
|
||||
instance.establishment_id = validated_data.get('establishment', instance.establishment.id)
|
||||
instance.is_active = validated_data.get('is_active', instance.is_active)
|
||||
instance.save()
|
||||
return instance
|
||||
|
||||
class ProfilUpdateSerializer(serializers.ModelSerializer):
|
||||
roles = ProfileRoleSerializer(many=True, required=False)
|
||||
|
||||
class Meta:
|
||||
model = Profile
|
||||
fields = ['id', 'password', 'email', 'code', 'datePeremption', 'username', 'roles']
|
||||
extra_kwargs = {
|
||||
'password': {'write_only': True, 'required': False}
|
||||
}
|
||||
|
||||
def update(self, instance, validated_data):
|
||||
roles_data = validated_data.pop('roles', [])
|
||||
password = validated_data.pop('password', None)
|
||||
instance = super().update(instance, validated_data)
|
||||
|
||||
if password:
|
||||
instance.set_password(password)
|
||||
|
||||
instance.full_clean()
|
||||
instance.save()
|
||||
|
||||
for role_data in roles_data:
|
||||
ProfileRole.objects.update_or_create(
|
||||
profile=instance,
|
||||
establishment_id=role_data.get('establishment_id'),
|
||||
defaults={
|
||||
'role_type': role_data.get('role_type'),
|
||||
'is_active': role_data.get('is_active', True)
|
||||
}
|
||||
)
|
||||
|
||||
return instance
|
||||
|
||||
def to_representation(self, instance):
|
||||
ret = super().to_representation(instance)
|
||||
ret['password'] = '********'
|
||||
return ret
|
||||
return instance
|
||||
Reference in New Issue
Block a user