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')
class SectionBlock(blocks.StructBlock): title = blocks.CharBlock() body = blocks.RichTextBlock() class Meta: icon = "form" template = 'tests/blocks/section_block.html'
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)
def test_include_block_tag_with_boundblock(self): """ The include_block tag should be able to render a BoundBlock's template while keeping the parent template's context """ block = blocks.CharBlock(template='tests/jinja2/heading_block.html') bound_block = block.bind('bonjour') result = render_to_string('tests/jinja2/include_block_test.html', { 'test_block': bound_block, 'language': 'fr', }) self.assertIn('<body><h1 lang="fr">bonjour</h1></body>', 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_filtered_value(self): """ The block parameter on include_block tag should support complex values including filters, e.g. {% include_block foo|default:123 %} """ block = blocks.CharBlock(template='tests/jinja2/heading_block.html') bound_block = block.bind('bonjour') result = render_to_string('tests/jinja2/include_block_test_with_filter.html', { 'test_block': bound_block, 'language': 'fr', }) self.assertIn('<body><h1 lang="fr">bonjour</h1></body>', result) result = render_to_string('tests/jinja2/include_block_test_with_filter.html', { 'test_block': None, 'language': 'fr', }) self.assertIn('<body>999</body>', result)
class LinkBlock(blocks.StructBlock): title = blocks.CharBlock() url = blocks.URLBlock() def get_context(self, value, parent_context=None): context = super(LinkBlock, self).get_context(value, parent_context) context['classname'] = parent_context['classname'] if value[ 'title'] == 'Torchbox' else 'normal' return context def get_form_context(self, value, prefix='', errors=None): context = super(LinkBlock, self).get_form_context(value, prefix=prefix, errors=errors) context['extra_var'] = "Hello from get_form_context!" return context class Meta: icon = "site" template = 'tests/blocks/link_block.html' form_template = 'tests/block_forms/link_block.html'
class InvalidStreamModel(models.Model): body = StreamField([ ('heading', blocks.CharBlock()), ('rich text', blocks.RichTextBlock()), ])