class ResourcesPage(Page): resources = StreamField( [ ( "resource", blocks.StreamBlock( [ ("resource_name", blocks.CharBlock()), ("information", blocks.RichTextBlock()), ( "structured_info_block", blocks.StreamBlock( [ ("heading", blocks.CharBlock()), ("body", blocks.RichTextBlock()), ] ), ), ] ), ) ], use_json_field=True, ) content_panels = Page.content_panels + [ FieldPanel("resources"), ]
class DocumentPage(Page): date_published = models.DateField("Last Updated", blank=True, null=True) body = StreamField( [( "section", blocks.StreamBlock([ ("header", CharBlock()), ("text", blocks.RichTextBlock()), ("quote", BlockQuoteBlock()), ( "subsection", blocks.StreamBlock([ ("header", CharBlock()), ("text", blocks.TextBlock()), ( "subsubsection", blocks.StreamBlock([ ("header", CharBlock()), ("text", blocks.TextBlock()), ]), ), ]), ), ]), )], null=True, blank=True, use_json_field=True, ) content_panels = Page.content_panels + [ FieldPanel("date_published"), FieldPanel("body"), ]
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)
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)
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)
class Migration(migrations.Migration): dependencies = [ ('tests', '0012_pagewithrelatedpages'), ] operations = [ migrations.AlterField( model_name='pagewithstreamfield', name='body', field=wagtail_fields.StreamField([('link_block', wagtail_blocks.StructBlock([('page', wagtail_blocks.PageChooserBlock(required=False)), ('text', wagtail_blocks.CharBlock(max_length=250))])), ('page', wagtail_blocks.PageChooserBlock()), ('stream', wagtail_blocks.StreamBlock([('page', wagtail_blocks.PageChooserBlock())])), ('rich_text', wagtail_blocks.RichTextBlock()), ('list_of_pages', wagtail_blocks.ListBlock(wagtail_blocks.PageChooserBlock()))], blank=True, verbose_name='Page body'), ), ]
class Migration(migrations.Migration): dependencies = [ ('wagtailcore', '0041_group_collection_permissions_verbose_name_plural'), ('tests', '0004_pagewithrichtext'), ] operations = [ migrations.CreateModel( name='PageWithStreamField', fields=[ ('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.Page')), ('body', wagtail_fields.StreamField([('link_block', wagtail_blocks.StructBlock([('page', wagtail_blocks.PageChooserBlock()), ('text', wagtail_blocks.CharBlock(max_length=250))])), ('page', wagtail_blocks.PageChooserBlock()), ('stream', wagtail_blocks.StreamBlock([('page', wagtail_blocks.PageChooserBlock())])), ('rich_text', wagtail_blocks.RichTextBlock()), ('list_of_pages', wagtail_blocks.ListBlock(wagtail_blocks.PageChooserBlock()))], blank=True, verbose_name='Page body')), ], options={ 'abstract': False, }, bases=('wagtailcore.page',), ), ]