0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 03:31:04 +01:00

Set context variable to None if no image provided to image templatetag

This commit is contained in:
Maylon Pedroso 2020-01-27 09:47:31 -05:00 committed by Matt Westcott
parent f52d3d89d1
commit 78cc81c505
4 changed files with 70 additions and 0 deletions

View File

@ -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)

View File

@ -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
======================

View File

@ -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'):

View File

@ -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('<img alt="Test image"', rendered)
def test_render_none_to_string(self):
"""
Tests that an ImageNode without image renders an empty string
"""
context = {'image': None}
node = ImageNode(Variable('image'), 'original')
rendered = node.render(context)
self.assertEqual(rendered, '')
def test_render_valid_image_as_context_variable(self):
"""
Tests that an ImageNode with a valid image and a context variable name
renders an empty string and puts a rendition in the context variable
"""
context = {'image': self.image, 'image_node': 'fake value'}
node = ImageNode(Variable('image'), 'original', 'image_node')
rendered = node.render(context)
self.assertEqual(rendered, '')
self.assertIsInstance(context['image_node'], Rendition)
def test_render_none_as_context_variable(self):
"""
Tests that an ImageNode without an image and a context variable name
renders an empty string and puts None in the context variable
"""
context = {'image': None, 'image_node': 'fake value'}
node = ImageNode(Variable('image'), 'original', 'image_node')
rendered = node.render(context)
self.assertEqual(rendered, '')
self.assertIsNone(context['image_node'])