From 4864920555d06432ed3849e9ac32f4c4daa440c5 Mon Sep 17 00:00:00 2001 From: Ascani Carlo <carlo.ratm@gmail.com> Date: Thu, 9 Jan 2020 17:53:09 +0100 Subject: [PATCH] Fix update_index throwing AttributeError when value is None (#5757) --- CHANGELOG.txt | 1 + docs/releases/2.8.rst | 1 + wagtail/contrib/table_block/blocks.py | 7 ++++--- wagtail/contrib/table_block/tests.py | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index e868a57b8e..49d4b9c6a7 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -34,6 +34,7 @@ Changelog * Fix: `file_size` and `file_hash` not updated when Document file changed (Andreas Bernacca) * Fix: Fixed order of URLs in project template so that static / media URLs are not blocked (Nick Smith) * Fix: Added verbose_name_plural for form submission model (Janneke Janssen) + * Fix: Prevent `update_index` failures and incorrect front-end rendering on blank `TableBlock` (Carlo Ascani) 2.7.1 (08.01.2020) diff --git a/docs/releases/2.8.rst b/docs/releases/2.8.rst index 8f1cb7686c..bb1c3f5c8f 100644 --- a/docs/releases/2.8.rst +++ b/docs/releases/2.8.rst @@ -59,6 +59,7 @@ Bug fixes * ``file_size`` and ``file_hash`` not updated when Document file changed (Andreas Bernacca) * Fixed order of URLs in project template so that static / media URLs are not blocked (Nick Smith) * Added verbose_name_plural for form submission model (Janneke Janssen) + * Prevent ``update_index`` failures and incorrect front-end rendering on blank ``TableBlock`` (Carlo Ascani) Upgrade considerations diff --git a/wagtail/contrib/table_block/blocks.py b/wagtail/contrib/table_block/blocks.py index 77a2b84e5f..ca976bcbf7 100644 --- a/wagtail/contrib/table_block/blocks.py +++ b/wagtail/contrib/table_block/blocks.py @@ -83,8 +83,9 @@ class TableBlock(FieldBlock): def get_searchable_content(self, value): content = [] - for row in value.get('data', []): - content.extend([v for v in row if v]) + if value: + for row in value.get('data', []): + content.extend([v for v in row if v]) return content def render(self, value, context=None): @@ -116,7 +117,7 @@ class TableBlock(FieldBlock): return render_to_string(template, new_context) else: - return self.render_basic(value, context=context) + return self.render_basic(value or "", context=context) @property def media(self): diff --git a/wagtail/contrib/table_block/tests.py b/wagtail/contrib/table_block/tests.py index b7b8ab9087..9045778132 100644 --- a/wagtail/contrib/table_block/tests.py +++ b/wagtail/contrib/table_block/tests.py @@ -224,6 +224,12 @@ class TestTableBlock(TestCase): content = block.get_searchable_content(value) self.assertEqual(content, ['Test 1', 'Test 2', 'Test 3', 'Bar', 'Foo', ]) + def test_searchable_content_for_null_block(self): + value = None + block = TableBlock() + content = block.get_searchable_content(value) + self.assertEqual(content, []) + def test_render_with_extra_context(self): """ Test that extra context variables passed in block.render are passed through @@ -264,6 +270,18 @@ class TestTableBlock(TestCase): self.assertHTMLEqual(result, expected) self.assertIn('Test 2', result) + def test_empty_table_block_is_not_rendered(self): + """ + Test an empty table is not rendered. + """ + value = None + block = TableBlock() + result = block.render(value) + expected = '' + + self.assertHTMLEqual(result, expected) + self.assertNotIn('None', result) + class TestTableBlockForm(WagtailTestUtils, SimpleTestCase):