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


Python blocks.CharBlock方法代码示例

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


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

示例1: test_render_within_structblock

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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_block_render_result_is_safe

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [as 别名]
def test_block_render_result_is_safe(self):
        """
        Ensure that any results of template rendering in block.render are marked safe
        so that they don't get double-escaped when inserted into a parent template (#2541)
        """
        stream_block = blocks.StreamBlock([
            ('paragraph', blocks.CharBlock(template='tests/jinja2/paragraph.html'))
        ])

        stream_value = stream_block.to_python([
            {'type': 'paragraph', 'value': 'hello world'},
        ])

        result = render_to_string('tests/jinja2/stream.html', {
            'value': stream_value,
        })

        self.assertIn('<p>hello world</p>', result) 
开发者ID:wagtail,项目名称:wagtail,代码行数:20,代码来源:test_jinja2.py

示例3: test_include_block_tag_with_streamvalue

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [as 别名]
def test_include_block_tag_with_streamvalue(self):
        """
        The include_block tag should be able to render a StreamValue's template
        while keeping the parent template's context
        """
        block = blocks.StreamBlock([
            ('heading', blocks.CharBlock(template='tests/jinja2/heading_block.html')),
            ('paragraph', blocks.CharBlock()),
        ], template='tests/jinja2/stream_with_language.html')

        stream_value = block.to_python([
            {'type': 'heading', 'value': 'Bonjour'}
        ])

        result = render_to_string('tests/jinja2/include_block_test.html', {
            'test_block': stream_value,
            'language': 'fr',
        })

        self.assertIn('<div class="heading" lang="fr"><h1 lang="fr">Bonjour</h1></div>', result) 
开发者ID:wagtail,项目名称:wagtail,代码行数:22,代码来源:test_jinja2.py

示例4: test_form_handling_is_independent_of_serialisation

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [as 别名]
def test_form_handling_is_independent_of_serialisation(self):
        class Base64EncodingCharBlock(blocks.CharBlock):
            """A CharBlock with a deliberately perverse JSON (de)serialisation format
            so that it visibly blows up if we call to_python / get_prep_value where we shouldn't"""

            def to_python(self, jsonish_value):
                # decode as base64 on the way out of the JSON serialisation
                return base64.b64decode(jsonish_value)

            def get_prep_value(self, native_value):
                # encode as base64 on the way into the JSON serialisation
                return base64.b64encode(native_value)

        block = Base64EncodingCharBlock()
        form_html = block.render_form('hello world', 'title')
        self.assertIn('value="hello world"', form_html)

        value_from_form = block.value_from_datadict({'title': 'hello world'}, {}, 'title')
        self.assertEqual('hello world', value_from_form) 
开发者ID:wagtail,项目名称:wagtail,代码行数:21,代码来源:test_blocks.py

示例5: test_meta_nested_inheritance

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [as 别名]
def test_meta_nested_inheritance(self):
        """
        Check that having a multi-level inheritance chain works
        """
        class HeadingBlock(blocks.CharBlock):
            class Meta:
                template = 'heading.html'
                test = 'Foo'

        class SubHeadingBlock(HeadingBlock):
            class Meta:
                template = 'subheading.html'

        block = SubHeadingBlock()
        self.assertEqual(block.meta.template, 'subheading.html')
        self.assertEqual(block.meta.test, 'Foo') 
开发者ID:wagtail,项目名称:wagtail,代码行数:18,代码来源:test_blocks.py

示例6: test_render

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例7: test_render_unknown_field

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例8: test_custom_render_form_template

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例9: test_custom_render_form_template_jinja

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例10: test_render_form_with_help_text

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例11: test_default_is_returned_as_structvalue

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例12: test_initialisation_from_subclass

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例13: test_value_property

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例14: test_render_with_template

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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

示例15: render

# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import CharBlock [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


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