Skip to content

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

Refactor get_first_image for Product and ProductVariant

fowczarekd85aa7e7beea
diff --git a/saleor/checkout/views/__init__.py b/saleor/checkout/views/__init__.py
index 511ae6c7ce..637ea342ec 100644
--- a/saleor/checkout/views/__init__.py
+++ b/saleor/checkout/views/__init__.py
@@ -222,6 +222,8 @@ def cart_summary(request, cart):
def prepare_line_data(line):
first_image = line.variant.get_first_image()
+ if first_image:
+ first_image = first_image.image
return {
'product': line.variant.product,
'variant': line.variant,
diff --git a/saleor/data_feeds/google_merchant.py b/saleor/data_feeds/google_merchant.py
index bde6a0c8e1..d62aac9724 100644
--- a/saleor/data_feeds/google_merchant.py
+++ b/saleor/data_feeds/google_merchant.py
@@ -116,8 +116,9 @@ def item_group_id(item):
def item_image_link(item, current_site):
- image = item.get_first_image()
- if image:
+ product_image = item.get_first_image()
+ if product_image:
+ image = product_image.image
return add_domain(current_site.domain, image.url, False)
return None
diff --git a/saleor/graphql/order/types.py b/saleor/graphql/order/types.py
index c655951e23..b6e1500487 100644
--- a/saleor/graphql/order/types.py
+++ b/saleor/graphql/order/types.py
@@ -3,7 +3,7 @@ import graphene_django_optimizer as gql_optimizer
from graphene import relay
from ...order import OrderEvents, OrderEventsEmails, models
-from ...product.templatetags.product_images import get_thumbnail
+from ...product.templatetags.product_images import get_product_image_thumbnail
from ..account.types import User
from ..core.types.common import CountableDjangoObjectType
from ..core.types.money import Money, TaxedMoney
@@ -131,7 +131,7 @@ class OrderLine(CountableDjangoObjectType):
return None
if not size:
size = 255
- url = get_thumbnail(
+ url = get_product_image_thumbnail(
self.variant.get_first_image(), size, method='thumbnail')
return info.context.build_absolute_uri(url)
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 32c181aa0d..0c4e7d348f 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -8,7 +8,8 @@ from graphene import relay
from graphql.error import GraphQLError
from ...product import models
-from ...product.templatetags.product_images import get_thumbnail
+from ...product.templatetags.product_images import (
+ get_thumbnail, get_product_image_thumbnail)
from ...product.utils import calculate_revenue_for_variant
from ...product.utils.availability import get_availability
from ...product.utils.costs import (
@@ -313,7 +314,8 @@ class Product(CountableDjangoObjectType):
def resolve_thumbnail_url(self, info, *, size=None):
if not size:
size = 255
- url = get_thumbnail(self.get_first_image(), size, method='thumbnail')
+ url = get_product_image_thumbnail(
+ self.get_first_image(), size, method='thumbnail')
return info.context.build_absolute_uri(url)
def resolve_url(self, info):
diff --git a/saleor/product/models.py b/saleor/product/models.py
index a4a058a14e..df0295c13a 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -174,7 +174,7 @@ class Product(SeoModel):
def get_first_image(self):
images = list(self.images.all())
- return images[0].image if images else None
+ return images[0] if images else None
def get_price_range(self, discounts=None, taxes=None):
if self.variants.all():
@@ -296,7 +296,7 @@ class ProductVariant(models.Model):
def get_first_image(self):
images = list(self.images.all())
if images:
- return images[0].image
+ return images[0]
return self.product.get_first_image()
def get_ajax_label(self, discounts=None):
diff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.py
index 0e50202dac..2246edf4b5 100644
--- a/saleor/product/templatetags/product_images.py
+++ b/saleor/product/templatetags/product_images.py
@@ -83,15 +83,22 @@ def get_thumbnail_size(size, method, rendition_key_set):
@register.simple_tag()
-def get_thumbnail(instance, size, method, rendition_key_set='products'):
- if instance:
+def get_thumbnail(image_file, size, method, rendition_key_set='products'):
+ if image_file:
used_size = get_thumbnail_size(size, method, rendition_key_set)
try:
- thumbnail = getattr(instance, method)[used_size]
+ thumbnail = getattr(image_file, method)[used_size]
except Exception:
logger.exception(
'Thumbnail fetch failed',
- extra={'instance': instance, 'size': size})
+ extra={'image_file': image_file, 'size': size})
else:
return thumbnail.url
return static(choose_placeholder('%sx%s' % (size, size)))
+
+
+def get_product_image_thumbnail(instance, size, method):
+ if instance:
+ return get_thumbnail(instance.image, size, method)
+ return get_thumbnail(None, size, method)
diff --git a/saleor/seo/schema/email.py b/saleor/seo/schema/email.py
index 87e1b1cbdd..ed51fc699f 100644
--- a/saleor/seo/schema/email.py
+++ b/saleor/seo/schema/email.py
@@ -32,8 +32,9 @@ def get_product_data(line, organization):
product_url = build_absolute_uri(product.get_absolute_url())
product_data['itemOffered']['url'] = product_url
- image = product.get_first_image()
- if image:
+ product_image = product.get_first_image()
+ if product_image:
+ image = product_image.image
product_data['itemOffered']['image'] = build_absolute_uri(
location=image.url)
return product_data
diff --git a/templates/checkout/index.html b/templates/checkout/index.html
index c417fe62e9..2ca72eb5cf 100644
--- a/templates/checkout/index.html
+++ b/templates/checkout/index.html
@@ -1,6 +1,6 @@
{% extends "base.html" %}
{% load i18n %}
-{% load get_thumbnail from product_images %}
+{% load get_product_image_thumbnail from product_images %}
{% load price from taxed_prices %}
{% load static %}
@@ -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.get_first_image method="thumbnail" size=60 %}"
- data-srcset="{% get_thumbnail line.variant.get_first_image method="thumbnail" size=60 %} 1x, {% get_thumbnail line.variant.get_first_image method="thumbnail" size=120 %} 2x"
+ <img class="lazyload lazypreload" data-src="{% get_product_image_thumbnail line.variant.get_first_image method="thumbnail" size=60 %}"
+ data-srcset="{% get_product_image_thumbnail line.variant.get_first_image method="thumbnail" size=60 %} 1x, {% get_product_image_thumbnail line.variant.get_first_image method="thumbnail" size=120 %} 2x"
alt="">
<p>{{ line.variant.product.translated }}<br><small>{{ line.variant.translated }}</small></p>
</a>
diff --git a/templates/dashboard/product/list.html b/templates/dashboard/product/list.html
index 399f9a839f..1a6b5fd641 100644
--- a/templates/dashboard/product/list.html
+++ b/templates/dashboard/product/list.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load materializecss %}
{% load price from taxed_prices %}
-{% load get_thumbnail from product_images %}
+{% load get_product_image_thumbnail from product_images %}
{% load static %}
{% load utils %}
@@ -73,8 +73,8 @@
<label for="id_products_{{ product.pk }}"></label>
</td>
<td class="avatar">
- <img src="{% get_thumbnail product.get_first_image size=60 method="thumbnail" %}"
- srcset="{% get_thumbnail product.get_first_image size=60 method="thumbnail" %} 1x, {% get_thumbnail product.get_first_image size=120 method="thumbnail" %} 2x"
+ <img src="{% get_product_image_thumbnail product.get_first_image size=60 method="thumbnail" %}"
+ srcset="{% get_product_image_thumbnail product.get_first_image size=60 method="thumbnail" %} 1x, {% get_product_image_thumbnail product.get_first_image size=120 method="thumbnail" %} 2x"
alt="">
</td>
<td>
diff --git a/templates/dashboard/search/results.html b/templates/dashboard/search/results.html
index 4964b3d740..ffe27e78d4 100644
--- a/templates/dashboard/search/results.html
+++ b/templates/dashboard/search/results.html
@@ -2,7 +2,7 @@
{% load i18n %}
{% load materializecss %}
{% load price from taxed_prices %}
-{% load get_thumbnail from product_images %}
+{% load get_product_image_thumbnail from product_images %}
{% load static %}
{% block title %}
@@ -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="{% get_thumbnail product.get_first_image size=60 method="thumbnail" %}"
- srcset="{% get_thumbnail product.get_first_image size=60 method="thumbnail" %} 1x, {% get_thumbnail product.get_first_image size=120 method="thumbnail" %} 2x"
+ <img src="{% get_product_image_thumbnail product.get_first_image size=60 method="thumbnail" %}"
+ srcset="{% get_product_image_thumbnail product.get_first_image size=60 method="thumbnail" %} 1x, {% get_product_image_thumbnail product.get_first_image size=120 method="thumbnail" %} 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 d68bbed198..608d06bf28 100644
--- a/templates/order/_ordered_items_table.html
+++ b/templates/order/_ordered_items_table.html
@@ -3,7 +3,7 @@
{% load i18n_address_tags %}
{% load placeholder %}
{% load price from taxed_prices %}
-{% load get_thumbnail from product_images %}
+{% load get_product_image_thumbnail from product_images %}
{% if order.user == user %}
<div class="order-details__addresses">
@@ -51,8 +51,8 @@
<div class="col-md-8 order-details__product__description">
{% if line.variant %}
<a class="link--clean" href="{{ line.variant.get_absolute_url }}">
- <img data-src="{% get_thumbnail line.variant.get_first_image size=60 method="thumbnail" %}"
- data-srcset="{% get_thumbnail line.variant.get_first_image size=60 method="thumbnail" %} 1x, {% get_thumbnail line.variant.get_first_image size=120 method="thumbnail" %} 2x"
+ <img data-src="{% get_product_image_thumbnail line.variant.get_first_image size=60 method="thumbnail" %}"
+ data-srcset="{% get_product_image_thumbnail line.variant.get_first_image size=60 method="thumbnail" %} 1x, {% get_product_image_thumbnail line.variant.get_first_image size=120 method="thumbnail" %} 2x"
class="float-left lazyload lazypreload"
src="{% placeholder size=60 %}">
<span>{{ line.translated_product_name|default:line.product_name }}</span>
diff --git a/templates/product/_items.html b/templates/product/_items.html
index bbebb2ef6a..88085fe2e2 100644
--- a/templates/product/_items.html
+++ b/templates/product/_items.html
@@ -1,7 +1,7 @@
{% load i18n %}
{% load static %}
{% load taxed_prices %}
-{% load get_thumbnail from product_images %}
+{% load get_product_image_thumbnail from product_images %}
{% load placeholder %}
{% for product, availability in products %}
@@ -11,8 +11,8 @@
<div>
<div class="product-image">
<img class="img-responsive lazyload lazypreload"
- data-src="{% get_thumbnail product.get_first_image method="thumbnail" size=255 %}"
- data-srcset="{% get_thumbnail product.get_first_image method="thumbnail" size=255 %} 1x, {% get_thumbnail product.get_first_image method="thumbnail" size=510 %} 2x"
+ data-src="{% get_product_image_thumbnail product.get_first_image method="thumbnail" size=255 %}"
+ data-srcset="{% get_product_image_thumbnail product.get_first_image method="thumbnail" size=255 %} 1x, {% get_product_image_thumbnail product.get_first_image method="thumbnail" size=510 %} 2x"
alt=""
src="{% placeholder size=255 %}">
</div>
diff --git a/templates/product/details.html b/templates/product/details.html
index 8828a5ce1f..bebcc3524d 100644
--- a/templates/product/details.html
+++ b/templates/product/details.html
@@ -4,6 +4,7 @@
{% load build_absolute_uri from urls %}
{% load get_object_properties from attributes %}
{% load get_thumbnail from product_images %}
+{% load get_product_image_thumbnail from product_images %}
{% load i18n %}
{% load markdown from markdown %}
{% load placeholder %}
@@ -27,7 +28,7 @@
<meta property="og:url" content="{{ product_url }}">
<link rel="canonical" href="{{ product_url }}">
- {% get_thumbnail product.get_first_image size=510 method="thumbnail" as product_image %}
+ {% get_product_image_thumbnail product.get_first_image size=510 method="thumbnail" as product_image %}
{% if product_image %}
<meta property="og:image" content="{{ product_image }}" />
<meta property="og:image:width" content="510">
diff --git a/templates/product/product_list_base.html b/templates/product/product_list_base.html
index f8960b9a9c..540192f5c4 100644
--- a/templates/product/product_list_base.html
+++ b/templates/product/product_list_base.html
@@ -115,7 +115,7 @@
{% block topcontent %}
{% if object.background_image %}
- <div class="row" id="product-list-image"
+ <div class="row" id="product-list-image" alt="{{ object.background_image_alt }}"
style="background-image: url({% get_thumbnail object.background_image size='1080x440' method="thumbnail" rendition_key_set="background_images" %})"></div>
{% endif %}
{% endblock topcontent %}
diff --git a/tests/test_product_tags.py b/tests/test_product_tags.py
index 8bfd865d63..c0b9c4e02d 100644
--- a/tests/test_product_tags.py
+++ b/tests/test_product_tags.py
@@ -1,11 +1,11 @@
from unittest.mock import Mock, patch
-import pytest
+import pytest
from django.templatetags.static import static
from django.test import override_settings
from saleor.product.templatetags.product_images import (
- choose_placeholder, get_thumbnail)
+ choose_placeholder, get_product_image_thumbnail, get_thumbnail)
@override_settings(
@@ -26,7 +26,15 @@ 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=10, method='crop')
+ output = get_thumbnail(image_file=None, size=10, method='crop')
+ assert output == static('placeholder')
+
+
+def test_get_product_image_thumbnail_no_instance(monkeypatch):
+ monkeypatch.setattr(
+ 'saleor.product.templatetags.product_images.choose_placeholder',
+ lambda x: 'placeholder')
+ output = get_product_image_thumbnail(instance=None, size=10, method='crop')
assert output == static('placeholder')