当前位置: 首页>>代码示例>>Python>>正文


Python blocks.StructBlock方法代码示例

本文整理汇总了Python中wagtail.core.blocks.StructBlock方法的典型用法代码示例。如果您正苦于以下问题:Python blocks.StructBlock方法的具体用法?Python blocks.StructBlock怎么用?Python blocks.StructBlock使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在wagtail.core.blocks的用法示例。


在下文中一共展示了blocks.StructBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: test_render_within_structblock

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
def test_render_within_structblock(self, get_embed):
        """
        When rendering the value of an EmbedBlock directly in a template
        (as happens when accessing it as a child of a StructBlock), the
        proper embed output should be rendered, not the URL.
        """
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = blocks.StructBlock([
            ('title', blocks.CharBlock()),
            ('embed', EmbedBlock()),
        ])

        block_val = block.to_python({'title': 'A test', 'embed': 'http://www.example.com/foo'})

        temp = template.Template('embed: {{ self.embed }}')
        context = template.Context({'self': block_val})
        result = temp.render(context)

        self.assertIn('<h1>Hello world!</h1>', result)

        # Check that get_embed was called correctly
        get_embed.assert_any_call('http://www.example.com/foo') 
开发者ID:wagtail,项目名称:wagtail,代码行数:25,代码来源:test_embeds.py

示例2: test_render

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
def test_render(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',
        }))
        expected_html = '\n'.join([
            '<dl>',
            '<dt>title</dt>',
            '<dd>Wagtail site</dd>',
            '<dt>link</dt>',
            '<dd>http://www.wagtail.io</dd>',
            '</dl>',
        ])

        self.assertHTMLEqual(html, expected_html) 
开发者ID:wagtail,项目名称:wagtail,代码行数:22,代码来源:test_blocks.py

示例3: test_render_unknown_field

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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) 
开发者ID:wagtail,项目名称:wagtail,代码行数:21,代码来源:test_blocks.py

示例4: test_custom_render_form_template

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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)) 
开发者ID:wagtail,项目名称:wagtail,代码行数:19,代码来源:test_blocks.py

示例5: test_custom_render_form_template_jinja

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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)) 
开发者ID:wagtail,项目名称:wagtail,代码行数:19,代码来源:test_blocks.py

示例6: test_render_form_with_help_text

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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) 
开发者ID:wagtail,项目名称:wagtail,代码行数:26,代码来源:test_blocks.py

示例7: test_default_is_returned_as_structvalue

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
def test_default_is_returned_as_structvalue(self):
        """When returning the default value of a StructBlock (e.g. because it's
        a child of another StructBlock, and the outer value is missing that key)
        we should receive it as a StructValue, not just a plain dict"""
        class PersonBlock(blocks.StructBlock):
            first_name = blocks.CharBlock()
            surname = blocks.CharBlock()

        class EventBlock(blocks.StructBlock):
            title = blocks.CharBlock()
            guest_speaker = PersonBlock(default={'first_name': 'Ed', 'surname': 'Balls'})

        event_block = EventBlock()

        event = event_block.to_python({'title': 'Birthday party'})

        self.assertEqual(event['guest_speaker']['first_name'], 'Ed')
        self.assertTrue(isinstance(event['guest_speaker'], blocks.StructValue)) 
开发者ID:wagtail,项目名称:wagtail,代码行数:20,代码来源:test_blocks.py

示例8: test_initialisation_from_subclass

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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) 
开发者ID:wagtail,项目名称:wagtail,代码行数:25,代码来源:test_blocks.py

示例9: test_value_property

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
def test_value_property(self):

        class SectionStructValue(blocks.StructValue):
            @property
            def foo(self):
                return 'bar %s' % self.get('title', '')

        class SectionBlock(blocks.StructBlock):
            title = blocks.CharBlock()
            body = blocks.RichTextBlock()

            class Meta:
                value_class = SectionStructValue

        block = SectionBlock()
        struct_value = block.to_python({'title': 'hello', 'body': '<b>world</b>'})
        value = struct_value.foo
        self.assertEqual(value, 'bar hello') 
开发者ID:wagtail,项目名称:wagtail,代码行数:20,代码来源:test_blocks.py

示例10: test_render_with_template

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
def test_render_with_template(self):

        class SectionStructValue(blocks.StructValue):
            def title_with_suffix(self):
                title = self.get('title')
                if title:
                    return 'SUFFIX %s' % title
                return 'EMPTY TITLE'

        class SectionBlock(blocks.StructBlock):
            title = blocks.CharBlock(required=False)

            class Meta:
                value_class = SectionStructValue

        block = SectionBlock(template='tests/blocks/struct_block_custom_value.html')
        struct_value = block.to_python({'title': 'hello'})
        html = block.render(struct_value)
        self.assertEqual(html, '<div>SUFFIX hello</div>\n')

        struct_value = block.to_python({})
        html = block.render(struct_value)
        self.assertEqual(html, '<div>EMPTY TITLE</div>\n') 
开发者ID:wagtail,项目名称:wagtail,代码行数:25,代码来源:test_blocks.py

示例11: render

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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',
            },
        ]) 
开发者ID:wagtail,项目名称:wagtail,代码行数:18,代码来源:test_blocks.py

示例12: render_form

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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 
开发者ID:wagtail,项目名称:wagtail,代码行数:21,代码来源:test_blocks.py

示例13: test_html_declarations

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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
        ) 
开发者ID:wagtail,项目名称:wagtail,代码行数:18,代码来源:test_blocks.py

示例14: test_searchable_content

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
def test_searchable_content(self):
        class LinkBlock(blocks.StructBlock):
            title = blocks.CharBlock()
            link = blocks.URLBlock()

        block = blocks.ListBlock(LinkBlock())
        content = block.get_searchable_content([
            {
                'title': "Wagtail",
                'link': 'http://www.wagtail.io',
            },
            {
                'title': "Django",
                'link': 'http://www.djangoproject.com',
            },
        ])

        self.assertEqual(content, ["Wagtail", "Django"]) 
开发者ID:wagtail,项目名称:wagtail,代码行数:20,代码来源:test_blocks.py

示例15: test_default_default

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StructBlock [as 别名]
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) 
开发者ID:wagtail,项目名称:wagtail,代码行数:20,代码来源:test_blocks.py


注:本文中的wagtail.core.blocks.StructBlock方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。