Ejemplo n.º 1
0
def test_is_image_size_valid_shouldValidateImageSize(image_url):
    image_url = image_url()
    img = Image.new('RGB', (512, 512))
    img.save(image_url)
    image_size = os.path.getsize(image_url)
    max_size = image_size + 1
    rightsize = is_image_size_valid(image_url, max_size)
    max_size = image_size - 1
    wrongsize = is_image_size_valid(image_url, max_size)
    os.remove(image_url)
    assert rightsize
    assert not wrongsize
Ejemplo n.º 2
0
	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
Ejemplo n.º 3
0
    def validate(self, exposeunit):
        try:
            image = exposeunit['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."
                })

            os.remove(url)

            image = exposeunit['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 that 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 exposeunit
Ejemplo n.º 4
0
    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."
            })
Ejemplo n.º 5
0
    def save(self):
        try:
            content_file_pdf = self.validated_data['content_file_pdf']
            content_title = self.validated_data['content_title']
            if len(content_title) < MIN_TITLE_LENGTH:
                raise serializers.ValidationError({
                    "response":
                    "Enter a title longer than " + str(MIN_TITLE_LENGTH) +
                    " characters."
                })
            content_body = self.validated_data['content_body']
            if len(content_body) < MIN_BODY_LENGTH:
                raise serializers.ValidationError({
                    "response":
                    "Enter a body longer than " + str(MIN_BODY_LENGTH) +
                    " characters."
                })
            content_summary = self.validated_data['content_summary']
            content_category = self.validated_data['content_category']
            content = CMSAuthorContent(author=self.validated_data['author'],
                                       content_title=content_title,
                                       content_body=content_body,
                                       content_summary=content_summary,
                                       content_file_pdf=content_file_pdf,
                                       content_category=content_category)

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

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

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

# 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)
            content.save()
            return content
        except KeyError:
            raise serializers.ValidationError({
                "response":
                "You must have a title, some content, and an file."
            })
Ejemplo n.º 6
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']
            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."
            })
Ejemplo n.º 7
0
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)