def test_get_embed(self): embed = get_embed('www.test.com/1234', max_width=400, finder=self.dummy_finder) # Check that the embed is correct self.assertEqual(embed.title, "Test: www.test.com/1234") self.assertEqual(embed.type, 'video') self.assertEqual(embed.width, 400) # Check ratio calculations self.assertEqual(embed.ratio, 480 / 400) self.assertEqual(embed.ratio_css, '120.0%') self.assertTrue(embed.is_responsive) # Check that there has only been one hit to the backend self.assertEqual(self.hit_count, 1) # Look for the same embed again and check the hit count hasn't increased embed = get_embed('www.test.com/1234', max_width=400, finder=self.dummy_finder) self.assertEqual(self.hit_count, 1) # Look for a different embed, hit count should increase embed = get_embed('www.test.com/4321', max_width=400, finder=self.dummy_finder) self.assertEqual(self.hit_count, 2) # Look for the same embed with a different width, this should also increase hit count embed = get_embed('www.test.com/4321', finder=self.dummy_finder) self.assertEqual(self.hit_count, 3)
def test_get_embed(self): embed = get_embed('www.test.com/1234', max_width=400, finder=self.dummy_finder) # Check that the embed is correct self.assertEqual(embed.title, "Test: www.test.com/1234") self.assertEqual(embed.type, 'video') self.assertEqual(embed.width, 400) # Check that there has only been one hit to the backend self.assertEqual(self.hit_count, 1) # Look for the same embed again and check the hit count hasn't increased embed = get_embed('www.test.com/1234', max_width=400, finder=self.dummy_finder) self.assertEqual(self.hit_count, 1) # Look for a different embed, hit count should increase embed = get_embed('www.test.com/4321', max_width=400, finder=self.dummy_finder) self.assertEqual(self.hit_count, 2) # Look for the same embed with a different width, this should also increase hit count embed = get_embed('www.test.com/4321', finder=self.dummy_finder) self.assertEqual(self.hit_count, 3)
def test_invalid_width(self): embed = get_embed('www.test.com/1234', max_width=400, finder=self.dummy_finder_invalid_width) # Width must be set to None self.assertEqual(embed.width, None)
def embed_to_editor_html(url): embed = embeds.get_embed(url) # catching EmbedException is the responsibility of the caller # Render template return render_to_string('wagtailembeds/embed_editor.html', { 'embed': embed, })
def embed(url, max_width=None): embed = get_embed(url, max_width=max_width) try: if embed is not None: return mark_safe(embed.html) else: return '' except: return ''
def embed_to_editor_html(url): embed = get_embed(url) if embed is None: return # Render template return render_to_string('wagtailembeds/embed_editor.html', { 'embed': embed, })
def embed_to_frontend_html(url): try: embed = embeds.get_embed(url) # Render template return render_to_string('wagtailembeds/embed_frontend.html', { 'embed': embed, }) except EmbedException: # silently ignore failed embeds, rather than letting them crash the page return ''
def embed_to_editor_html(url): try: embed = embeds.get_embed(url) # Render template return render_to_string('wagtailembeds/embed_editor.html', { 'embed': embed, }) except embeds.EmbedException: # Could be replaced with a nice error message return ''
def test_no_html(self): def no_html_finder(url, max_width=None): """ A finder which returns everything but HTML """ embed = self.dummy_finder(url, max_width) embed['html'] = None return embed embed = get_embed('www.test.com/1234', max_width=400, finder=no_html_finder) self.assertEqual(embed.html, '')
def test_get_embed(self): embed = get_embed("www.test.com/1234", max_width=400, finder=self.dummy_finder) # Check that the embed is correct self.assertEqual(embed.title, "Test: www.test.com/1234") self.assertEqual(embed.type, "video") self.assertEqual(embed.width, 400) # Check that there has only been one hit to the backend self.assertEqual(self.hit_count, 1) # Look for the same embed again and check the hit count hasn't increased embed = get_embed("www.test.com/1234", max_width=400, finder=self.dummy_finder) self.assertEqual(self.hit_count, 1) # Look for a different embed, hit count should increase embed = get_embed("www.test.com/4321", max_width=400, finder=self.dummy_finder) self.assertEqual(self.hit_count, 2) # Look for the same embed with a different width, this should also increase hit count embed = get_embed("www.test.com/4321", finder=self.dummy_finder) self.assertEqual(self.hit_count, 3)
def clean(self): from girleffect.utils.blocks import validate_hex if self.hero_strapline_hex: if not validate_hex(self.hero_strapline_hex): raise ValidationError( {'hero_strapline_hex': _('Please enter a valid hex code')}) # Validating if URL is a valid YouTube URL youtube_embed = self.link_youtube if youtube_embed: youtube_finder = OEmbedFinder(providers=[oembed_providers.youtube]) if not youtube_finder.accept(youtube_embed): raise ValidationError( {'link_youtube': _('Please supply a valid YouTube URL.')}) else: try: embed = get_embed(youtube_embed) self.link_youtube_html = embed.html except EmbedException: raise ValidationError( {'link_youtube': _('Embed cannot be found.')}) # Validating links populated_fields = [] for link_field in [self.link_page, self.link_youtube]: if link_field: populated_fields.append(link_field) # Only only one or less fields can be selected if len(populated_fields) > 1: error_message = 'Please choose only one of Link Page or Link YouTube as destination.' raise ValidationError({ 'link_page': error_message, 'link_youtube': error_message }) # Link fields should have link text if len(populated_fields) >= 1 and not self.link_text: raise ValidationError({ 'link_text': 'Link text is required if link destination has been selected' }) return super(HeroVideoFields, self).clean()
def embed_to_frontend_html(url): try: embed = embeds.get_embed(url) # Work out ratio if embed.width and embed.height: ratio = str(embed.height / embed.width * 100) + "%" else: ratio = "0" # Render template return render_to_string('wagtailembeds/embed_frontend.html', { 'embed': embed, 'ratio': ratio, }) except embeds.EmbedException: return ''
def embed_to_frontend_html(url): try: embed = embeds.get_embed(url) # Work out ratio if embed.width and embed.height: ratio = str(embed.height / embed.width * 100) + "%" else: ratio = "0" # Render template return render_to_string('wagtailembeds/embed_frontend.html', { 'embed': embed, 'ratio': ratio, }) except EmbedException: # silently ignore failed embeds, rather than letting them crash the page return ''
def data(self): data = {'title': self.title} prev_page = self.prev_page data['prev_url'] = prev_page.url if prev_page else None next_page = self.next_page data['next_url'] = next_page.url if next_page else None data['embed_url'] = self.embed_url try: # use default SoundCloud embed style for dispatches; # bypass overrides in our custom finder in localore/embeds.py data['embed_html'] = embeds.get_embed( self.embed_url, finder=get_default_finder()).html except EmbedException: data['embed_html'] = '' return data
def data(self): data = { 'title': self.title } prev_page = self.prev_page data['prev_url'] = prev_page.url if prev_page else None next_page = self.next_page data['next_url'] = next_page.url if next_page else None data['embed_url'] = self.embed_url try: # use default SoundCloud embed style for dispatches; # bypass overrides in our custom finder in localore/embeds.py data['embed_html'] = embeds.get_embed( self.embed_url, finder=get_default_finder() ).html except EmbedException: data['embed_html'] = '' return data
def test_no_finders_available(self): with self.assertRaises(EmbedUnsupportedProviderException): get_embed('www.test.com/1234', max_width=400)
def get_embed(url, max_width=None): try: return embeds.get_embed(url, max_width=max_width) except EmbedException: return ''
def embed(url, max_width=None): try: embed = embeds.get_embed(url, max_width=max_width) return mark_safe(embed.html) except embeds.EmbedException: return ''