Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change size arg from str to int and choose thumbnail size only from predefined
oldPadavand80fa2c47e2a
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.pyindex 745a50f4d9..1b8b289d0c 100644--- a/saleor/graphql/product/types.py+++ b/saleor/graphql/product/types.py@@ -3,7 +3,8 @@ from graphene import relayfrom graphene_django.filter import DjangoFilterConnectionFieldfrom ...product import models-from ...product.templatetags.product_images import product_first_image+from ...product.templatetags.product_images import (+ get_thumbnail, product_first_image)from ...product.utils import products_with_detailsfrom ...product.utils.availability import get_availabilityfrom ...product.utils.costs import (@@ -130,8 +131,8 @@ class Product(CountableDjangoObjectType):thumbnail_url = graphene.String(description='The URL of a main thumbnail for a product.',size=graphene.Argument(- graphene.String,- description='Size of a thumbnail, for example 255x255.'))+ graphene.Int,+ description='Size of thumbnail'))availability = graphene.Field(ProductAvailability,description="""Informs about product's availability in the storefront,@@ -154,7 +155,7 @@ class Product(CountableDjangoObjectType):def resolve_thumbnail_url(self, info, *, size=None):if not size:- size = '255x255'+ size = 255return product_first_image(self, size)def resolve_url(self, info):@@ -264,8 +265,8 @@ class ProductImage(CountableDjangoObjectType):url = graphene.String(required=True,description='',- size=graphene.String(- description='Size of an image, for example 255x255.'))+ size=graphene.Int(+ description='Size of image'))class Meta:description = 'Represents a product image.'@@ -275,5 +276,5 @@ class ProductImage(CountableDjangoObjectType):def resolve_url(self, info, *, size=None):if size:- return self.image.crop[size].url+ return get_thumbnail(self.image, size, 'crop')return self.image.urldiff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.pyindex 3145c92549..35b7d773d7 100644--- a/saleor/product/templatetags/product_images.py+++ b/saleor/product/templatetags/product_images.py@@ -47,24 +47,49 @@ def choose_placeholder(size=''):@register.simple_tag()def get_thumbnail(instance, size, method='crop'):- size_name = '%s__%s' % (method, size)+ size_name = '%s__%sx%s' % (method, size, size)on_demand = settings.VERSATILEIMAGEFIELD_SETTINGS['create_images_on_demand']if instance:- if size_name not in AVAILABLE_SIZES and not on_demand:- msg = (- "Thumbnail size %s is not defined in settings "- "and it won't be generated automatically" % size_name)- warnings.warn(msg)+ # select equal size if exists, otherwise select closest larger size+ # if not more than 2 times larger, otherwise select closest smaller+ # size+ if size_name in AVAILABLE_SIZES or on_demand:+ used_size = '%sx%s' % (size, size)+ else:+ closest_larger = float('inf')+ closest_smaller = 0+ for available_size in AVAILABLE_SIZES:+ available_method, size_str = available_size.split('__')+ width, height = [int(s) for s in size_str.split('x')]+ avail_min_dim = min(width, height)++ if available_method != method:+ continue+ if size < avail_min_dim <= size * 2:+ closest_larger = min(avail_min_dim, closest_larger)+ if avail_min_dim <= size:+ closest_smaller = max(avail_min_dim, closest_smaller)++ if closest_larger != float('inf'):+ used_size = '%sx%s' % (closest_larger, closest_larger)+ elif closest_smaller:+ used_size = '%sx%s' % (closest_smaller, closest_smaller)+ else:+ msg = (+ "Thumbnail size %s is not defined in settings "+ "and it won't be generated automatically" % size_name)+ warnings.warn(msg)+ used_size = '%sx%s' % (size, size)try:- thumbnail = getattr(instance, method)[size]+ thumbnail = getattr(instance, method)[used_size]except Exception:logger.exception('Thumbnail fetch failed',extra={'instance': instance, 'size': size})else:return thumbnail.url- return static(choose_placeholder(size))+ return static(choose_placeholder('%sx%s' % (size, size)))@register.simple_tag()