From 9ce5f89d74bc47a558f5550a73d3070143345714 Mon Sep 17 00:00:00 2001 From: Kuroshini Date: Mon, 20 Jan 2020 14:18:56 +0300 Subject: [PATCH] try to fix issue w/ news images optimization --- .../commands/news_optimize_images.py | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/apps/news/management/commands/news_optimize_images.py b/apps/news/management/commands/news_optimize_images.py index 440afbc2..011bf6ae 100644 --- a/apps/news/management/commands/news_optimize_images.py +++ b/apps/news/management/commands/news_optimize_images.py @@ -28,29 +28,30 @@ class Command(BaseCommand): def optimize(self, text, max_size, max_quality): """optimize news images""" - for image in get_url_images_in_text(text): - try: - size, width, height = get_image_meta_by_url(image) - except IOError as ie: - self.stdout.write(self.style.NOTICE(f'{ie}\n')) - continue + if isinstance(text, str): + for image in get_url_images_in_text(text): + try: + size, width, height = get_image_meta_by_url(image) + except IOError as ie: + self.stdout.write(self.style.NOTICE(f'{ie}\n')) + continue - if size < max_size: - self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2**20)}Mb\n')) - continue + if size < max_size: + self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2**20)}Mb\n')) + continue - percents = round(max_size / (size * 0.01)) - width = round(width * percents / 100) - height = round(height * percents / 100) - optimized_image = get_thumbnail( - file_=image, - geometry_string=f'{width}x{height}', - upscale=False, - quality=max_quality - ).url - text = text.replace(image, optimized_image) - self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n' - f'Quality [{percents}%]\n')) + percents = round(max_size / (size * 0.01)) + width = round(width * percents / 100) + height = round(height * percents / 100) + optimized_image = get_thumbnail( + file_=image, + geometry_string=f'{width}x{height}', + upscale=False, + quality=max_quality + ).url + text = text.replace(image, optimized_image) + self.stdout.write(self.style.SUCCESS(f'Optimized {image} -> {optimized_image}\n' + f'Quality [{percents}%]\n')) return text