Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Introduce templatetag for fetching image thumbnails
Artur Smęt5c91a2c8dda6
diff --git a/saleor/product/templatetags/product_images.py b/saleor/product/templatetags/product_images.pynew file mode 100644index 0000000000..bc517ee6b1--- /dev/null+++ b/saleor/product/templatetags/product_images.py@@ -0,0 +1,43 @@+import logging+import warnings++from django.template.context_processors import static+from django import template+from django.conf import settings++logger = logging.getLogger(__name__)+register = template.Library()+++# cache available sizes at module level+def get_available_sizes():+ all_sizes = set()+ keys = settings.VERSATILEIMAGEFIELD_RENDITION_KEY_SETS+ for size_group, sizes in keys.items():+ for size_name, size in sizes:+ all_sizes.add(size)+ return all_sizes++AVAILABLE_SIZES = get_available_sizes()++[email protected]_tag()+def product_image(instance, size, method='crop'):+ if instance:+ size_name = '%s__%s' % (method, size)+ if (size_name not in AVAILABLE_SIZES and not+ settings.VERSATILEIMAGEFIELD_SETTINGS['create_images_on_demand']):+ msg = ('Thumbnail size %s is not defined in settings '+ 'and it won\'t be generated automatically' % size_name)+ warnings.warn(msg)+ try:+ if method == 'crop':+ thumbnail = instance.crop[size]+ else:+ thumbnail = instance.thumbnail[size]+ except:+ logger.exception('Thumbnail fetch failed',+ extra={'instance': instance, 'size': size})+ else:+ return thumbnail.url+ return static('dist/images/product-image-placeholder.png')