Skip to content

Commit

Permalink
Merge pull request #3197 from solarissmoke/fix/backport-thumbnail-fix
Browse files Browse the repository at this point in the history
Prepare 2.0.3 release that fixes thumbnailing regression
  • Loading branch information
solarissmoke committed Oct 8, 2019
2 parents 9067528 + 0b19f99 commit 3317c47
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 2 deletions.
7 changes: 7 additions & 0 deletions docs/source/ref/settings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,13 @@ Thumbnailer class that will be used to generate thumbnails. Available options:
``pip install django-oscar[easy-thumbnails]``. Custom thumbnailer class (based on
``oscar.core.thumbnails.AbstractThumbnailer``) can be used as well.

``OSCAR_THUMBNAIL_DEBUG``
-------------------------

Default: Same as ``DEBUG``

When set to ``True`` the ``ThumbnailNode.render`` method can raise errors. Django recommends that tags never raise errors in the ``Node.render`` method in production.

Slug settings
=============

Expand Down
2 changes: 2 additions & 0 deletions docs/source/ref/templatetags.rst
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ or

{% oscar_thumbnail [source] [size] [options] as [variable] %}

When the ``OSCAR_THUMBNAIL_DEBUG`` setting is set to ``True``, this template tag will fail with an error if an exception is raised while generating the thumbnail. If set to ``False``, an empty string is returned.

The arguments are:

=================== =====================================================
Expand Down
1 change: 1 addition & 0 deletions docs/source/releases/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Release notes for each version of Oscar published to PyPI.
v2.0
v2.0.1
v2.0.2
v2.0.3


1.6 release branch
Expand Down
16 changes: 16 additions & 0 deletions docs/source/releases/v2.0.3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
=======================
Oscar 2.0.3 release notes
=======================

:release: 2019-10-08

This is Oscar 2.0.3, a bugfix release.

Bug fixes
=========

- Added an ``OSCAR_THUMBNAIL_DEBUG`` setting to control whether the
``oscar_thumbnail`` template tag raises exceptions. This fixes a regression in
Oscar 2 where thumbnailing errors resulted in exceptions being propagated,
contrary to Django's recommendations that template tags should never
raise exceptions.
2 changes: 1 addition & 1 deletion src/oscar/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Use 'alpha', 'beta', 'rc' or 'final' as the 4th element to indicate release type.
VERSION = (2, 0, 2, 'final')
VERSION = (2, 0, 3, 'final')


def get_short_version():
Expand Down
17 changes: 16 additions & 1 deletion src/oscar/templatetags/image_tags.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import logging
import re

from django import template
Expand All @@ -11,6 +12,7 @@

register = template.Library()
kw_pat = re.compile(r'^(?P<key>[\w]+)=(?P<value>.+)$')
logger = logging.getLogger('oscar.thumbnail')


def do_dynamic_image_url(parser, token):
Expand Down Expand Up @@ -101,9 +103,22 @@ def __init__(self, parser, token):
expr = parser.compile_filter(m.group('value'))
self.options.append((key, expr))

def get_thumbnail_options(self, context):
return {'size': self.size_var.resolve(context)}

def render(self, context):
try:
return self._render(context)
except Exception as e:
if getattr(settings, 'OSCAR_THUMBNAIL_DEBUG', settings.DEBUG):
raise e

logger.exception(e)
return ''

def _render(self, context):
source = self.source_var.resolve(context)
options = {'size': self.size_var.resolve(context)}
options = self.get_thumbnail_options(context)
for key, expr in self.options:
value = self.no_resolve.get(text_type(expr), expr.resolve(context))
options[key] = value
Expand Down
27 changes: 27 additions & 0 deletions tests/integration/templatetags/test_oscar_thumbnail.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
from unittest.mock import patch

from django import template
from django.test import TestCase
Expand Down Expand Up @@ -59,6 +60,32 @@ def test_oscar_thumbnail_tag_as_context_value(self):
def test_oscar_thumbnail_sizes(self):
self._test_oscar_thumbnail_tag_sizes()

@patch('oscar.templatetags.image_tags.ThumbnailNode._render')
def test_doesnt_raise_if_oscar_thumbnail_debug_is_false(self, render_mock):
render_mock.side_effect = ValueError()
with override_settings(OSCAR_THUMBNAIL_DEBUG=True):
with self.assertRaises(ValueError):
self.template.render(self.context)

@patch('oscar.templatetags.image_tags.ThumbnailNode._render')
def test_raises_if_oscar_thumbnail_debug_is_true(self, render_mock):
render_mock.side_effect = ValueError()
with override_settings(OSCAR_THUMBNAIL_DEBUG=False):
self.assertEqual(self.template.render(self.context), '')

@patch('oscar.templatetags.image_tags.ThumbnailNode._render')
def test_doesnt_raise_if_debug_is_false_and_oscar_thumbnail_debug_is_not_set(self, render_mock):
render_mock.side_effect = ValueError()
with override_settings(DEBUG=True):
with self.assertRaises(ValueError):
self.template.render(self.context)

@patch('oscar.templatetags.image_tags.ThumbnailNode._render')
def test_raises_if_debug_is_true_and_oscar_thumbnail_debug_is_not_set(self, render_mock):
render_mock.side_effect = ValueError()
with override_settings(DEBUG=False):
self.assertEqual(self.template.render(self.context), '')


@override_settings(
OSCAR_THUMBNAILER='oscar.core.thumbnails.SorlThumbnail',
Expand Down

0 comments on commit 3317c47

Please sign in to comment.