Beispiel #1
0
    def create(self, validated_data):
        # create an image object
        image = Image(**validated_data)

        if validated_data.get("base_64", None):
            file_type = get_file_type(validated_data["base_64"])
            if not file_type:
                raise serializers.ValidationError(
                    {"error": "Image cannot be decoded"})
            else:
                image.extension = file_type
        if validated_data.get("remote_location", None):
            image_check = valid_remote_image(validated_data["remote_location"])
            if not image_check:
                raise serializers.ValidationError(
                    {"error": "Image cannot be downloaded"})
            else:
                success, file_type, base_64 = image_check
                image.base_64 = base_64
                image.extension = file_type
                validated_data["base_64"] = image.base_64

        validated_data["extension"] = image.extension

        image.save()
        # return the correct uuid for the API response
        validated_data["id"] = image.id
        f_data = ContentFile(base64.b64decode(image.base_64))
        image.raw_file.save(f"file_name.{image.extension}", f_data)
        validated_data["raw_file"] = image.raw_file
        convert.delay(image.id)
        return Image(**validated_data)