Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix codeclimate warning and add test
oldPadavan2f3fef734d1f
diff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.pyindex c5cb0fecab..c9c6c43a2a 100644--- a/saleor/product/templatetags/product_images.py+++ b/saleor/product/templatetags/product_images.py@@ -45,6 +45,32 @@ def choose_placeholder(size=''):return placeholder+def get_thumbnail_size(size, method):+ """ Return closest larger size if not more than 2 times larger, otherwise+ return closest smaller size+ """+ closest_larger = float('inf')+ closest_smaller = 0+ for available_size in AVAILABLE_SIZES:+ available_method, avail_size_str = available_size.split('__')+ width, height = [int(s) for s in avail_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'):+ return '%sx%s' % (closest_larger, closest_larger)+ elif closest_smaller:+ return'%sx%s' % (closest_smaller, closest_smaller)+ else:+ return None++@register.simple_tag()def get_thumbnail(instance, size, method='crop'):size_str = '%sx%s' % (size, size)@@ -52,36 +78,14 @@ def get_thumbnail(instance, size, method='crop'):on_demand = settings.VERSATILEIMAGEFIELD_SETTINGS['create_images_on_demand']if instance:- # 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 = size_str- else:- closest_larger = float('inf')- closest_smaller = 0- for available_size in AVAILABLE_SIZES:- available_method, avail_size_str = available_size.split('__')- width, height = [int(s) for s in avail_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:+ used_size = size_str+ if size_name not in AVAILABLE_SIZES and not on_demand:+ used_size = get_thumbnail_size(size, method)+ if not used_size:msg = ("Thumbnail size %s is not defined in settings ""and it won't be generated automatically" % size_name)warnings.warn(msg)- used_size = size_strtry:thumbnail = getattr(instance, method)[used_size]except Exception:diff --git a/tests/test_product_tags.py b/tests/test_product_tags.pyindex e93c746544..2692988789 100644--- a/tests/test_product_tags.py+++ b/tests/test_product_tags.py@@ -31,7 +31,9 @@ def test_get_thumbnail_no_instance(monkeypatch):@patch('saleor.product.templatetags.product_images.AVAILABLE_SIZES',- {'crop__10x10', 'crop__100x100', 'crop__1000x1000', 'crop__2000x2000'})+ {+ 'thumbnail__800x800', 'crop__100x100', 'crop__1000x1000',+ 'crop__2000x2000'})@override_settings(VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': False})def test_get_thumbnail_to_larger():@@ -55,6 +57,20 @@ def test_get_thumbnail_to_smaller():assert cropped == cropped_value.url+@patch(+ 'saleor.product.templatetags.product_images.AVAILABLE_SIZES',+ {'thumbnail__800x800'})+@override_settings(+ VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': False},+ PLACEHOLDER_IMAGES={1080: 'images/placeholder1080x1080.png'})+def test_get_thumbnail_no_match_by_method():+ instance = Mock()+ cropped_value = Mock(url='crop.jpg')+ instance.crop = {'1000x1000': cropped_value}+ cropped = get_thumbnail(instance, 800, method='crop')+ assert cropped == static('images/placeholder1080x1080.png')++@override_settings(VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': True})def test_product_first_image():