Store block definitions in Block.definition_registry

pull/12700/head
Sage Abdullah 2024-12-16 16:38:37 +00:00
rodzic 841b36ed18
commit 3ea241370d
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: EB1A33CC51CC0217
2 zmienionych plików z 14 dodań i 0 usunięć

Wyświetl plik

@ -55,6 +55,7 @@ class BaseBlock(type):
class Block(metaclass=BaseBlock):
name = ""
creation_counter = 0
definition_registry = {}
TEMPLATE_VAR = "value"
@ -94,6 +95,7 @@ class Block(metaclass=BaseBlock):
self.creation_counter = Block.creation_counter
Block.creation_counter += 1
self.definition_prefix = "blockdef-%d" % self.creation_counter
Block.definition_registry[self.definition_prefix] = self
self.label = self.meta.label or ""

Wyświetl plik

@ -57,6 +57,11 @@ class TestBlock(SimpleTestCase):
obj = object()
self.assertIs(blocks.Block().normalize(obj), obj)
def test_block_definition_registry(self):
"""Instantiating a Block should register it in the definition registry"""
block = blocks.Block()
self.assertIs(blocks.Block.definition_registry[block.definition_prefix], block)
class TestFieldBlock(WagtailTestUtils, SimpleTestCase):
def test_charfield_render(self):
@ -65,6 +70,13 @@ class TestFieldBlock(WagtailTestUtils, SimpleTestCase):
self.assertEqual(html, "Hello world!")
def test_block_definition_registry(self):
block = blocks.CharBlock(label="Test block")
registered_block = blocks.Block.definition_registry[block.definition_prefix]
self.assertIsInstance(registered_block, blocks.CharBlock)
self.assertEqual(registered_block.meta.label, "Test block")
self.assertIs(registered_block, block)
def test_charfield_render_with_template(self):
block = blocks.CharBlock(template="tests/blocks/heading_block.html")
html = block.render("Hello world!")