Move set_meta_options logic to the base Block class

pull/6730/head
Matt Westcott 2021-01-25 14:39:49 +00:00
rodzic d42b9b4a74
commit a8f41258b5
2 zmienionych plików z 9 dodań i 11 usunięć

Wyświetl plik

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

Wyświetl plik

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