mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-04 21:44:44 +00:00
Moved embed finders into separate modules
This commit is contained in:
parent
8afe69fd72
commit
40778b538d
11 changed files with 132 additions and 118 deletions
|
|
@ -97,7 +97,7 @@ class TestExpandDbHtml(TestCase):
|
|||
result = expand_db_html(html)
|
||||
self.assertEqual(result, '<a id="1">foo</a>')
|
||||
|
||||
@patch('wagtail.wagtailembeds.embeds.oembed')
|
||||
@patch('wagtail.wagtailembeds.finders.oembed.find_embed')
|
||||
def test_expand_db_html_with_embed(self, oembed):
|
||||
oembed.return_value = {
|
||||
'title': 'test title',
|
||||
|
|
|
|||
|
|
@ -1,115 +1,12 @@
|
|||
from datetime import datetime
|
||||
import json
|
||||
|
||||
# Needs to be imported like this to allow @patch to work in tests
|
||||
from django.utils.six.moves.urllib import request as urllib_request
|
||||
from django.utils.six.moves.urllib.request import Request
|
||||
from django.utils.six.moves.urllib.error import URLError
|
||||
from django.utils.six.moves.urllib.parse import urlencode
|
||||
from django.utils.module_loading import import_string
|
||||
from django.conf import settings
|
||||
|
||||
from wagtail.wagtailembeds.oembed_providers import get_oembed_provider
|
||||
from wagtail.wagtailembeds.models import Embed
|
||||
|
||||
|
||||
class EmbedException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class EmbedNotFoundException(EmbedException):
|
||||
pass
|
||||
|
||||
|
||||
class EmbedlyException(EmbedException):
|
||||
pass
|
||||
|
||||
|
||||
class AccessDeniedEmbedlyException(EmbedlyException):
|
||||
pass
|
||||
|
||||
|
||||
def embedly(url, max_width=None, key=None):
|
||||
from embedly import Embedly
|
||||
|
||||
# Get embedly key
|
||||
if key is None:
|
||||
key = settings.WAGTAILEMBEDS_EMBEDLY_KEY
|
||||
|
||||
# Get embedly client
|
||||
client = Embedly(key=key)
|
||||
|
||||
# Call embedly
|
||||
if max_width is not None:
|
||||
oembed = client.oembed(url, maxwidth=max_width, better=False)
|
||||
else:
|
||||
oembed = client.oembed(url, better=False)
|
||||
|
||||
# Check for error
|
||||
if oembed.get('error'):
|
||||
if oembed['error_code'] in [401, 403]:
|
||||
raise AccessDeniedEmbedlyException
|
||||
elif oembed['error_code'] == 404:
|
||||
raise EmbedNotFoundException
|
||||
else:
|
||||
raise EmbedlyException
|
||||
|
||||
# Convert photos into HTML
|
||||
if oembed['type'] == 'photo':
|
||||
html = '<img src="%s" />' % (oembed['url'], )
|
||||
else:
|
||||
html = oembed.get('html')
|
||||
|
||||
# Return embed as a dict
|
||||
return {
|
||||
'title': oembed['title'] if 'title' in oembed else '',
|
||||
'author_name': oembed['author_name'] if 'author_name' in oembed else '',
|
||||
'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '',
|
||||
'type': oembed['type'],
|
||||
'thumbnail_url': oembed.get('thumbnail_url'),
|
||||
'width': oembed.get('width'),
|
||||
'height': oembed.get('height'),
|
||||
'html': html,
|
||||
}
|
||||
|
||||
|
||||
def oembed(url, max_width=None):
|
||||
# Find provider
|
||||
provider = get_oembed_provider(url)
|
||||
if provider is None:
|
||||
raise EmbedNotFoundException
|
||||
|
||||
# Work out params
|
||||
params = {'url': url, 'format': 'json'}
|
||||
if max_width:
|
||||
params['maxwidth'] = max_width
|
||||
|
||||
# Perform request
|
||||
request = Request(provider + '?' + urlencode(params))
|
||||
request.add_header('User-agent', 'Mozilla/5.0')
|
||||
try:
|
||||
r = urllib_request.urlopen(request)
|
||||
except URLError:
|
||||
raise EmbedNotFoundException
|
||||
oembed = json.loads(r.read().decode('utf-8'))
|
||||
|
||||
# Convert photos into HTML
|
||||
if oembed['type'] == 'photo':
|
||||
html = '<img src="%s" />' % (oembed['url'], )
|
||||
else:
|
||||
html = oembed.get('html')
|
||||
|
||||
# Return embed as a dict
|
||||
return {
|
||||
'title': oembed['title'] if 'title' in oembed else '',
|
||||
'author_name': oembed['author_name'] if 'author_name' in oembed else '',
|
||||
'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '',
|
||||
'type': oembed['type'],
|
||||
'thumbnail_url': oembed.get('thumbnail_url'),
|
||||
'width': oembed.get('width'),
|
||||
'height': oembed.get('height'),
|
||||
'html': html,
|
||||
}
|
||||
from wagtail.wagtailembeds.exceptions import EmbedException, EmbedNotFoundException # noqa
|
||||
from wagtail.wagtailembeds.finders.oembed import oembed
|
||||
from wagtail.wagtailembeds.finders.embedly import embedly
|
||||
|
||||
|
||||
def get_default_finder():
|
||||
|
|
@ -118,7 +15,7 @@ def get_default_finder():
|
|||
return import_string(settings.WAGTAILEMBEDS_EMBED_FINDER)
|
||||
|
||||
# Use embedly if the embedly key is set
|
||||
if hasattr(settings, 'WAGTAILEMBEDS_EMBEDLY_KEY') or hasattr(settings, 'EMBEDLY_KEY'):
|
||||
if hasattr(settings, 'WAGTAILEMBEDS_EMBEDLY_KEY'):
|
||||
return embedly
|
||||
|
||||
# Fall back to oembed
|
||||
|
|
|
|||
6
wagtail/wagtailembeds/exceptions.py
Normal file
6
wagtail/wagtailembeds/exceptions.py
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
class EmbedException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
class EmbedNotFoundException(EmbedException):
|
||||
pass
|
||||
0
wagtail/wagtailembeds/finders/__init__.py
Normal file
0
wagtail/wagtailembeds/finders/__init__.py
Normal file
57
wagtail/wagtailembeds/finders/embedly.py
Normal file
57
wagtail/wagtailembeds/finders/embedly.py
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from wagtail.wagtailembeds.exceptions import EmbedException, EmbedNotFoundException
|
||||
|
||||
|
||||
class EmbedlyException(EmbedException):
|
||||
pass
|
||||
|
||||
|
||||
class AccessDeniedEmbedlyException(EmbedlyException):
|
||||
pass
|
||||
|
||||
|
||||
def embedly(url, max_width=None, key=None):
|
||||
from embedly import Embedly
|
||||
|
||||
# Get embedly key
|
||||
if key is None:
|
||||
key = settings.WAGTAILEMBEDS_EMBEDLY_KEY
|
||||
|
||||
# Get embedly client
|
||||
client = Embedly(key=key)
|
||||
|
||||
# Call embedly
|
||||
if max_width is not None:
|
||||
oembed = client.oembed(url, maxwidth=max_width, better=False)
|
||||
else:
|
||||
oembed = client.oembed(url, better=False)
|
||||
|
||||
# Check for error
|
||||
if oembed.get('error'):
|
||||
if oembed['error_code'] in [401, 403]:
|
||||
raise AccessDeniedEmbedlyException
|
||||
elif oembed['error_code'] == 404:
|
||||
raise EmbedNotFoundException
|
||||
else:
|
||||
raise EmbedlyException
|
||||
|
||||
# Convert photos into HTML
|
||||
if oembed['type'] == 'photo':
|
||||
html = '<img src="%s" />' % (oembed['url'], )
|
||||
else:
|
||||
html = oembed.get('html')
|
||||
|
||||
# Return embed as a dict
|
||||
return {
|
||||
'title': oembed['title'] if 'title' in oembed else '',
|
||||
'author_name': oembed['author_name'] if 'author_name' in oembed else '',
|
||||
'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '',
|
||||
'type': oembed['type'],
|
||||
'thumbnail_url': oembed.get('thumbnail_url'),
|
||||
'width': oembed.get('width'),
|
||||
'height': oembed.get('height'),
|
||||
'html': html,
|
||||
}
|
||||
51
wagtail/wagtailembeds/finders/oembed.py
Normal file
51
wagtail/wagtailembeds/finders/oembed.py
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
from __future__ import absolute_import
|
||||
|
||||
import json
|
||||
|
||||
# Needs to be imported like this to allow @patch to work in tests
|
||||
from django.utils.six.moves.urllib import request as urllib_request
|
||||
from django.utils.six.moves.urllib.request import Request
|
||||
from django.utils.six.moves.urllib.error import URLError
|
||||
from django.utils.six.moves.urllib.parse import urlencode
|
||||
|
||||
from wagtail.wagtailembeds.oembed_providers import get_oembed_provider
|
||||
from wagtail.wagtailembeds.exceptions import EmbedNotFoundException
|
||||
|
||||
|
||||
def oembed(url, max_width=None):
|
||||
# Find provider
|
||||
provider = get_oembed_provider(url)
|
||||
if provider is None:
|
||||
raise EmbedNotFoundException
|
||||
|
||||
# Work out params
|
||||
params = {'url': url, 'format': 'json'}
|
||||
if max_width:
|
||||
params['maxwidth'] = max_width
|
||||
|
||||
# Perform request
|
||||
request = Request(provider + '?' + urlencode(params))
|
||||
request.add_header('User-agent', 'Mozilla/5.0')
|
||||
try:
|
||||
r = urllib_request.urlopen(request)
|
||||
except URLError:
|
||||
raise EmbedNotFoundException
|
||||
oembed = json.loads(r.read().decode('utf-8'))
|
||||
|
||||
# Convert photos into HTML
|
||||
if oembed['type'] == 'photo':
|
||||
html = '<img src="%s" />' % (oembed['url'], )
|
||||
else:
|
||||
html = oembed.get('html')
|
||||
|
||||
# Return embed as a dict
|
||||
return {
|
||||
'title': oembed['title'] if 'title' in oembed else '',
|
||||
'author_name': oembed['author_name'] if 'author_name' in oembed else '',
|
||||
'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '',
|
||||
'type': oembed['type'],
|
||||
'thumbnail_url': oembed.get('thumbnail_url'),
|
||||
'width': oembed.get('width'),
|
||||
'height': oembed.get('height'),
|
||||
'html': html,
|
||||
}
|
||||
|
|
@ -3,6 +3,7 @@ from __future__ import division # Use true division
|
|||
from django.template.loader import render_to_string
|
||||
|
||||
from wagtail.wagtailembeds import embeds
|
||||
from wagtail.wagtailembeds.exceptions import EmbedException
|
||||
|
||||
|
||||
def embed_to_frontend_html(url):
|
||||
|
|
@ -20,7 +21,7 @@ def embed_to_frontend_html(url):
|
|||
'embed': embed,
|
||||
'ratio': ratio,
|
||||
})
|
||||
except embeds.EmbedException:
|
||||
except EmbedException:
|
||||
# silently ignore failed embeds, rather than letting them crash the page
|
||||
return ''
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
from wagtail.wagtailembeds import format, embeds
|
||||
from wagtail.wagtailembeds import format
|
||||
from wagtail.wagtailembeds.exceptions import EmbedException
|
||||
|
||||
|
||||
class MediaEmbedHandler(object):
|
||||
|
|
@ -28,7 +29,7 @@ class MediaEmbedHandler(object):
|
|||
if for_editor:
|
||||
try:
|
||||
return format.embed_to_editor_html(attrs['url'])
|
||||
except embeds.EmbedException:
|
||||
except EmbedException:
|
||||
# Could be replaced with a nice error message
|
||||
return ''
|
||||
else:
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ from django import template
|
|||
from django.utils.safestring import mark_safe
|
||||
|
||||
from wagtail.wagtailembeds import embeds
|
||||
|
||||
from wagtail.wagtailembeds.exceptions import EmbedException
|
||||
|
||||
register = template.Library()
|
||||
|
||||
|
|
@ -12,5 +12,5 @@ def embed(url, max_width=None):
|
|||
try:
|
||||
embed = embeds.get_embed(url, max_width=max_width)
|
||||
return mark_safe(embed.html)
|
||||
except embeds.EmbedException:
|
||||
except EmbedException:
|
||||
return ''
|
||||
|
|
|
|||
|
|
@ -19,14 +19,14 @@ from wagtail.wagtailcore import blocks
|
|||
from wagtail.tests.utils import WagtailTestUtils
|
||||
|
||||
from wagtail.wagtailembeds.rich_text import MediaEmbedHandler
|
||||
from wagtail.wagtailembeds.embeds import (
|
||||
EmbedNotFoundException,
|
||||
from wagtail.wagtailembeds.embeds import get_embed
|
||||
from wagtail.wagtailembeds.exceptions import EmbedNotFoundException
|
||||
from wagtail.wagtailembeds.finders.embedly import (
|
||||
EmbedlyException,
|
||||
AccessDeniedEmbedlyException,
|
||||
get_embed,
|
||||
embedly as wagtail_embedly,
|
||||
oembed as wagtail_oembed,
|
||||
)
|
||||
from wagtail.wagtailembeds.finders.oembed import oembed as wagtail_oembed
|
||||
from wagtail.wagtailembeds.templatetags.wagtailembeds_tags import embed as embed_filter
|
||||
from wagtail.wagtailembeds.blocks import EmbedBlock, EmbedValue
|
||||
from wagtail.wagtailembeds.models import Embed
|
||||
|
|
|
|||
|
|
@ -5,7 +5,8 @@ from wagtail.wagtailadmin.modal_workflow import render_modal_workflow
|
|||
from wagtail.wagtailembeds.forms import EmbedForm
|
||||
from wagtail.wagtailembeds.format import embed_to_editor_html
|
||||
|
||||
from wagtail.wagtailembeds.embeds import EmbedNotFoundException, EmbedlyException, AccessDeniedEmbedlyException
|
||||
from wagtail.wagtailembeds.exceptions import EmbedNotFoundException
|
||||
from wagtail.wagtailembeds.finders.embedly import EmbedlyException, AccessDeniedEmbedlyException
|
||||
|
||||
|
||||
def chooser(request):
|
||||
|
|
|
|||
Loading…
Reference in a new issue