Skip to content

Commit

Permalink
Merge a9e38be into c22c82a
Browse files Browse the repository at this point in the history
  • Loading branch information
bburan committed Mar 24, 2014
2 parents c22c82a + a9e38be commit 6df1c46
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
15 changes: 13 additions & 2 deletions embed_video/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import requests
import json

import urllib
try:
import urlparse
except ImportError:
Expand Down Expand Up @@ -106,14 +107,15 @@ class MyBackend(VideoBackend):
``{{ width }}``, ``{{ height }}``
"""

def __init__(self, url, is_secure=False):
def __init__(self, url, is_secure=False, query=None):
"""
First it tries to load data from cache and if it don't succeed, run
:py:meth:`init` and then save it to cache.
"""
self.is_secure = is_secure
self.backend = self.__class__.__name__
self._url = url
self._query = None

@cached_property
def code(self):
Expand Down Expand Up @@ -155,7 +157,14 @@ def get_url(self):
"""
Returns URL folded from :py:data:`pattern_url` and parsed code.
"""
return self.pattern_url.format(code=self.code, protocol=self.protocol)
url = self.pattern_url.format(code=self.code, protocol=self.protocol)
if self._query is not None:
result = list(urlparse.urlparse(url))
qs = urlparse.parse_qs(result[4])
qs.update(self._query)
result[4] = urllib.urlencode(qs, doseq=True)
url = urlparse.urlunparse(result)
return url

def get_thumbnail_url(self):
"""
Expand All @@ -169,6 +178,8 @@ def get_embed_code(self, width, height):
"""
Returns embed code rendered from template :py:data:`template_name`.
"""
# Need to cache this locally so that it can be accessed by the
# `get_url` method when called in the template.
return render_to_string(self.template_name, {
'backend': self,
'width': width,
Expand Down
16 changes: 12 additions & 4 deletions embed_video/templatetags/embed_video_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,13 @@ def get_backend(backend_or_url, context=None):
"""
Returns instance of VideoBackend. If context is passed to the method
and request is secure, than the is_secure mark is set to backend.
A string or VideoBackend instance can be passed to the method.
"""

backend = backend_or_url if isinstance(backend_or_url, VideoBackend) \
else detect_backend(backend_or_url)

if context and 'request' in context:
backend.is_secure = context['request'].is_secure()

Expand Down Expand Up @@ -175,7 +175,7 @@ def __repr__(self):


@register.filter(is_safe=True)
def embed(backend, size='small'):
def embed(backend, size='small', **kwargs):
"""
.. warning::
.. deprecated:: 0.7
Expand All @@ -197,3 +197,11 @@ def embed(backend, size='small'):
{{ 'http://www.youtube.com/watch?v=guXyvo2FfLs'|embed:'large' }}
"""
return VideoNode.embed(backend, size)


@register.simple_tag
def video_embed(url, size='small', **kwargs):
backend = VideoNode.get_backend(url)
backend._query = kwargs
width, height = VideoNode.get_size(size)
return mark_safe(backend.get_embed_code(width=width, height=height))

0 comments on commit 6df1c46

Please sign in to comment.