本文整理汇总了Python中wagtail.core.blocks.StreamBlock方法的典型用法代码示例。如果您正苦于以下问题:Python blocks.StreamBlock方法的具体用法?Python blocks.StreamBlock怎么用?Python blocks.StreamBlock使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类wagtail.core.blocks
的用法示例。
在下文中一共展示了blocks.StreamBlock方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_block_render_result_is_safe
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [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)
示例2: test_rich_text_is_safe
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_rich_text_is_safe(self):
"""
Ensure that RichText values are marked safe
so that they don't get double-escaped when inserted into a parent template (#2542)
"""
stream_block = blocks.StreamBlock([
('paragraph', blocks.RichTextBlock(template='tests/jinja2/rich_text.html'))
])
stream_value = stream_block.to_python([
{'type': 'paragraph', 'value': '<p>Merry <a linktype="page" id="4">Christmas</a>!</p>'},
])
result = render_to_string('tests/jinja2/stream.html', {
'value': stream_value,
})
self.assertIn('<p>Merry <a href="/events/christmas/">Christmas</a>!</p>', result)
示例3: test_include_block_tag_with_streamvalue
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [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)
示例4: test_render_passes_context_to_children
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_render_passes_context_to_children(self):
block = blocks.StreamBlock([
('heading', blocks.CharBlock(template='tests/blocks/heading_block.html')),
('paragraph', blocks.CharBlock()),
])
value = block.to_python([
{'type': 'heading', 'value': 'Bonjour'}
])
html = block.render(value, context={
'language': 'fr',
})
self.assertIn('<div class="block-heading"><h1 lang="fr">Bonjour</h1></div>', html)
# calling render_as_block(context=foo) on value (a StreamValue instance)
# should be equivalent to block.render(value, context=foo)
html = value.render_as_block(context={
'language': 'fr',
})
self.assertIn('<div class="block-heading"><h1 lang="fr">Bonjour</h1></div>', html)
示例5: test_render_on_stream_child_uses_child_template
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_render_on_stream_child_uses_child_template(self):
"""
Accessing a child element of the stream (giving a StreamChild object) and rendering it
should use the block template, not just render the value's string representation
"""
block = blocks.StreamBlock([
('heading', blocks.CharBlock(template='tests/blocks/heading_block.html')),
('paragraph', blocks.CharBlock()),
])
value = block.to_python([
{'type': 'heading', 'value': 'Hello'}
])
html = value[0].render()
self.assertEqual('<h1>Hello</h1>', html)
# StreamChild.__str__ should do the same
html = str(value[0])
self.assertEqual('<h1>Hello</h1>', html)
# and so should StreamChild.render_as_block
html = value[0].render_as_block()
self.assertEqual('<h1>Hello</h1>', html)
示例6: render_form
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def render_form(self):
class ArticleBlock(blocks.StreamBlock):
heading = blocks.CharBlock()
paragraph = blocks.CharBlock()
block = ArticleBlock()
value = block.to_python([
{
'type': 'heading',
'value': "My title",
'id': '123123123',
},
{
'type': 'paragraph',
'value': 'My first paragraph',
},
{
'type': 'paragraph',
'value': 'My second paragraph',
},
])
return block.render_form(value, prefix='myarticle')
示例7: test_min_num_validation_errors
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
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))
示例8: test_max_num_validation_errors
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
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))
示例9: test_block_counts_min_validation_errors
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_block_counts_min_validation_errors(self):
class ValidatedBlock(blocks.StreamBlock):
char = blocks.CharBlock()
url = blocks.URLBlock()
block = ValidatedBlock(block_counts={'char': {'min_num': 1}})
value = blocks.StreamValue(block, [
('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 minimum number of items is 1']
})
# a value with 1 char block should pass validation
value = blocks.StreamValue(block, [
('url', 'http://example.com/'),
('char', 'foo'),
('url', 'http://example.com/'),
])
self.assertTrue(block.clean(value))
示例10: test_ordering_in_form_submission_uses_order_field
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_ordering_in_form_submission_uses_order_field(self):
class ArticleBlock(blocks.StreamBlock):
heading = blocks.CharBlock()
paragraph = blocks.CharBlock()
block = ArticleBlock()
# check that items are ordered by the 'order' field, not the order they appear in the form
post_data = {'article-count': '3'}
for i in range(0, 3):
post_data.update({
'article-%d-deleted' % i: '',
'article-%d-order' % i: str(2 - i),
'article-%d-type' % i: 'heading',
'article-%d-value' % i: "heading %d" % i,
'article-%d-id' % i: "000%d" % i,
})
block_value = block.value_from_datadict(post_data, {}, 'article')
self.assertEqual(block_value[2].value, "heading 0")
self.assertEqual(block_value[2].id, "0000")
示例11: test_ordering_in_form_submission_is_numeric
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_ordering_in_form_submission_is_numeric(self):
class ArticleBlock(blocks.StreamBlock):
heading = blocks.CharBlock()
paragraph = blocks.CharBlock()
block = ArticleBlock()
# check that items are ordered by 'order' numerically, not alphabetically
post_data = {'article-count': '12'}
for i in range(0, 12):
post_data.update({
'article-%d-deleted' % i: '',
'article-%d-order' % i: str(i),
'article-%d-type' % i: 'heading',
'article-%d-value' % i: "heading %d" % i
})
block_value = block.value_from_datadict(post_data, {}, 'article')
self.assertEqual(block_value[2].value, "heading 2")
示例12: test_constructor_default
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_constructor_default(self):
"""Test that we can specify a default value in the constructor of a StreamBlock"""
class ArticleBlock(blocks.StreamBlock):
heading = blocks.CharBlock()
paragraph = blocks.CharBlock()
class Meta:
default = [('heading', 'A default heading')]
# to access the default value, we retrieve it through a StructBlock
# from a struct value that's missing that key
class ArticleContainerBlock(blocks.StructBlock):
author = blocks.CharBlock()
article = ArticleBlock(default=[('heading', 'A different default heading')])
block = ArticleContainerBlock()
struct_value = block.to_python({'author': 'Bob'})
stream_value = struct_value['article']
self.assertTrue(isinstance(stream_value, blocks.StreamValue))
self.assertEqual(len(stream_value), 1)
self.assertEqual(stream_value[0].block_type, 'heading')
self.assertEqual(stream_value[0].value, 'A different default heading')
示例13: check_get_prep_value
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def check_get_prep_value(self, stream_data, is_lazy):
class ArticleBlock(blocks.StreamBlock):
heading = blocks.CharBlock()
paragraph = blocks.CharBlock()
block = ArticleBlock()
value = blocks.StreamValue(block, stream_data, is_lazy=is_lazy)
jsonish_value = block.get_prep_value(value)
self.assertEqual(len(jsonish_value), 2)
self.assertEqual(jsonish_value[0], {'type': 'heading', 'value': 'this is my heading', 'id': '0000'})
self.assertEqual(jsonish_value[1]['type'], 'paragraph')
self.assertEqual(jsonish_value[1]['value'], '<p>this is a paragraph</p>')
# get_prep_value should assign a new (random and non-empty)
# ID to this block, as it didn't have one already.
self.assertTrue(jsonish_value[1]['id'])
# Calling get_prep_value again should preserve existing IDs, including the one
# just assigned to block 1
jsonish_value_again = block.get_prep_value(value)
self.assertEqual(jsonish_value[0]['id'], jsonish_value_again[0]['id'])
self.assertEqual(jsonish_value[1]['id'], jsonish_value_again[1]['id'])
示例14: test_render_with_classname_via_kwarg
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_render_with_classname_via_kwarg(self):
"""form_classname from kwargs to be used as an additional class when rendering stream block"""
block = blocks.StreamBlock([
(b'heading', blocks.CharBlock()),
(b'paragraph', blocks.CharBlock()),
], form_classname='rocket-section')
value = block.to_python([
{
'type': 'heading',
'value': "Falcon Heavy",
'id': '2',
},
{
'type': 'paragraph',
'value': "Ultra heavy launch capability",
'id': '3',
}
])
html = block.render_form(value)
# including leading space to ensure class name gets added correctly
self.assertEqual(html.count(' rocket-section'), 1)
示例15: test_render_with_classname_via_class_meta
# 需要导入模块: from wagtail.core import blocks [as 别名]
# 或者: from wagtail.core.blocks import StreamBlock [as 别名]
def test_render_with_classname_via_class_meta(self):
"""form_classname from meta to be used as an additional class when rendering stream block"""
class ProfileBlock(blocks.StreamBlock):
username = blocks.CharBlock()
class Meta:
form_classname = 'profile-block-large'
block = ProfileBlock()
value = block.to_python([
{
'type': 'username',
'value': "renegadeM@ster",
'id': '789',
}
])
html = block.render_form(value, prefix='profiles')
# including leading space to ensure class name gets added correctly
self.assertEqual(html.count(' profile-block-large'), 1)