From a8f41258b5fd9869fd13f44f14be375e2aed17b8 Mon Sep 17 00:00:00 2001 From: Matt Westcott Date: Mon, 25 Jan 2021 14:39:49 +0000 Subject: [PATCH] Move set_meta_options logic to the base Block class --- wagtail/core/blocks/base.py | 15 +++++++++------ wagtail/core/blocks/stream_block.py | 5 ----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/wagtail/core/blocks/base.py b/wagtail/core/blocks/base.py index 8adb22f731..af3881570e 100644 --- a/wagtail/core/blocks/base.py +++ b/wagtail/core/blocks/base.py @@ -127,15 +127,18 @@ class Block(metaclass=BaseBlock): if not self.meta.label: self.label = capfirst(force_str(name).replace('_', ' ')) - def set_meta_options(self, options): + def set_meta_options(self, opts): """ - Called when this block is used as the top-level block of a StreamField, to pass on any options - from the StreamField constructor that ought to be handled by the block, e.g. + Update this block's meta options (out of the ones designated as mutable) from the given dict. + Used by the StreamField constructor to pass on kwargs that are to be handled by the block, + since the block object has already been created by that point, e.g.: body = StreamField(SomeStreamBlock(), max_num=5) """ - # Ignore all options here; block types that are allowed at the top level (i.e. currently just - # StreamBlock) and recognise these options will override this method - pass + for attr, value in opts.items(): + if attr in self.MUTABLE_META_ATTRIBUTES: + setattr(self.meta, attr, value) + else: + raise TypeError("set_meta_options received unexpected option: %r" % attr) @property def media(self): diff --git a/wagtail/core/blocks/stream_block.py b/wagtail/core/blocks/stream_block.py index ed6b060408..1dcf1dc3db 100644 --- a/wagtail/core/blocks/stream_block.py +++ b/wagtail/core/blocks/stream_block.py @@ -51,11 +51,6 @@ class BaseStreamBlock(Block): self.dependencies = self.child_blocks.values() - def set_meta_options(self, opts): - for attr in ['required', 'min_num', 'max_num', 'block_counts']: - if attr in opts: - setattr(self.meta, attr, opts[attr]) - def get_default(self): """ Default values set on a StreamBlock should be a list of (type_name, value) tuples -