示例#1
0
    def save(self):

        try:
            backgroundimage = self.validated_data['backgroundimage']
            avatarimage = self.validated_data['avatarimage']
            name = self.validated_data['name']
            description = self.validated_data['description']
            community = Communities(
                name=name,
                backgroundimage=backgroundimage,
                avatarimage=avatarimage,
                description=description,
            )

            url = os.path.join(settings.TEMP, str(backgroundimage))
            storage = FileSystemStorage(location=url)

            with storage.open('', 'wb+') as destination:
                for chunk in backgroundimage.chunks():
                    destination.write(chunk)
                destination.close()

            if not is_image_size_valid(url, IMAGE_SIZE_MAX_BYTES):
                os.remove(url)
                raise serializers.ValidationError(
                    {"response": "That image is too large. Images must be less than 3 MB. Try a different image."})

            if not is_image_aspect_ratio_valid(url):
                os.remove(url)
                raise serializers.ValidationError(
                    {"response": "Image height must not exceed image width. Try a different image."})

            os.remove(url)
            community.save()
            return community
        except KeyError:
            raise serializers.ValidationError(
                {"response": "You must have name, backgroundimage, avatarimage and description for community."})
示例#2
0
 def make_dataset():
     img = in_memory_image()
     community = Communities(name="Com")
     community.avatarimage = img
     community.backgroundimage = img
     community.save()
     blog = Blog(title="Blog", community=community)
     blog.image = img
     blog.save()
     return blog, community
示例#3
0
 def make_dataset():
     community = Communities(name="Com")
     community.save()
     blog = Blog(title="Blog", community=community)
     blog.save()
     return blog, community