def test_is_image_aspect_ratio_valid_shouldValidateImageAspectRaio(image_url): image_url = image_url() img = Image.new('RGB', (512, 512)) img.save(image_url) right = is_image_aspect_ratio_valid(image_url) os.remove(image_url) img = img.resize((512, 1024)) img.save(image_url) wrong = is_image_aspect_ratio_valid(image_url) os.remove(image_url) assert right assert not wrong
def validate(self, blog_post): try: title = blog_post['title'] if len(title) < MIN_TITLE_LENGTH: raise serializers.ValidationError({"response": "Enter a title longer than " + str(MIN_TITLE_LENGTH) + " characters."}) body = blog_post['body'] if len(body) < MIN_BODY_LENGTH: raise serializers.ValidationError({"response": "Enter a body longer than " + str(MIN_BODY_LENGTH) + " characters."}) image = blog_post['image'] url = os.path.join(settings.TEMP , str(image)) storage = FileSystemStorage(location=url) with storage.open('', 'wb+') as destination: for chunk in image.chunks(): destination.write(chunk) destination.close() # Check image size 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 2 MB. Try a different image."}) # Check image aspect ratio 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) except KeyError: pass return blog_post
def validate(self, community): try: image = community['backgroundimage'] url = os.path.join(settings.TEMP, str(image)) storage = FileSystemStorage(location=url) with storage.open('', "wb+") as destination: for chunk in image.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) image = community['avatarimage'] url = os.path.join(settings.TEMP, str(image)) storage = FileSystemStorage(location=url) with storage.open('', "wb+") as destination: for chunk in image.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) except KeyError: pass return community
def save(self): try: image = self.validated_data['image'] title = self.validated_data['title'] if len(title) < MIN_TITLE_LENGTH: raise serializers.ValidationError({ "response": "Enter a title longer than " + str(MIN_TITLE_LENGTH) + " characters." }) body = self.validated_data['body'] if len(body) < MIN_BODY_LENGTH: raise serializers.ValidationError({ "response": "Enter a body longer than " + str(MIN_BODY_LENGTH) + " characters." }) blog = Blog( community=self.validated_data['community'], title=title, body=body, image=image, ) url = os.path.join(settings.TEMP, str(image)) storage = FileSystemStorage(location=url) with storage.open('', 'wb+') as destination: for chunk in image.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) blog.save() return blog except KeyError: raise serializers.ValidationError({ "response": "You must have a title, some content, and an image." })
def save(self): try: backgroundimage = self.validated_data['backgroundimage'] avatarimage = self.validated_data['avatarimage'] name = self.validated_data['name'] description = self.validated_data['description'] exposeunit = Exposeunits( 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 differnet 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) exposeunit.save() return exposeunit except KeyError: raise serializers.ValidationError({ "response": "You must have name, backgroundimage, avatarimage and description for the exposeunit." })
def image_validator(image, image_max_size=1024 * 1024 * 3): # In case the image already exists (Example : /communitie/com-abc.png ) then we need image name only as # as we make use of temporary storage img = str(image).rsplit('/')[-1] url = os.path.join(settings.MEDIA_ROOT, img) storage = FileSystemStorage(location=url) with storage.open('', 'wb+') as destination: for chunk in image.chunks(): destination.write(chunk) destination.close() if not is_image_size_valid(url, image_max_size): os.remove(url) raise ValidationError( "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 ValidationError( "Image height must not exceed image width. Try a different image.") os.remove(url)