Skip to content

Commit 94f92ac

Browse files
author
floguy
committed
Added much more robust replacement mechanism using Django templates.
git-svn-id: https://django-oembed.googlecode.com/svn/trunk@8 4c08321d-174e-0410-8f1d-477679e35061
1 parent d5292dc commit 94f92ac

File tree

4 files changed

+5
-12
lines changed

4 files changed

+5
-12
lines changed

oembed/core.py

+2-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from django.utils.safestring import mark_safe
1414
#from django.utils.html import conditional_escape
1515
from oembed.models import ProviderRule, StoredOEmbed
16+
from django.template.loader import render_to_string
1617

1718
conditional_escape = lambda x: x
1819

@@ -21,12 +22,6 @@
2122
MAX_HEIGHT = getattr(settings, "OEMBED_MAX_HEIGHT", 240)
2223
FORMAT = getattr(settings, "OEMBED_FORMAT", "json")
2324

24-
HTML_MAPPING = {
25-
'photo': 'url',
26-
'video': 'html',
27-
'rich': 'html',
28-
}
29-
3025
def fetch(url, user_agent="django-oembed"):
3126
request = urllib2.Request(url)
3227
request.add_header('User-Agent', user_agent)
@@ -111,12 +106,10 @@ def replace(text, max_width=MAX_WIDTH, max_height=MAX_HEIGHT):
111106
urls.add(part)
112107
indices_rules.append(i)
113108
parts.append(part)
114-
print part
115109
index += 1
116110
if to_append:
117111
parts.append(to_append)
118112
index += 1
119-
print urls
120113
for stored_embed in StoredOEmbed.objects.filter(match__in=urls, max_width=max_width, max_height = max_height):
121114
stored[stored_embed.match] = stored_embed
122115
for i, id_to_replace in enumerate(indices):
@@ -130,10 +123,7 @@ def replace(text, max_width=MAX_WIDTH, max_height=MAX_HEIGHT):
130123
rule.endpoint, part, max_width, max_height, FORMAT
131124
)
132125
resp = simplejson.loads(fetch(url))
133-
replacement = None
134-
for key in HTML_MAPPING.iterkeys():
135-
if resp['type'] == key:
136-
replacement = resp[HTML_MAPPING[key]]
126+
replacement = render_to_string('oembed/%s.html' % resp['type'], {'response': resp})
137127
if replacement:
138128
stored_embed = StoredOEmbed.objects.create(
139129
match = part,

oembed/templates/oembed/photo.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% autoescape off %}<img src="{{ response.url }}" alt="{{ response.title }}"></img>{% endautoescape %}

oembed/templates/oembed/rich.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% autoescape off %}{{ response.html }}{% endautoescape %}

oembed/templates/oembed/video.html

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% autoescape off %}{{ response.html }}{% endautoescape %}

0 commit comments

Comments
 (0)