0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-11-29 17:36:49 +01:00

Fix #2789: Convert embed filter to templatetag (#2975)

* Add template tag for embedding while preserving the filter for now

* [FIX] Isort error in embed tests
This commit is contained in:
Janneke Janssen 2016-09-21 14:28:28 +02:00 committed by Karl Hobley
parent 71ce47bc5a
commit 361991a21b
3 changed files with 44 additions and 1 deletions

View File

@ -12,7 +12,7 @@ class EmbedValue(object):
Native value of an EmbedBlock. Should, at minimum, have a 'url' property
and render as the embed HTML when rendered in a template.
NB We don't use a wagtailembeds.model.Embed object for this, because
we want to be able to do {{ value.url|embed:max_width=500 }} without
we want to be able to do {% embed value.url 500 %} without
doing a redundant fetch of the embed at the default width.
"""
def __init__(self, url):

View File

@ -1,8 +1,11 @@
from __future__ import absolute_import, unicode_literals
import warnings
from django import template
from django.utils.safestring import mark_safe
from wagtail.utils.deprecation import RemovedInWagtail18Warning
from wagtail.wagtailembeds import embeds
from wagtail.wagtailembeds.exceptions import EmbedException
@ -11,6 +14,17 @@ register = template.Library()
@register.filter
def embed(url, max_width=None):
warnings.warn(
"The embed filter has been converted to a template tag. "
"Use {% embed my_embed_url %} instead.",
category=RemovedInWagtail18Warning, stacklevel=2
)
return embed_tag(url, max_width)
@register.simple_tag(name='embed')
def embed_tag(url, max_width=None):
try:
embed = embeds.get_embed(url, max_width=max_width)
return mark_safe(embed.html)

View File

@ -23,6 +23,7 @@ from wagtail.wagtailembeds.finders.oembed import oembed as wagtail_oembed
from wagtail.wagtailembeds.models import Embed
from wagtail.wagtailembeds.rich_text import MediaEmbedHandler
from wagtail.wagtailembeds.templatetags.wagtailembeds_tags import embed as embed_filter
from wagtail.wagtailembeds.templatetags.wagtailembeds_tags import embed_tag
try:
import embedly # noqa
@ -365,6 +366,34 @@ class TestEmbedFilter(TestCase):
self.assertEqual(result, '')
class TestEmbedTag(TestCase):
@patch('wagtail.wagtailembeds.embeds.get_embed')
def test_direct_call(self, get_embed):
get_embed.return_value = Embed(html='<img src="http://www.example.com" />')
result = embed_tag('http://www.youtube.com/watch/')
self.assertEqual(result, '<img src="http://www.example.com" />')
@patch('wagtail.wagtailembeds.embeds.get_embed')
def test_call_from_template(self, get_embed):
get_embed.return_value = Embed(html='<img src="http://www.example.com" />')
temp = template.Template('{% load wagtailembeds_tags %}{% embed "http://www.youtube.com/watch/" %}')
result = temp.render(template.Context())
self.assertEqual(result, '<img src="http://www.example.com" />')
@patch('wagtail.wagtailembeds.embeds.get_embed')
def test_catches_embed_not_found(self, get_embed):
get_embed.side_effect = EmbedNotFoundException
temp = template.Template('{% load wagtailembeds_tags %}{% embed "http://www.youtube.com/watch/" %}')
result = temp.render(template.Context())
self.assertEqual(result, '')
class TestEmbedBlock(TestCase):
def test_deserialize(self):
"""