Python wagtail.embeds.blocks.EmbedBlock() Examples

The following are 11 code examples of wagtail.embeds.blocks.EmbedBlock(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module wagtail.embeds.blocks , or try the search function .
Example #1
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
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') 
Example #2
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def test_default(self):
        block1 = EmbedBlock()
        self.assertEqual(block1.get_default(), None)

        block2 = EmbedBlock(default='')
        self.assertEqual(block2.get_default(), None)

        block3 = EmbedBlock(default=None)
        self.assertEqual(block3.get_default(), None)

        block4 = EmbedBlock(default='http://www.example.com/foo')
        self.assertIsInstance(block4.get_default(), EmbedValue)
        self.assertEqual(block4.get_default().url, 'http://www.example.com/foo')

        block5 = EmbedBlock(default=EmbedValue('http://www.example.com/foo'))
        self.assertIsInstance(block5.get_default(), EmbedValue)
        self.assertEqual(block5.get_default().url, 'http://www.example.com/foo') 
Example #3
Source File: fields.py    From developer-portal with Mozilla Public License 2.0 6 votes vote down vote up
def __init__(self, *args, **kwargs):
        if "default" not in kwargs:
            kwargs["default"] = None

        super().__init__(
            [
                ("paragraph", RichTextBlock(features=RICH_TEXT_FEATURES)),
                ("image", ImageChooserBlock()),
                ("button", ButtonBlock()),
                ("embed", EmbedBlock()),
                (
                    "embed_html",
                    RawHTMLBlock(
                        help_text=(
                            "Warning: be careful what you paste here, since this "
                            "field could introduce XSS (or similar) bugs. "
                            "This field is meant solely for third-party embeds."
                        )
                    ),
                ),
                ("code_snippet", CodeSnippetBlock()),
            ],
            **kwargs
        ) 
Example #4
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_deserialize(self):
        """
        Deserialising the JSONish value of an EmbedBlock (a URL) should give us an EmbedValue
        for that URL
        """
        block = EmbedBlock(required=False)

        block_val = block.to_python('http://www.example.com/foo')
        self.assertIsInstance(block_val, EmbedValue)
        self.assertEqual(block_val.url, 'http://www.example.com/foo')

        # empty values should yield None
        empty_block_val = block.to_python('')
        self.assertEqual(empty_block_val, None) 
Example #5
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_serialize(self):
        block = EmbedBlock(required=False)

        block_val = EmbedValue('http://www.example.com/foo')
        serialized_val = block.get_prep_value(block_val)
        self.assertEqual(serialized_val, 'http://www.example.com/foo')

        serialized_empty_val = block.get_prep_value(None)
        self.assertEqual(serialized_empty_val, '') 
Example #6
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_render(self, get_embed):
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = EmbedBlock()
        block_val = block.to_python('http://www.example.com/foo')

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

        # Check that the embed was in the returned HTML
        self.assertIn('<h1>Hello world!</h1>', result)

        # Check that get_embed was called correctly
        get_embed.assert_any_call('http://www.example.com/foo') 
Example #7
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_render_form(self):
        """
        The form field for an EmbedBlock should be a text input containing
        the URL
        """
        block = EmbedBlock()

        form_html = block.render_form(EmbedValue('http://www.example.com/foo'), prefix='myembed')
        self.assertIn('<input ', form_html)
        self.assertIn('value="http://www.example.com/foo"', form_html) 
Example #8
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_clean_required(self, get_embed):
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = EmbedBlock()

        cleaned_value = block.clean(
            EmbedValue('https://www.youtube.com/watch?v=_U79Wc965vw'))
        self.assertIsInstance(cleaned_value, EmbedValue)
        self.assertEqual(cleaned_value.url,
                         'https://www.youtube.com/watch?v=_U79Wc965vw')

        with self.assertRaisesMessage(ValidationError, ''):
            block.clean(None) 
Example #9
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_clean_non_required(self, get_embed):
        get_embed.return_value = Embed(html='<h1>Hello world!</h1>')

        block = EmbedBlock(required=False)

        cleaned_value = block.clean(
            EmbedValue('https://www.youtube.com/watch?v=_U79Wc965vw'))
        self.assertIsInstance(cleaned_value, EmbedValue)
        self.assertEqual(cleaned_value.url,
                         'https://www.youtube.com/watch?v=_U79Wc965vw')

        cleaned_value = block.clean(None)
        self.assertIsNone(cleaned_value) 
Example #10
Source File: test_embeds.py    From wagtail with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def test_clean_invalid_url(self, get_embed):
        get_embed.side_effect = EmbedNotFoundException

        non_required_block = EmbedBlock(required=False)

        with self.assertRaises(ValidationError):
            non_required_block.clean(
                EmbedValue('http://no-oembed-here.com/something'))

        required_block = EmbedBlock()

        with self.assertRaises(ValidationError):
            required_block.clean(
                EmbedValue('http://no-oembed-here.com/something')) 
Example #11
Source File: streamfield.py    From wagtail-torchbox with MIT License 5 votes vote down vote up
def serialise_block(self, block, value):
        if hasattr(block, 'to_graphql_representation'):
            return block.to_graphql_representation(value)
        elif isinstance(block, blocks.RichTextBlock):
            return serialize_rich_text(value.source)
        elif isinstance(block, EmbedBlock):
            try:
                embed = get_embed(value.url)
                return {
                    'html': embed.html,
                    'url': value.url,
                }
            except EmbedException:
                return {
                    'html': '',
                    'url': None
                }
        elif isinstance(block, ImageChooserBlock):
            # FIXME
            return {
                'id': value.id,
                'alt': value.title,
                'src': settings.MEDIA_PREFIX + value.file.url,
                'hash': value.get_file_hash()
            }
        elif isinstance(block, blocks.FieldBlock):
            return value
        elif isinstance(block, blocks.StructBlock):
            return self.serialise_struct_block(block, value)
        elif isinstance(block, blocks.ListBlock):
            return self.serialise_list_block(block, value)
        elif isinstance(block, blocks.StreamBlock):
            return self.serialise_stream_block(block, value)