optimize fix

This commit is contained in:
a.gorbunov 2020-01-20 10:25:09 +00:00
parent 1bc7a9f13b
commit bb8accefbf

View File

@ -11,19 +11,26 @@ class Command(BaseCommand):
SORL_THUMBNAIL_ALIAS = 'collection_image' SORL_THUMBNAIL_ALIAS = 'collection_image'
def handle(self, *args, **options): def handle(self, *args, **options):
max_size = 1048576
with transaction.atomic(): with transaction.atomic():
for collection in Collection.objects.all(): for collection in Collection.objects.all():
if not image_url_valid(collection.image_url): if not image_url_valid(collection.image_url):
continue continue
_, width, height = get_image_meta_by_url(collection.image_url) size, width, height = get_image_meta_by_url(collection.image_url)
sorl_settings = settings.SORL_THUMBNAIL_ALIASES[self.SORL_THUMBNAIL_ALIAS]
sorl_width_height = sorl_settings['geometry_string'].split('x')
if int(sorl_width_height[0]) > width or int(sorl_width_height[1]) > height: if size < max_size:
collection.image_url = get_thumbnail( self.stdout.write(self.style.SUCCESS(f'No need to compress images size is {size / (2 ** 20)}Mb\n'))
file_=collection.image_url, continue
**settings.SORL_THUMBNAIL_ALIASES[self.SORL_THUMBNAIL_ALIAS]
).url percents = round(max_size / (size * 0.01))
width = round(width * percents / 100)
height = round(height * percents / 100)
collection.image_url = get_thumbnail(
file_=collection.image_url,
geometry_string=f'{width}x{height}',
upscale=False
).url
collection.save() collection.save()