From 24ad0ac3df868a22d49071c1fe2d886b2dd9bdeb Mon Sep 17 00:00:00 2001 From: Serafeim Papastefanos Date: Tue, 11 Feb 2014 09:14:43 +0200 Subject: [PATCH 1/2] Check if EMBEDLY_KEY has been set This will check if EMBEDLY_KEY has been set in settings. If not it will return a nice error message instead of throwing a 500 error. --- wagtail/wagtailembeds/embeds.py | 7 +++++-- wagtail/wagtailembeds/views/chooser.py | 7 +++++-- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/wagtail/wagtailembeds/embeds.py b/wagtail/wagtailembeds/embeds.py index dd416fab1a..5108e51857 100644 --- a/wagtail/wagtailembeds/embeds.py +++ b/wagtail/wagtailembeds/embeds.py @@ -13,8 +13,11 @@ def get_embed(url, max_width=None): except Embed.DoesNotExist: pass - # Call embedly API - client = Embedly(key=settings.EMBEDLY_KEY) + try: + # Call embedly API + client = Embedly(key=settings.EMBEDLY_KEY) + except AttributeError: + return None if max_width is not None: oembed = client.oembed(url, maxwidth=max_width, better=False) else: diff --git a/wagtail/wagtailembeds/views/chooser.py b/wagtail/wagtailembeds/views/chooser.py index 17d9f49c83..13a835acbf 100644 --- a/wagtail/wagtailembeds/views/chooser.py +++ b/wagtail/wagtailembeds/views/chooser.py @@ -1,7 +1,7 @@ from django.forms.util import ErrorList +from django.conf import settings from wagtail.wagtailadmin.modal_workflow import render_modal_workflow - from wagtail.wagtailembeds.forms import EmbedForm from wagtail.wagtailembeds.format import embed_to_editor_html @@ -27,7 +27,10 @@ def chooser_upload(request): ) else: errors = form._errors.setdefault('url', ErrorList()) - errors.append('This URL is not recognised') + if not hasattr(settings, 'EMBEDLY_KEY'): + errors.append('Please set EMBEDLY_KEY in your settings') + else: + errors.append('This URL is not recognised') return render_modal_workflow(request, 'wagtailembeds/chooser/chooser.html', 'wagtailembeds/chooser/chooser.js', { 'form': form, }) From 3cd138cda4cfc2b040d5025e84d1ba4b11db4d29 Mon Sep 17 00:00:00 2001 From: Serafeim Papastefanos Date: Tue, 11 Feb 2014 09:51:35 +0200 Subject: [PATCH 2/2] Modified embeds.py to make it work The Embedly API was not working at all because the return value of Embedly (client) offered the attributes in dictionary style and not as an object. Now everything seems to be fine. --- wagtail/wagtailembeds/embeds.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/wagtail/wagtailembeds/embeds.py b/wagtail/wagtailembeds/embeds.py index 5108e51857..59d349a267 100644 --- a/wagtail/wagtailembeds/embeds.py +++ b/wagtail/wagtailembeds/embeds.py @@ -24,7 +24,7 @@ def get_embed(url, max_width=None): oembed = client.oembed(url, better=False) # Check for error - if oembed.error: + if oembed.get('error'): return None # Save result to database @@ -32,18 +32,18 @@ def get_embed(url, max_width=None): url=url, max_width=max_width, defaults={ - 'type': oembed.type, - 'title': oembed.title, - 'thumbnail_url': oembed.thumbnail_url, - 'width': oembed.width, - 'height': oembed.height + 'type': oembed['type'], + 'title': oembed['title'], + 'thumbnail_url': oembed.get('thumbnail_url'), + 'width': oembed.get('width'), + 'height': oembed.get('height') } ) - if oembed.type == 'photo': - html = '' % (oembed.url, ) + if oembed['type'] == 'photo': + html = '' % (oembed['url'], ) else: - html = oembed.html + html = oembed.get('html') if html: row.html = html