Python wagtail.core.blocks.ListBlock() Examples
The following are 26
code examples of wagtail.core.blocks.ListBlock().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
wagtail.core.blocks
, or try the search function
.
Example #1
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def render(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = blocks.ListBlock(LinkBlock()) return block.render([ { 'title': "Wagtail", 'link': 'http://www.wagtail.io', }, { 'title': "Django", 'link': 'http://www.djangoproject.com', }, ])
Example #2
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def render_form(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = blocks.ListBlock(LinkBlock) html = block.render_form([ { 'title': "Wagtail", 'link': 'http://www.wagtail.io', }, { 'title': "Django", 'link': 'http://www.djangoproject.com', }, ], prefix='links') return html
Example #3
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_html_declarations(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = blocks.ListBlock(LinkBlock) html = block.html_declarations() self.assertTagInTemplateScript( '<input id="__PREFIX__-value-title" name="__PREFIX__-value-title" placeholder="Title" type="text" />', html ) self.assertTagInTemplateScript( '<input id="__PREFIX__-value-link" name="__PREFIX__-value-link" placeholder="Link" type="url" />', html )
Example #4
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_html_declarations_uses_default(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock(default="Github") link = blocks.URLBlock(default="http://www.github.com") block = blocks.ListBlock(LinkBlock) html = block.html_declarations() self.assertTagInTemplateScript( ( '<input id="__PREFIX__-value-title" name="__PREFIX__-value-title" placeholder="Title"' ' type="text" value="Github" />' ), html ) self.assertTagInTemplateScript( ( '<input id="__PREFIX__-value-link" name="__PREFIX__-value-link" placeholder="Link"' ' type="url" value="http://www.github.com" />' ), html )
Example #5
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_system_checks_recurse_into_lists(self): failing_block = blocks.RichTextBlock() block = blocks.StreamBlock([ ('paragraph_list', blocks.ListBlock( blocks.StructBlock([ ('heading', blocks.CharBlock()), ('rich text', failing_block), ]) )) ]) errors = block.check() self.assertEqual(len(errors), 1) self.assertEqual(errors[0].id, 'wagtailcore.E001') self.assertEqual(errors[0].hint, "Block names cannot contain spaces") self.assertEqual(errors[0].obj, failing_block)
Example #6
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_default_default(self): """ if no explicit 'default' is set on the ListBlock, it should fall back on a single instance of the child block in its default state. """ class ShoppingListBlock(blocks.StructBlock): shop = blocks.CharBlock() items = blocks.ListBlock(blocks.CharBlock(default='chocolate')) block = ShoppingListBlock() # the value here does not specify an 'items' field, so this should revert to the ListBlock's default form_html = block.render_form(block.to_python({'shop': 'Tesco'}), prefix='shoppinglist') self.assertIn( '<input type="hidden" name="shoppinglist-items-count" id="shoppinglist-items-count" value="1">', form_html ) self.assertIn('value="chocolate"', form_html)
Example #7
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_can_specify_default(self): class ShoppingListBlock(blocks.StructBlock): shop = blocks.CharBlock() items = blocks.ListBlock(blocks.CharBlock(), default=['peas', 'beans', 'carrots']) block = ShoppingListBlock() # the value here does not specify an 'items' field, so this should revert to the ListBlock's default form_html = block.render_form(block.to_python({'shop': 'Tesco'}), prefix='shoppinglist') self.assertIn( '<input type="hidden" name="shoppinglist-items-count" id="shoppinglist-items-count" value="3">', form_html ) self.assertIn('value="peas"', form_html)
Example #8
Source File: streamfield.py From wagtail-graphql with MIT License | 5 votes |
def _is_list_block(block): return isinstance(block, ListBlock)
Example #9
Source File: streamfield.py From wagtail-torchbox with MIT License | 5 votes |
def serialise_block(self, block, value): if hasattr(block, 'to_graphql_representation'): return block.to_graphql_representation(value) elif isinstance(block, blocks.RichTextBlock): return serialize_rich_text(value.source) elif isinstance(block, EmbedBlock): try: embed = get_embed(value.url) return { 'html': embed.html, 'url': value.url, } except EmbedException: return { 'html': '', 'url': None } elif isinstance(block, ImageChooserBlock): # FIXME return { 'id': value.id, 'alt': value.title, 'src': settings.MEDIA_PREFIX + value.file.url, 'hash': value.get_file_hash() } elif isinstance(block, blocks.FieldBlock): return value elif isinstance(block, blocks.StructBlock): return self.serialise_struct_block(block, value) elif isinstance(block, blocks.ListBlock): return self.serialise_list_block(block, value) elif isinstance(block, blocks.StreamBlock): return self.serialise_stream_block(block, value)
Example #10
Source File: list_block.py From wagtail-react-streamfield with BSD 3-Clause "New" or "Revised" License | 5 votes |
def clean(self, value): result = [] errors = [] for child_val in value: try: result.append(self.child_block.clean(child_val)) except ValidationError as e: errors.append(ErrorList([e])) else: errors.append(None) if any(errors): raise ValidationError('Validation error in ListBlock', params=errors) if self.meta.min_num is not None and self.meta.min_num > len(value): raise ValidationError( _('The minimum number of items is %d') % self.meta.min_num ) elif self.required and len(value) == 0: raise ValidationError(_('This field is required.')) if self.meta.max_num is not None and self.meta.max_num < len(value): raise ValidationError( _('The maximum number of items is %d') % self.meta.max_num ) return result
Example #11
Source File: list_block.py From wagtail-react-streamfield with BSD 3-Clause "New" or "Revised" License | 5 votes |
def definition(self): definition = super(ListBlock, self).definition definition.update( children=[self.child_block.definition], minNum=self.meta.min_num, maxNum=self.meta.max_num, ) html = self.get_instance_html([]) if html is not None: definition['html'] = html return definition
Example #12
Source File: wagtailstreamforms_fields.py From wagtailstreamforms with MIT License | 5 votes |
def get_form_block(self): return blocks.StructBlock( [ ("label", blocks.CharBlock()), ("help_text", blocks.CharBlock(required=False)), ("required", blocks.BooleanBlock(required=False)), ("choices", blocks.ListBlock(blocks.CharBlock(label="Option"))), ], icon=self.icon, label=self.label, )
Example #13
Source File: wagtailstreamforms_fields.py From wagtailstreamforms with MIT License | 5 votes |
def get_form_block(self): return blocks.StructBlock( [ ("label", blocks.CharBlock()), ("help_text", blocks.CharBlock(required=False)), ("required", blocks.BooleanBlock(required=False)), ("choices", blocks.ListBlock(blocks.CharBlock(label="Option"))), ], icon=self.icon, label=self.label, )
Example #14
Source File: wagtailstreamforms_fields.py From wagtailstreamforms with MIT License | 5 votes |
def get_form_block(self): return blocks.StructBlock( [ ("label", blocks.CharBlock()), ("help_text", blocks.CharBlock(required=False)), ("required", blocks.BooleanBlock(required=False)), ("choices", blocks.ListBlock(blocks.CharBlock(label="Option"))), ], icon=self.icon, label=self.label, )
Example #15
Source File: wagtailstreamforms_fields.py From wagtailstreamforms with MIT License | 5 votes |
def get_form_block(self): return blocks.StructBlock( [ ("label", blocks.CharBlock()), ("help_text", blocks.CharBlock(required=False)), ("required", blocks.BooleanBlock(required=False)), ("empty_label", blocks.CharBlock(required=False)), ("choices", blocks.ListBlock(blocks.CharBlock(label="Option"))), ], icon=self.icon, label=self.label, )
Example #16
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_calls_child_bulk_to_python_when_available(self): page_ids = [2, 3, 4, 5] expected_pages = Page.objects.filter(pk__in=page_ids) block = blocks.ListBlock(blocks.PageChooserBlock()) with self.assertNumQueries(1): pages = block.to_python(page_ids) self.assertSequenceEqual(pages, expected_pages)
Example #17
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_with_classname_via_class_meta(self): """form_classname from meta to be used as an additional class when rendering list block""" class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() class CustomListBlock(blocks.ListBlock): class Meta: form_classname = 'custom-list-class' block = CustomListBlock(LinkBlock) html = block.render_form([ { 'title': "Wagtail", 'link': 'http://www.wagtail.io', }, { 'title': "Django", 'link': 'http://www.djangoproject.com', }, ], prefix='links') # including leading space to ensure class name gets added correctly self.assertEqual(html.count(' custom-list-class'), 1)
Example #18
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialise_with_class(self): block = blocks.ListBlock(blocks.CharBlock) # Child block should be initialised for us self.assertIsInstance(block.child_block, blocks.CharBlock)
Example #19
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_ordering_in_form_submission_is_numeric(self): block = blocks.ListBlock(blocks.CharBlock()) # check that items are ordered by 'order' numerically, not alphabetically post_data = {'shoppinglist-count': '12'} for i in range(0, 12): post_data.update({ 'shoppinglist-%d-deleted' % i: '', 'shoppinglist-%d-order' % i: str(i), 'shoppinglist-%d-value' % i: "item %d" % i }) block_value = block.value_from_datadict(post_data, {}, 'shoppinglist') self.assertEqual(block_value[2], "item 2")
Example #20
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_ordering_in_form_submission_uses_order_field(self): block = blocks.ListBlock(blocks.CharBlock()) # check that items are ordered by the 'order' field, not the order they appear in the form post_data = {'shoppinglist-count': '3'} for i in range(0, 3): post_data.update({ 'shoppinglist-%d-deleted' % i: '', 'shoppinglist-%d-order' % i: str(2 - i), 'shoppinglist-%d-value' % i: "item %d" % i }) block_value = block.value_from_datadict(post_data, {}, 'shoppinglist') self.assertEqual(block_value[2], "item 0")
Example #21
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_value_omitted_from_data(self): block = blocks.ListBlock(blocks.CharBlock()) # overall value is considered present in the form if the 'count' field is present self.assertFalse(block.value_omitted_from_data({'mylist-count': '0'}, {}, 'mylist')) self.assertFalse(block.value_omitted_from_data({ 'mylist-count': '1', 'mylist-0-value': 'hello', 'mylist-0-deleted': '', 'mylist-0-order': '0' }, {}, 'mylist')) self.assertTrue(block.value_omitted_from_data({'nothing-here': 'nope'}, {}, 'mylist'))
Example #22
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_html_declaration_inheritance(self): class CharBlockWithDeclarations(blocks.CharBlock): def html_declarations(self): return '<script type="text/x-html-template">hello world</script>' block = blocks.ListBlock(CharBlockWithDeclarations()) self.assertIn('<script type="text/x-html-template">hello world</script>', block.all_html_declarations())
Example #23
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_media_inheritance(self): class ScriptedCharBlock(blocks.CharBlock): media = forms.Media(js=['scripted_char_block.js']) block = blocks.ListBlock(ScriptedCharBlock()) self.assertIn('scripted_char_block.js', ''.join(block.all_media().render_js()))
Example #24
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_passes_context_to_children(self): """ Template context passed to the render method should be passed on to the render method of the child block. """ block = blocks.ListBlock( blocks.CharBlock(template='tests/blocks/heading_block.html') ) html = block.render(["Bonjour le monde!", "Au revoir le monde!"], context={ 'language': 'fr', }) self.assertIn('<h1 lang="fr">Bonjour le monde!</h1>', html) self.assertIn('<h1 lang="fr">Au revoir le monde!</h1>', html)
Example #25
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_calls_block_render_on_children(self): """ The default rendering of a ListBlock should invoke the block's render method on each child, rather than just outputting the child value as a string. """ block = blocks.ListBlock( blocks.CharBlock(template='tests/blocks/heading_block.html') ) html = block.render(["Hello world!", "Goodbye world!"]) self.assertIn('<h1>Hello world!</h1>', html) self.assertIn('<h1>Goodbye world!</h1>', html)
Example #26
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialise_with_instance(self): child_block = blocks.CharBlock() block = blocks.ListBlock(child_block) self.assertEqual(block.child_block, child_block)