Python wagtail.core.blocks.URLBlock() Examples
The following are 30
code examples of wagtail.core.blocks.URLBlock().
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 test_custom_render_form_template_jinja(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock(required=False) link = blocks.URLBlock(required=False) class Meta: form_template = 'tests/jinja2/struct_block_form_template.html' block = LinkBlock() html = block.render_form(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', }), prefix='mylink') self.assertIn('<div>Hello</div>', html) self.assertHTMLEqual('<div>Hello</div>', html) self.assertTrue(isinstance(html, SafeText))
Example #2
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_initialisation_from_subclass(self): class LinkStructValue(blocks.StructValue): def url(self): return self.get('page') or self.get('link') class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() page = blocks.PageChooserBlock(required=False) link = blocks.URLBlock(required=False) class Meta: value_class = LinkStructValue block = LinkBlock() self.assertEqual(list(block.child_blocks.keys()), ['title', 'page', 'link']) block_value = block.to_python({'title': 'Website', 'link': 'https://website.com'}) self.assertIsInstance(block_value, LinkStructValue) default_value = block.get_default() self.assertIsInstance(default_value, LinkStructValue)
Example #3
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 #4
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 #5
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 #6
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 #7
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_render_form_with_help_text(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() class Meta: help_text = "Self-promotion is encouraged" block = LinkBlock() html = block.render_form(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', }), prefix='mylink') self.assertInHTML('<div class="help"><span class="icon-help-inverse" aria-hidden="true"></span> Self-promotion is encouraged</div>', html) # check it can be overridden in the block constructor block = LinkBlock(help_text="Self-promotion is discouraged") html = block.render_form(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', }), prefix='mylink') self.assertInHTML('<div class="help"><span class="icon-help-inverse" aria-hidden="true"></span> Self-promotion is discouraged</div>', html)
Example #8
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_render_form_uses_default_value(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock(default="Torchbox") link = blocks.URLBlock(default="http://www.torchbox.com") block = LinkBlock() html = block.render_form(block.to_python({}), prefix='mylink') self.assertInHTML( '<input id="mylink-title" name="mylink-title" placeholder="Title" type="text" value="Torchbox" />', html ) self.assertInHTML( ( '<input id="mylink-link" name="mylink-link" placeholder="Link"' ' type="url" value="http://www.torchbox.com" />' ), html )
Example #9
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_render_with_classname_via_kwarg(self): """form_classname from kwargs to be used as an additional class when rendering list block""" class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = blocks.ListBlock(LinkBlock, form_classname='special-list-class') 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(' special-list-class'), 1)
Example #10
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_custom_render_form_template(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock(required=False) link = blocks.URLBlock(required=False) class Meta: form_template = 'tests/block_forms/struct_block_form_template.html' block = LinkBlock() html = block.render_form(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', }), prefix='mylink') self.assertIn('<div>Hello</div>', html) self.assertHTMLEqual('<div>Hello</div>', html) self.assertTrue(isinstance(html, SafeText))
Example #11
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_get_form_context(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = LinkBlock() context = block.get_form_context(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', }), prefix='mylink') self.assertTrue(isinstance(context['children'], collections.OrderedDict)) self.assertEqual(len(context['children']), 2) self.assertTrue(isinstance(context['children']['title'], blocks.BoundBlock)) self.assertEqual(context['children']['title'].value, "Wagtail site") self.assertTrue(isinstance(context['children']['link'], blocks.BoundBlock)) self.assertEqual(context['children']['link'].value, 'http://www.wagtail.io') self.assertEqual(context['block_definition'], block) self.assertEqual(context['prefix'], 'mylink')
Example #12
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_render_unknown_field(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = LinkBlock() html = block.render(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', 'image': 10, })) self.assertIn('<dt>title</dt>', html) self.assertIn('<dd>Wagtail site</dd>', html) self.assertIn('<dt>link</dt>', html) self.assertIn('<dd>http://www.wagtail.io</dd>', html) # Don't render the extra item self.assertNotIn('<dt>image</dt>', html)
Example #13
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_validation_errors(self): class ValidatedBlock(blocks.StreamBlock): char = blocks.CharBlock() url = blocks.URLBlock() block = ValidatedBlock() value = blocks.StreamValue(block, [ ('char', ''), ('char', 'foo'), ('url', 'http://example.com/'), ('url', 'not a url'), ]) with self.assertRaises(ValidationError) as catcher: block.clean(value) self.assertEqual(catcher.exception.params, { 0: ['This field is required.'], 3: ['Enter a valid URL.'], })
Example #14
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_min_num_validation_errors(self): class ValidatedBlock(blocks.StreamBlock): char = blocks.CharBlock() url = blocks.URLBlock() block = ValidatedBlock(min_num=1) value = blocks.StreamValue(block, []) with self.assertRaises(ValidationError) as catcher: block.clean(value) self.assertEqual(catcher.exception.params, { '__all__': ['The minimum number of items is 1'] }) # a value with >= 1 blocks should pass validation value = blocks.StreamValue(block, [('char', 'foo')]) self.assertTrue(block.clean(value))
Example #15
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_max_num_validation_errors(self): class ValidatedBlock(blocks.StreamBlock): char = blocks.CharBlock() url = blocks.URLBlock() block = ValidatedBlock(max_num=1) value = blocks.StreamValue(block, [ ('char', 'foo'), ('char', 'foo'), ('url', 'http://example.com/'), ('url', 'http://example.com/'), ]) with self.assertRaises(ValidationError) as catcher: block.clean(value) self.assertEqual(catcher.exception.params, { '__all__': ['The maximum number of items is 1'] }) # a value with 1 block should pass validation value = blocks.StreamValue(block, [('char', 'foo')]) self.assertTrue(block.clean(value))
Example #16
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_value_from_datadict(self): block = blocks.StructBlock([ ('title', blocks.CharBlock()), ('link', blocks.URLBlock()), ]) struct_val = block.value_from_datadict({ 'mylink-title': "Torchbox", 'mylink-link': "http://www.torchbox.com" }, {}, 'mylink') self.assertEqual(struct_val['title'], "Torchbox") self.assertEqual(struct_val['link'], "http://www.torchbox.com") self.assertTrue(isinstance(struct_val, blocks.StructValue)) self.assertTrue(isinstance(struct_val.bound_blocks['link'].block, blocks.URLBlock))
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_initialisation_with_mixins(self): class LinkStructValue(blocks.StructValue): pass class StylingMixinStructValue(blocks.StructValue): pass class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() class Meta: value_class = LinkStructValue class StylingMixin(blocks.StructBlock): classname = blocks.CharBlock() class StyledLinkBlock(StylingMixin, LinkBlock): source = blocks.CharBlock() block = StyledLinkBlock() self.assertEqual(list(block.child_blocks.keys()), ['title', 'link', 'classname', 'source']) block_value = block.to_python({ 'title': 'Website', 'link': 'https://website.com', 'source': 'google', 'classname': 'full-size', }) self.assertIsInstance(block_value, LinkStructValue)
Example #19
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_block_counts_max_validation_errors(self): class ValidatedBlock(blocks.StreamBlock): char = blocks.CharBlock() url = blocks.URLBlock() block = ValidatedBlock(block_counts={'char': {'max_num': 1}}) value = blocks.StreamValue(block, [ ('char', 'foo'), ('char', 'foo'), ('url', 'http://example.com/'), ('url', 'http://example.com/'), ]) with self.assertRaises(ValidationError) as catcher: block.clean(value) self.assertEqual(catcher.exception.params, { '__all__': ['Char: The maximum number of items is 1'] }) # a value with 1 char block should pass validation value = blocks.StreamValue(block, [ ('char', 'foo'), ('url', 'http://example.com/'), ('url', 'http://example.com/'), ]) self.assertTrue(block.clean(value))
Example #20
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialisation(self): class CustomStructValue(blocks.StructValue): def joined(self): return self.get('title', '') + self.get('link', '') block = blocks.StructBlock([ ('title', blocks.CharBlock()), ('link', blocks.URLBlock()), ], value_class=CustomStructValue) self.assertEqual(list(block.child_blocks.keys()), ['title', 'link']) block_value = block.to_python({'title': 'Birthday party', 'link': 'https://myparty.co.uk'}) self.assertIsInstance(block_value, CustomStructValue) default_value = block.get_default() self.assertIsInstance(default_value, CustomStructValue) value_from_datadict = block.value_from_datadict({ 'mylink-title': "Torchbox", 'mylink-link': "http://www.torchbox.com" }, {}, 'mylink') self.assertIsInstance(value_from_datadict, CustomStructValue) value = block.to_python({'title': 'Torchbox', 'link': 'http://www.torchbox.com/'}) clean_value = block.clean(value) self.assertTrue(isinstance(clean_value, CustomStructValue)) self.assertEqual(clean_value['title'], 'Torchbox') value = block.to_python({'title': 'Torchbox', 'link': 'not a url'}) with self.assertRaises(ValidationError): block.clean(value)
Example #21
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_clean(self): block = blocks.StructBlock([ ('title', blocks.CharBlock()), ('link', blocks.URLBlock()), ]) value = block.to_python({'title': 'Torchbox', 'link': 'http://www.torchbox.com/'}) clean_value = block.clean(value) self.assertTrue(isinstance(clean_value, blocks.StructValue)) self.assertEqual(clean_value['title'], 'Torchbox') value = block.to_python({'title': 'Torchbox', 'link': 'not a url'}) with self.assertRaises(ValidationError): block.clean(value)
Example #22
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.StructBlock([ ('title', blocks.CharBlock()), ('link', blocks.URLBlock()), ]) # overall value is considered present in the form if any sub-field is present self.assertFalse(block.value_omitted_from_data({'mylink-title': 'Torchbox'}, {}, 'mylink')) self.assertTrue(block.value_omitted_from_data({'nothing-here': 'nope'}, {}, 'mylink'))
Example #23
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialisation(self): block = blocks.StructBlock([ ('title', blocks.CharBlock()), ('link', blocks.URLBlock()), ]) self.assertEqual(list(block.child_blocks.keys()), ['title', 'link'])
Example #24
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>' class LinkBlock(blocks.StructBlock): title = CharBlockWithDeclarations(default="Torchbox") link = blocks.URLBlock(default="http://www.torchbox.com") block = LinkBlock() self.assertIn('<script type="text/x-html-template">hello world</script>', block.all_html_declarations())
Example #25
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']) class LinkBlock(blocks.StructBlock): title = ScriptedCharBlock(default="Torchbox") link = blocks.URLBlock(default="http://www.torchbox.com") block = LinkBlock() self.assertIn('scripted_char_block.js', ''.join(block.all_media().render_js()))
Example #26
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_form_unknown_field(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = LinkBlock() html = block.render_form(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', 'image': 10, }), prefix='mylink') self.assertInHTML( ( '<input id="mylink-title" name="mylink-title" placeholder="Title"' ' type="text" value="Wagtail site" />' ), html ) self.assertInHTML( ( '<input id="mylink-link" name="mylink-link" placeholder="Link" type="url"' ' value="http://www.wagtail.io" />' ), html ) # Don't render the extra field self.assertNotIn('mylink-image', html)
Example #27
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_render_form(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock(required=False) link = blocks.URLBlock(required=False) block = LinkBlock() html = block.render_form(block.to_python({ 'title': "Wagtail site", 'link': 'http://www.wagtail.io', }), prefix='mylink') self.assertIn('<div class="struct-block">', html) self.assertIn('<div class="field char_field widget-text_input fieldname-title">', html) self.assertIn('<label class="field__label" for="mylink-title">Title</label>', html) self.assertInHTML( '<input id="mylink-title" name="mylink-title" placeholder="Title" type="text" value="Wagtail site" />', html ) self.assertIn('<div class="field url_field widget-url_input fieldname-link">', html) self.assertInHTML( ( '<input id="mylink-link" name="mylink-link" placeholder="Link"' ' type="url" value="http://www.wagtail.io" />' ), html ) self.assertNotIn('<li class="required">', html)
Example #28
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialisation_with_mixins(self): """ The order of fields of classes with multiple parent classes is slightly surprising at first. Child fields are inherited in a bottom-up order, by traversing the MRO in reverse. In the example below, ``StyledLinkBlock`` will have an MRO of:: [StyledLinkBlock, StylingMixin, LinkBlock, StructBlock, ...] This will result in ``classname`` appearing *after* ``title`` and ``link`` in ``StyleLinkBlock`.child_blocks`, even though ``StylingMixin`` appeared before ``LinkBlock``. """ class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() class StylingMixin(blocks.StructBlock): classname = blocks.CharBlock() class StyledLinkBlock(StylingMixin, LinkBlock): source = blocks.CharBlock() block = StyledLinkBlock() self.assertEqual(list(block.child_blocks.keys()), ['title', 'link', 'classname', 'source'])
Example #29
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialisation_with_multiple_subclassses(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() class StyledLinkBlock(LinkBlock): classname = blocks.CharBlock() block = StyledLinkBlock() self.assertEqual(list(block.child_blocks.keys()), ['title', 'link', 'classname'])
Example #30
Source File: test_blocks.py From wagtail with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_initialisation_from_subclass_with_extra(self): class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() link = blocks.URLBlock() block = LinkBlock([ ('classname', blocks.CharBlock()) ]) self.assertEqual(list(block.child_blocks.keys()), ['title', 'link', 'classname'])