Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Add placeholder selector
Krzysztof Wolskia4df4e20c0df
diff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.pyindex 8b95f77ac7..ea7319a398 100644--- a/saleor/product/templatetags/product_images.py+++ b/saleor/product/templatetags/product_images.py@@ -1,4 +1,5 @@import logging+import reimport warningsfrom django import template@@ -18,9 +19,30 @@ def get_available_sizes():all_sizes.add(size)return all_sizes+AVAILABLE_SIZES = get_available_sizes()+def choose_placeholder(size=''):+ # type: (str) -> str+ """Assign placeholder at least as big as provided size if possible.+ When size is bigger than available, return the biggest.+ If size is invalid or not provided, return DEFAULT_PLACEHOLDER"""+ placeholder = settings.DEFAULT_PLACEHOLDER+ parsed_sizes = re.match(r'(\d+)x(\d+)', size)+ available_sizes = sorted(settings.PLACEHOLDER_IMAGES.keys())+ if parsed_sizes and available_sizes:+ # check for placeholder equal or bigger than requested picture+ x_size, y_size = parsed_sizes.groups()+ max_size = max([int(x_size), int(y_size)])+ bigger_or_eq = list(filter(lambda x: x >= max_size, available_sizes))+ if len(bigger_or_eq) > 0:+ placeholder = settings.PLACEHOLDER_IMAGES[bigger_or_eq[0]]+ else:+ placeholder = settings.PLACEHOLDER_IMAGES[available_sizes[-1]]+ return placeholder++@register.simple_tag()def get_thumbnail(instance, size, method='crop'):size_name = '%s__%s' % (method, size)@@ -40,7 +62,7 @@ def get_thumbnail(instance, size, method='crop'):extra={'instance': instance, 'size': size})else:return thumbnail.url- return static('images/placeholder' + size + '.png')+ return static(choose_placeholder(size))@register.simple_tag()diff --git a/saleor/settings.py b/saleor/settings.pyindex a537bf220b..41f2cb6bf3 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -309,13 +309,23 @@ VERSATILEIMAGEFIELD_RENDITION_KEY_SETS = {('product_small_2x', 'crop__120x120'),('product_list', 'crop__255x255'),('product_list_2x', 'crop__510x510')]}-+VERSATILEIMAGEFIELD_SETTINGS = {# Images should be pre-generated on Production environment'create_images_on_demand': ast.literal_eval(os.environ.get('CREATE_IMAGES_ON_DEMAND', 'True')),}+PLACEHOLDER_IMAGES = {+ 60: 'images/placeholder60x60.png',+ 120: 'images/placeholder120x120.png',+ 255: 'images/placeholder255x255.png',+ 540: 'images/placeholder540x540.png',+ 1080: 'images/placeholder1080x1080.png'+}++DEFAULT_PLACEHOLDER = 'images/placeholder255x255.png'+WEBPACK_LOADER = {'DEFAULT': {'CACHE': not DEBUG,diff --git a/templates/category/_items.html b/templates/category/_items.htmlindex af600a5231..7e58f6b249 100644--- a/templates/category/_items.html+++ b/templates/category/_items.html@@ -3,6 +3,7 @@{% load price_range from price_ranges %}{% load product_first_image from product_images %}{% load product_availability_schema from jsonld %}+{% load get_thumbnail from product_images %}{% for product, availability in products %}<div class="col-6 col-lg-3 product-list">@@ -12,8 +13,8 @@<a href="{{ product.get_absolute_url }}"><div class="text-center"><div>- <img class="img-responsive"- src="{% get_thumbnail line.variant.product.images.first.image method="crop" size="255x255" %}" srcset="{% get_thumbnail line.variant.product.images.first.image method="crop" size="255x255" %} 1x, {% get_thumbnail line.variant.product.images.first.image method="crop" size="510x510" %} 2x"+ <img class="img-responsive"+ src="{% get_thumbnail line.variant.product.images.first.image method="crop" size="255x255" %}" srcset="{% get_thumbnail line.variant.product.images.first.image method="crop" size="255x255" %} 1x, {% get_thumbnail line.variant.product.images.first.image method="crop" size="510x510" %} 2x"alt=""><span class="product-list-item-name" title="{{ product }}">{{ product }}</span></div>diff --git a/tests/test_product_tags.py b/tests/test_product_tags.pyindex 72833b1be4..bc8a005650 100644--- a/tests/test_product_tags.py+++ b/tests/test_product_tags.py@@ -1,8 +1,7 @@-from mock import Mockfrom django.contrib.staticfiles.templatetags.staticfiles import static+from mock import Mockfrom saleor.product.templatetags.product_images import (- get_thumbnail, product_first_image)-+ choose_placeholder, get_thumbnail,product_first_image)def test_get_thumbnail():instance = Mock()@@ -16,9 +15,13 @@ def test_get_thumbnail():assert thumb == thumbnail_value.url-def test_get_thumbnail_no_instance():+def test_get_thumbnail_no_instance(monkeypatch):+ """When no instance, function should return placeholder"""+ monkeypatch.setattr(+ 'saleor.product.templatetags.product_images.choose_placeholder',+ lambda x: 'placeholder')output = get_thumbnail(instance=None, size='10x10', method='crop')- assert output == static('images/placeholder60x60.png')+ assert output == static('placeholder')def test_product_first_image():@@ -31,3 +34,28 @@ def test_product_first_image():mock_product = Mock(images=mock_queryset)out = product_first_image(mock_product, '10x10', method='crop')assert out == 'crop.jpg'+++def test_choose_placeholder(settings):+ settings.PLACEHOLDER_IMAGES = {+ 10: '10_placeholder',+ 20: '20_placeholder',+ 30: '30_placeholder'}++ settings.DEFAULT_PLACEHOLDER = 'default_placeholder'++ # wrong or no size returns default+ assert choose_placeholder('wrong') == settings.DEFAULT_PLACEHOLDER+ assert choose_placeholder() == settings.DEFAULT_PLACEHOLDER++ # exact size+ assert choose_placeholder('10x10') == settings.PLACEHOLDER_IMAGES[10]++ # when exact not found, choose bigger available+ assert choose_placeholder('15x15') == settings.PLACEHOLDER_IMAGES[20]++ # like previous, but only one side bigger+ assert choose_placeholder('10x15') == settings.PLACEHOLDER_IMAGES[20]++ # when too big requested, choose the biggest available+ assert choose_placeholder('1500x1500') == settings.PLACEHOLDER_IMAGES[30]