26 jan

mail@pastecode.io avatar
unknown
python
2 years ago
1.4 kB
4
Indexable
Never
class UserCreateSerializer(serializers.ModelSerializer):
    photo = PhotoHybridImageField(required=False)
    phone = serializers.CharField(validators=[PhoneRegexValidator()])
    mobile = serializers.CharField(validators=[PhoneRegexValidator()])

    class Meta:
        model = Profile
        fields = ["id", "surname", "salutation", "sex", "phone", "mobile", "photo"]
        extra_kwargs = {}

    def to_representation(self, instance):
        response = super().to_representation(instance)
        response["user"] = UserSerializer(instance.user).data
        return response

    def validate(self, data):
        validated_data = super().validate(data)
        for key, value in validated_data.items():
            if not value:
                raise serializers.ValidationError({"error": f"{key} should not be blank"})

        if validated_data.get("hospital_profile") and validated_data.get("careteam"):
            careteam_hospitals = set([careteam.hospital_profile for careteam in validated_data.get("careteam")])
            if set(validated_data.get("hospital_profile")) != careteam_hospitals:
                raise serializers.ValidationError({"error": f"Atleast one careteam must be selected from"
                                                            f" each hospital"})

        return validated_data
        

 return Response({"message": "User Created", "data": serializer.data}, status=status.HTTP_201_CREATED)
git