From 3c8b43f09a981c4948dc0b1103a2db0849391685 Mon Sep 17 00:00:00 2001 From: Michael van Tellingen Date: Wed, 8 Feb 2017 19:18:46 +0100 Subject: [PATCH] Fix a regression when overriding Block.get_context() If ``Block.get_context()`` was overriden without accepting the ``parent_context`` kwarg the fallback handling threw an exception if ``context`` was None. See #3346 --- wagtail/wagtailcore/blocks/base.py | 2 +- wagtail/wagtailcore/tests/test_blocks.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/wagtail/wagtailcore/blocks/base.py b/wagtail/wagtailcore/blocks/base.py index 153176298..0320c6ad2 100644 --- a/wagtail/wagtailcore/blocks/base.py +++ b/wagtail/wagtailcore/blocks/base.py @@ -246,7 +246,7 @@ class Block(six.with_metaclass(BaseBlock, object)): "keyword argument" % class_with_render_method, category=RemovedInWagtail111Warning ) - new_context = context + new_context = context or {} new_context.update(self.get_context(value)) return mark_safe(render_to_string(template, new_context)) diff --git a/wagtail/wagtailcore/tests/test_blocks.py b/wagtail/wagtailcore/tests/test_blocks.py index 726ee53f1..69f3831f3 100644 --- a/wagtail/wagtailcore/tests/test_blocks.py +++ b/wagtail/wagtailcore/tests/test_blocks.py @@ -84,6 +84,17 @@ class TestFieldBlock(unittest.TestCase): self.assertIs(ws[0].category, RemovedInWagtail111Warning) self.assertEqual(html, '

Bonjour le monde!

') + def test_charfield_render_with_legacy_get_context_none(self): + block = NoExtraContextCharBlock(template='tests/blocks/heading_block.html') + with warnings.catch_warnings(record=True) as ws: + warnings.simplefilter('always') + + html = block.render("Bonjour le monde!") + + self.assertEqual(len(ws), 1) + self.assertIs(ws[0].category, RemovedInWagtail111Warning) + self.assertEqual(html, '

Bonjour le monde!

') + def test_charfield_render_form(self): block = blocks.CharBlock() html = block.render_form("Hello world!")