From 78cc81c505fc46f4bdaaa2bf35a1b5421aef9e51 Mon Sep 17 00:00:00 2001 From: Maylon Pedroso Date: Mon, 27 Jan 2020 09:47:31 -0500 Subject: [PATCH] Set context variable to None if no image provided to image templatetag --- CHANGELOG.txt | 1 + docs/releases/2.9.rst | 2 + .../images/templatetags/wagtailimages_tags.py | 2 + wagtail/images/tests/test_templatetags.py | 65 +++++++++++++++++++ 4 files changed, 70 insertions(+) create mode 100644 wagtail/images/tests/test_templatetags.py diff --git a/CHANGELOG.txt b/CHANGELOG.txt index 8eee4ce891..957b1e7b54 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -21,6 +21,7 @@ Changelog * Fix: Improve Wagtail logo contrast by adding a background (Brian Edelman, Simon Evans, Ben Enright) * Fix: Prevent duplicate notification messages on page locking (Jacob Topp-Mugglestone) * Fix: Fix InlinePanel item non field errors not visible (Storm Heg) + * Fix: `{% image ... as var %}` now clears the context variable when passed None as an image (Maylon Pedroso) 2.8 (03.02.2020) diff --git a/docs/releases/2.9.rst b/docs/releases/2.9.rst index 5f55a017fe..414b938941 100644 --- a/docs/releases/2.9.rst +++ b/docs/releases/2.9.rst @@ -35,6 +35,8 @@ Bug fixes * Improved Wagtail logo contrast by adding a background (Brian Edelman, Simon Evans, Ben Enright) * Prevent duplicate notification messages on page locking (Jacob Topp-Mugglestone) * Rendering of non field errors for InlinePanel items (Storm Heg) + * ``{% image ... as var %}`` now clears the context variable when passed None as an image (Maylon Pedroso) + Upgrade considerations ====================== diff --git a/wagtail/images/templatetags/wagtailimages_tags.py b/wagtail/images/templatetags/wagtailimages_tags.py index 78ad60934a..811d9d843b 100644 --- a/wagtail/images/templatetags/wagtailimages_tags.py +++ b/wagtail/images/templatetags/wagtailimages_tags.py @@ -97,6 +97,8 @@ class ImageNode(template.Node): return '' if not image: + if self.output_var_name: + context[self.output_var_name] = None return '' if not hasattr(image, 'get_rendition'): diff --git a/wagtail/images/tests/test_templatetags.py b/wagtail/images/tests/test_templatetags.py new file mode 100644 index 0000000000..a74742dd2e --- /dev/null +++ b/wagtail/images/tests/test_templatetags.py @@ -0,0 +1,65 @@ +from django.template import Variable +from django.test import TestCase + +from wagtail.images.models import Image, Rendition +from wagtail.images.templatetags.wagtailimages_tags import ImageNode +from wagtail.images.tests.utils import get_test_image_file + + +class ImageNodeTestCase(TestCase): + + @classmethod + def setUpTestData(cls): + # Create an image for running tests on + cls.image = Image.objects.create( + title="Test image", + file=get_test_image_file(), + ) + + def test_render_valid_image_to_string(self): + """ + Tests that an ImageNode with a valid image renders an img tag + """ + context = {'image': self.image} + node = ImageNode(Variable('image'), 'original') + + rendered = node.render(context) + + self.assertIn('