Use a dict as the in-code representation of the lookup table

This makes it more human-readable.
pull/12101/head
Matt Westcott 2024-06-18 14:10:32 +01:00 zatwierdzone przez Matt Westcott
rodzic 1d6c1c7f2e
commit 935a579426
5 zmienionych plików z 55 dodań i 52 usunięć

Wyświetl plik

@ -8,18 +8,18 @@ class BlockDefinitionLookup:
a compact representation that avoids repeating the same definition whenever a
block is re-used in multiple places over the block definition tree.
The underlying data is a list of block definitions, such as:
The underlying data is a dict of block definitions, such as:
```
[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.RichTextBlock", [], {}),
("wagtail.blocks.StreamBlock", [
{
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.RichTextBlock", [], {}),
2: ("wagtail.blocks.StreamBlock", [
[
("heading", 0),
("paragraph", 1),
],
], {}),
]
}
```
where each definition is a tuple of (module_path, args, kwargs) similar to that
@ -80,3 +80,6 @@ class BlockDefinitionLookupBuilder:
self.blocks.append(deconstructed)
block_indexes.append((index, deconstructed))
return index
def get_lookup_as_dict(self):
return dict(enumerate(self.blocks))

Wyświetl plik

@ -263,9 +263,9 @@ class TestTableBlock(TestCase):
class TestBlockDefinitionLookup(TestCase):
def test_block_lookup(self):
lookup = BlockDefinitionLookup(
[
("wagtail.blocks.CharBlock", [], {"required": True}),
(
{
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: (
"wagtail.blocks.ChoiceBlock",
[],
{
@ -276,7 +276,7 @@ class TestBlockDefinitionLookup(TestCase):
]
},
),
(
2: (
"wagtail.contrib.typed_table_block.blocks.TypedTableBlock",
[
[
@ -286,7 +286,7 @@ class TestBlockDefinitionLookup(TestCase):
],
{},
),
]
}
)
struct_block = lookup.get_block(2)
self.assertIsInstance(struct_block, TypedTableBlock)

Wyświetl plik

@ -171,7 +171,7 @@ class StreamField(models.Field):
for name, block in self.stream_block.child_blocks.items()
]
args = [block_types]
kwargs["block_lookup"] = lookup.blocks
kwargs["block_lookup"] = lookup.get_lookup_as_dict()
return name, path, args, kwargs
def to_python(self, value):

Wyświetl plik

@ -5888,10 +5888,10 @@ class TestValidationErrorAsJsonData(TestCase):
class TestBlockDefinitionLookup(TestCase):
def test_simple_lookup(self):
lookup = BlockDefinitionLookup(
[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.RichTextBlock", [], {}),
]
{
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.RichTextBlock", [], {}),
}
)
char_block = lookup.get_block(0)
char_block.set_name("title")
@ -5913,10 +5913,10 @@ class TestBlockDefinitionLookup(TestCase):
def test_structblock_lookup(self):
lookup = BlockDefinitionLookup(
[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.RichTextBlock", [], {}),
(
{
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.RichTextBlock", [], {}),
2: (
"wagtail.blocks.StructBlock",
[
[
@ -5926,7 +5926,7 @@ class TestBlockDefinitionLookup(TestCase):
],
{},
),
]
}
)
struct_block = lookup.get_block(2)
self.assertIsInstance(struct_block, blocks.StructBlock)
@ -5938,10 +5938,10 @@ class TestBlockDefinitionLookup(TestCase):
def test_streamblock_lookup(self):
lookup = BlockDefinitionLookup(
[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.RichTextBlock", [], {}),
(
{
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.RichTextBlock", [], {}),
2: (
"wagtail.blocks.StreamBlock",
[
[
@ -5951,7 +5951,7 @@ class TestBlockDefinitionLookup(TestCase):
],
{},
),
]
}
)
stream_block = lookup.get_block(2)
self.assertIsInstance(stream_block, blocks.StreamBlock)
@ -5963,10 +5963,10 @@ class TestBlockDefinitionLookup(TestCase):
def test_listblock_lookup(self):
lookup = BlockDefinitionLookup(
[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.ListBlock", [0], {}),
]
{
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.ListBlock", [0], {}),
}
)
list_block = lookup.get_block(1)
self.assertIsInstance(list_block, blocks.ListBlock)
@ -5977,9 +5977,9 @@ class TestBlockDefinitionLookup(TestCase):
# Passing a class as the child block is still valid; this is not converted
# to a reference
lookup = BlockDefinitionLookup(
[
("wagtail.blocks.ListBlock", [blocks.CharBlock], {}),
]
{
0: ("wagtail.blocks.ListBlock", [blocks.CharBlock], {}),
}
)
list_block = lookup.get_block(0)
self.assertIsInstance(list_block, blocks.ListBlock)

Wyświetl plik

@ -731,11 +731,11 @@ class TestConstructStreamFieldFromLookup(TestCase):
("paragraph", 1),
("button", 3),
],
block_lookup=[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.RichTextBlock", [], {}),
("wagtail.blocks.PageChooserBlock", [], {}),
(
block_lookup={
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.RichTextBlock", [], {}),
2: ("wagtail.blocks.PageChooserBlock", [], {}),
3: (
"wagtail.blocks.StructBlock",
[
[
@ -745,7 +745,7 @@ class TestConstructStreamFieldFromLookup(TestCase):
],
{},
),
],
},
)
stream_block = field.stream_block
self.assertIsInstance(stream_block, blocks.StreamBlock)
@ -773,11 +773,11 @@ class TestConstructStreamFieldFromLookup(TestCase):
def test_construct_top_level_block_from_lookup(self):
field = StreamField(
4,
block_lookup=[
("wagtail.blocks.CharBlock", [], {"required": True}),
("wagtail.blocks.RichTextBlock", [], {}),
("wagtail.blocks.PageChooserBlock", [], {}),
(
block_lookup={
0: ("wagtail.blocks.CharBlock", [], {"required": True}),
1: ("wagtail.blocks.RichTextBlock", [], {}),
2: ("wagtail.blocks.PageChooserBlock", [], {}),
3: (
"wagtail.blocks.StructBlock",
[
[
@ -787,7 +787,7 @@ class TestConstructStreamFieldFromLookup(TestCase):
],
{},
),
(
4: (
"wagtail.blocks.StreamBlock",
[
[
@ -798,7 +798,7 @@ class TestConstructStreamFieldFromLookup(TestCase):
],
{},
),
],
},
)
stream_block = field.stream_block
self.assertIsInstance(stream_block, blocks.StreamBlock)
@ -857,11 +857,11 @@ class TestDeconstructStreamFieldWithLookup(TestCase):
kwargs,
{
"blank": True,
"block_lookup": [
("wagtail.blocks.CharBlock", (), {"required": True}),
("wagtail.blocks.RichTextBlock", (), {}),
("wagtail.blocks.PageChooserBlock", (), {}),
(
"block_lookup": {
0: ("wagtail.blocks.CharBlock", (), {"required": True}),
1: ("wagtail.blocks.RichTextBlock", (), {}),
2: ("wagtail.blocks.PageChooserBlock", (), {}),
3: (
"wagtail.blocks.StructBlock",
[
[
@ -871,6 +871,6 @@ class TestDeconstructStreamFieldWithLookup(TestCase):
],
{},
),
],
},
},
)