From 17c564b7a015a616ee58726ace77b96775bd6e0a Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Tue, 16 Oct 2018 12:07:09 +0100 Subject: [PATCH] Catch empty non-StreamValues in get_prep_value. Fixes #4809 --- wagtail/core/blocks/stream_block.py | 5 +- .../0035_streamfieldmigrationmodels.py | 47 +++++++++++++++++++ ...36_streamfieldmigrationmodels_add_field.py | 33 +++++++++++++ wagtail/tests/testapp/models.py | 19 ++++++++ 4 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 wagtail/tests/testapp/migrations/0035_streamfieldmigrationmodels.py create mode 100644 wagtail/tests/testapp/migrations/0036_streamfieldmigrationmodels_add_field.py diff --git a/wagtail/core/blocks/stream_block.py b/wagtail/core/blocks/stream_block.py index d47f827e3..7deeff408 100644 --- a/wagtail/core/blocks/stream_block.py +++ b/wagtail/core/blocks/stream_block.py @@ -243,8 +243,9 @@ class BaseStreamBlock(Block): ], is_lazy=True) def get_prep_value(self, value): - if value is None: - # treat None as identical to an empty stream + if not value: + # Falsy values (including None, empty string, empty list, and + # empty StreamValue) become an empty stream return [] else: # value is a StreamValue - delegate to its get_prep_value() method diff --git a/wagtail/tests/testapp/migrations/0035_streamfieldmigrationmodels.py b/wagtail/tests/testapp/migrations/0035_streamfieldmigrationmodels.py new file mode 100644 index 000000000..08172b22b --- /dev/null +++ b/wagtail/tests/testapp/migrations/0035_streamfieldmigrationmodels.py @@ -0,0 +1,47 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.15 on 2018-10-16 10:58 +from __future__ import unicode_literals + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('wagtailcore', '0040_page_draft_title'), + ('tests', '0034_advertwithuuidcustomprimarykey'), + ] + + operations = [ + migrations.CreateModel( + name='AddedStreamFieldWithEmptyListDefaultPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), + ], + options={ + 'abstract': False, + }, + bases=('wagtailcore.page',), + ), + migrations.CreateModel( + name='AddedStreamFieldWithEmptyStringDefaultPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), + ], + options={ + 'abstract': False, + }, + bases=('wagtailcore.page',), + ), + migrations.CreateModel( + name='AddedStreamFieldWithoutDefaultPage', + fields=[ + ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), + ], + options={ + 'abstract': False, + }, + bases=('wagtailcore.page',), + ), + ] diff --git a/wagtail/tests/testapp/migrations/0036_streamfieldmigrationmodels_add_field.py b/wagtail/tests/testapp/migrations/0036_streamfieldmigrationmodels_add_field.py new file mode 100644 index 000000000..bca667e0d --- /dev/null +++ b/wagtail/tests/testapp/migrations/0036_streamfieldmigrationmodels_add_field.py @@ -0,0 +1,33 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.15 on 2018-10-16 11:00 +from __future__ import unicode_literals + +from django.db import migrations +import wagtail.core.blocks +import wagtail.core.fields + + +class Migration(migrations.Migration): + + dependencies = [ + ('tests', '0035_streamfieldmigrationmodels'), + ] + + operations = [ + migrations.AddField( + model_name='addedstreamfieldwithemptylistdefaultpage', + name='body', + field=wagtail.core.fields.StreamField([('title', wagtail.core.blocks.CharBlock())], default=[]), + ), + migrations.AddField( + model_name='addedstreamfieldwithemptystringdefaultpage', + name='body', + field=wagtail.core.fields.StreamField([('title', wagtail.core.blocks.CharBlock())], default=''), + ), + migrations.AddField( + model_name='addedstreamfieldwithoutdefaultpage', + name='body', + field=wagtail.core.fields.StreamField([('title', wagtail.core.blocks.CharBlock())], default=''), + preserve_default=False, + ), + ] diff --git a/wagtail/tests/testapp/models.py b/wagtail/tests/testapp/models.py index a29b4a51e..835da832d 100644 --- a/wagtail/tests/testapp/models.py +++ b/wagtail/tests/testapp/models.py @@ -1232,3 +1232,22 @@ class TabbedSettings(TestSetting): class AlwaysShowInMenusPage(Page): show_in_menus_default = True + + +# test for AddField migrations on StreamFields using various default values +class AddedStreamFieldWithoutDefaultPage(Page): + body = StreamField([ + ('title', CharBlock()) + ]) + + +class AddedStreamFieldWithEmptyStringDefaultPage(Page): + body = StreamField([ + ('title', CharBlock()) + ], default='') + + +class AddedStreamFieldWithEmptyListDefaultPage(Page): + body = StreamField([ + ('title', CharBlock()) + ], default=[])