Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Fix templates and tests

oldPadavan20a67e79a52b
diff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.py
index 35b7d773d7..c5cb0fecab 100644
--- a/saleor/product/templatetags/product_images.py
+++ b/saleor/product/templatetags/product_images.py
@@ -47,7 +47,8 @@ def choose_placeholder(size=''):
@register.simple_tag()
def get_thumbnail(instance, size, method='crop'):
- size_name = '%s__%sx%s' % (method, size, size)
+ size_str = '%sx%s' % (size, size)
+ size_name = '%s__%s' % (method, size_str)
on_demand = settings.VERSATILEIMAGEFIELD_SETTINGS[
'create_images_on_demand']
if instance:
@@ -55,13 +56,13 @@ def get_thumbnail(instance, size, method='crop'):
# 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)
+ used_size = size_str
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')]
+ 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:
@@ -80,7 +81,7 @@ def get_thumbnail(instance, size, method='crop'):
"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)
+ used_size = size_str
try:
thumbnail = getattr(instance, method)[used_size]
except Exception:
@@ -89,7 +90,7 @@ def get_thumbnail(instance, size, method='crop'):
extra={'instance': instance, 'size': size})
else:
return thumbnail.url
- return static(choose_placeholder('%sx%s' % (size, size)))
+ return static(choose_placeholder(size_str))
@register.simple_tag()
diff --git a/templates/cart_dropdown.html b/templates/cart_dropdown.html
index c966ea9dc7..3afb116e7a 100644
--- a/templates/cart_dropdown.html
+++ b/templates/cart_dropdown.html
@@ -12,8 +12,8 @@
<div class="col-md-10">
<a class="link--clean" href="{{ line.variant_url }}">
<img class="cart-dropdown__image lazyload lazypreload"
- data-src="{% get_thumbnail line.image size="60x60" %}"
- data-srcset="{% get_thumbnail line.image size="60x60" %} 1x, {% get_thumbnail line.image size="120x120" %} 2x"
+ data-src="{% get_thumbnail line.image size=60 %}"
+ data-srcset="{% get_thumbnail line.image size=60 %} 1x, {% get_thumbnail line.image size=120 %} 2x"
alt=""
src="{% placeholder size=120 %}">
<h3>
diff --git a/templates/checkout/index.html b/templates/checkout/index.html
index ea2ee06b86..bd42c32bdb 100644
--- a/templates/checkout/index.html
+++ b/templates/checkout/index.html
@@ -42,8 +42,8 @@
<div class="row">
<div class="col-7 cart__line__product">
<a class="link--clean" href="{{ line.variant.get_absolute_url }}">
- <img class="lazyload lazypreload" data-src="{% get_thumbnail line.variant.product.images.first.image method="crop" size="60x60" %}"
- data-srcset="{% get_thumbnail line.variant.product.images.first.image method="crop" size="60x60" %} 1x, {% get_thumbnail line.variant.product.images.first.image method="crop" size="120x120" %} 2x"
+ <img class="lazyload lazypreload" data-src="{% get_thumbnail line.variant.product.images.first.image method="crop" size=60 %}"
+ data-srcset="{% get_thumbnail line.variant.product.images.first.image method="crop" size=60 %} 1x, {% get_thumbnail line.variant.product.images.first.image method="crop" size=120 %} 2x"
alt="">
<p>{{ line.variant.product }}<br><small>{{ line.variant }}</small></p>
</a>
diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.html
index 5e22236b33..3a68890196 100644
--- a/templates/dashboard/product/list.html
+++ b/templates/dashboard/product/list.html
@@ -73,8 +73,8 @@
<label for="id_products_{{ product.pk }}"></label>
</td>
<td class="avatar">
- <img src="{% product_first_image product size="60x60" method="crop" %}"
- srcset="{% product_first_image product size="60x60" method="crop" %} 1x, {% product_first_image product size="120x120" method="crop" %} 2x"
+ <img src="{% product_first_image product size=60 method="crop" %}"
+ srcset="{% product_first_image product size=60 method="crop" %} 1x, {% product_first_image product size=120 method="crop" %} 2x"
alt="">
</td>
<td>
diff --git a/templates/dashboard/search/results.html b/templates/dashboard/search/results.html
index 2545205352..2fcb72a846 100644
--- a/templates/dashboard/search/results.html
+++ b/templates/dashboard/search/results.html
@@ -27,8 +27,8 @@
{% for product in products %}
<li class="collection-item avatar list-item">
<a href="{% url 'dashboard:product-details' pk=product.pk %}">
- <img src="{% product_first_image product size="60x60" method="crop" %}"
- srcset="{% product_first_image product size="60x60" method="crop" %} 1x, {% product_first_image product size="120x120" method="crop" %} 2x"
+ <img src="{% product_first_image product size=60 method="crop" %}"
+ srcset="{% product_first_image product size=60 method="crop" %} 1x, {% product_first_image product size=120 method="crop" %} 2x"
class="circle" alt="{{ product.name }}">
<span class="list-item-name">
{{ product.name }}
diff --git a/templates/order/_ordered_items_table.html b/templates/order/_ordered_items_table.html
index c53aff1eaa..6ef13725dd 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -50,8 +50,8 @@
<div class="row">
<div class="col-md-8">
<a class="link--clean" href="{% if line.variant %}{{ line.variant.get_absolute_url }}{% endif %}">
- <img data-src="{% product_first_image line.variant.product size="60x60" method="crop" %}"
- data-srcset="{% product_first_image line.variant.product size="60x60" method="crop" %} 1x, {% product_first_image line.variant.product size="120x120" method="crop" %} 2x"
+ <img data-src="{% product_first_image line.variant.product size=60 method="crop" %}"
+ data-srcset="{% product_first_image line.variant.product size=60 method="crop" %} 1x, {% product_first_image line.variant.product size=120 method="crop" %} 2x"
class="float-left lazyload lazypreload"
src="{% placeholder size=60 %}">
<span class="order-details__product__description">{{ line.product_name }}</span>
diff --git a/templates/product/_items.html b/templates/product/_items.html
index ceb4979277..6e828f5793 100644
--- a/templates/product/_items.html
+++ b/templates/product/_items.html
@@ -11,8 +11,8 @@
<div class="text-center">
<div>
<img class="img-responsive lazyload lazypreload"
- data-src="{% product_first_image product method="crop" size="255x255" %}"
- data-srcset="{% product_first_image product method="crop" size="255x255" %} 1x, {% product_first_image product method="crop" size="510x510" %} 2x"
+ data-src="{% product_first_image product method="crop" size=255 %}"
+ data-srcset="{% product_first_image product method="crop" size=255 %} 1x, {% product_first_image product method="crop" size=510 %} 2x"
alt=""
src="{% placeholder size=255 %}">
<span class="product-list-item-name" title="{{ product }}">{{ product }}</span>
diff --git a/templates/product/details.html b/templates/product/details.html
index 5f70b6c829..575eb9b107 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -27,7 +27,7 @@
<meta property="og:url" content="{{ product_url }}">
<link rel="canonical" href="{{ product_url }}">
- {% product_first_image product size="510x510" as product_image %}
+ {% product_first_image product size=510 as product_image %}
{% if product_image %}
<meta property="og:image" content="{{ product_image }}" />
<meta property="og:image:width" content="510">
@@ -74,8 +74,8 @@
{% for image in images %}
<div class="carousel-item{% if forloop.first %} active{% endif %}">
<img class="d-block img-fluid lazyload lazypreload"
- data-src="{% get_thumbnail image.image method="crop" size="540x540" %}"
- data-srcset="{% get_thumbnail image.image method="crop" size="540x540" %} 1x, {% get_thumbnail image.image method="crop" size="1080x1080" %} 2x"
+ data-src="{% get_thumbnail image.image method="crop" size=540 %}"
+ data-srcset="{% get_thumbnail image.image method="crop" size=540 %} 1x, {% get_thumbnail image.image method="crop" size=1080 %} 2x"
alt=""
src="{% placeholder size=540 %}">
</div>
@@ -93,8 +93,8 @@
{% for image in images %}
{% if images|length > 1 %}
<li data-target="#carousel-example-generic" data-slide-to="{{ forloop.counter0 }}"{% if forloop.first %} class="active"{% endif %}>
- <img data-src="{% get_thumbnail image.image method="crop" size="60x60" %}"
- data-srcset="{% get_thumbnail image.image method="crop" size="60x60" %} 1x, {% get_thumbnail image.image method="crop" size="120x120" %} 2x"
+ <img data-src="{% get_thumbnail image.image method="crop" size=60 %}"
+ data-srcset="{% get_thumbnail image.image method="crop" size=60 %} 1x, {% get_thumbnail image.image method="crop" size=120 %} 2x"
alt=""
class="lazyload lazypreload"
src="{% placeholder size=60 %}">
diff --git a/tests/api/test_graphql.py b/tests/api/test_graphql.py
index cc8e7a2375..96392eccb2 100644
--- a/tests/api/test_graphql.py
+++ b/tests/api/test_graphql.py
@@ -120,8 +120,8 @@ def test_real_query(user_api_client, product):
...ProductPriceFragmentQuery
__typename
}
- thumbnailUrl1x: thumbnailUrl(size: "255x255")
- thumbnailUrl2x: thumbnailUrl(size: "510x510")
+ thumbnailUrl1x: thumbnailUrl(size: 255)
+ thumbnailUrl2x: thumbnailUrl(size: 510)
url
__typename
}
diff --git a/tests/test_product_tags.py b/tests/test_product_tags.py
index 3de94651cf..e93c746544 100644
--- a/tests/test_product_tags.py
+++ b/tests/test_product_tags.py
@@ -1,20 +1,23 @@
-from unittest.mock import Mock
+from unittest.mock import Mock, patch
from django.contrib.staticfiles.templatetags.staticfiles import static
+from django.test import override_settings
from saleor.product.templatetags.product_images import (
choose_placeholder, get_thumbnail, product_first_image)
+@override_settings(
+ VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': True})
def test_get_thumbnail():
instance = Mock()
cropped_value = Mock(url='crop.jpg')
thumbnail_value = Mock(url='thumb.jpg')
instance.crop = {'10x10': cropped_value}
instance.thumbnail = {'10x10': thumbnail_value}
- cropped = get_thumbnail(instance, '10x10', method='crop')
+ cropped = get_thumbnail(instance, 10, method='crop')
assert cropped == cropped_value.url
- thumb = get_thumbnail(instance, '10x10', method='thumbnail')
+ thumb = get_thumbnail(instance, 10, method='thumbnail')
assert thumb == thumbnail_value.url
@@ -22,10 +25,38 @@ def test_get_thumbnail_no_instance(monkeypatch):
monkeypatch.setattr(
'saleor.product.templatetags.product_images.choose_placeholder',
lambda x: 'placeholder')
- output = get_thumbnail(instance=None, size='10x10', method='crop')
+ output = get_thumbnail(instance=None, size=10, method='crop')
assert output == static('placeholder')
+@patch(
+ 'saleor.product.templatetags.product_images.AVAILABLE_SIZES',
+ {'crop__10x10', 'crop__100x100', 'crop__1000x1000', 'crop__2000x2000'})
+@override_settings(
+ VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': False})
+def test_get_thumbnail_to_larger():
+ instance = Mock()
+ cropped_value = Mock(url='crop.jpg')
+ instance.crop = {'1000x1000': cropped_value}
+ cropped = get_thumbnail(instance, 800, method='crop')
+ assert cropped == cropped_value.url
+
+
+@patch(
+ 'saleor.product.templatetags.product_images.AVAILABLE_SIZES',
+ {'crop__10x10', 'crop__100x100', 'crop__1000x1000', 'crop__2000x2000'})
+@override_settings(
+ VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': False})
+def test_get_thumbnail_to_smaller():
+ instance = Mock()
+ cropped_value = Mock(url='crop.jpg')
+ instance.crop = {'100x100': cropped_value}
+ cropped = get_thumbnail(instance, 400, method='crop')
+ assert cropped == cropped_value.url
+
+
+@override_settings(
+ VERSATILEIMAGEFIELD_SETTINGS={'create_images_on_demand': True})
def test_product_first_image():
mock_product_image = Mock()
mock_product_image.image = Mock()
@@ -34,7 +65,7 @@ def test_product_first_image():
mock_queryset = Mock()
mock_queryset.all.return_value = [mock_product_image]
mock_product = Mock(images=mock_queryset)
- out = product_first_image(mock_product, '10x10', method='crop')
+ out = product_first_image(mock_product, 10, method='crop')
assert out == 'crop.jpg'