Skip to content

Commit

Permalink
Merge 9f605de into 1b208b9
Browse files Browse the repository at this point in the history
  • Loading branch information
yetty committed Jul 26, 2014
2 parents 1b208b9 + 9f605de commit 1dcd874
Show file tree
Hide file tree
Showing 18 changed files with 570 additions and 645 deletions.
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Release 1.0.0 (dev)
-------------------

**Backward incompatible changes:**

- filter `embed_video_tags.embed` has been removed

Backward compatible changes:
*No changes yet.*


Expand Down
58 changes: 29 additions & 29 deletions embed_video/backends.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
import re
import requests
import sys
import json
import requests

try:
# Python <= 2.7
import urlparse
from urllib import urlencode
except ImportError:
# support for py3
if sys.version_info.major == 3:
import urllib.parse as urlparse
from urllib.parse import urlencode
else:
import urlparse

from django.conf import settings
from django.http import QueryDict
from django.template.loader import render_to_string
from django.utils.functional import cached_property
from django.utils.safestring import mark_safe
from django.utils.datastructures import SortedDict

from .utils import import_by_path
from .settings import EMBED_VIDEO_BACKENDS, EMBED_VIDEO_TIMEOUT
from .settings import EMBED_VIDEO_BACKENDS, EMBED_VIDEO_TIMEOUT, \
EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY


class EmbedVideoException(Exception):
Expand Down Expand Up @@ -112,30 +109,27 @@ class MyBackend(VideoBackend):
``{{ width }}``, ``{{ height }}``
"""

def __init__(self, url, is_secure=False, query=None):
default_query = ''

def __init__(self, url, is_secure=False):
"""
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.update_query(query)

def update_query(self, query=None):
self._query = SortedDict(self.get_default_query())
if query is not None:
self._query.update(query)
self.query = QueryDict(self.default_query, mutable=True)

@cached_property
def code(self):
return self.get_code()

@cached_property
@property
def url(self):
return self.get_url()

@cached_property
@property
def protocol(self):
return 'https' if self.allow_https and self.is_secure else 'http'

Expand All @@ -147,6 +141,16 @@ def thumbnail(self):
def info(self):
return self.get_info()

@property
def query(self):
return self._query

@query.setter
def query(self, value):
self._query = value \
if isinstance(value, QueryDict) \
else QueryDict(value, mutable=True)

@classmethod
def is_valid(cls, url):
"""
Expand All @@ -168,8 +172,7 @@ def get_url(self):
Returns URL folded from :py:data:`pattern_url` and parsed code.
"""
url = self.pattern_url.format(code=self.code, protocol=self.protocol)
if self._query:
url += '?' + urlencode(self._query, doseq=True)
url += '?' + self.query.urlencode() if self.query else ''
return mark_safe(url)

def get_thumbnail_url(self):
Expand All @@ -193,12 +196,9 @@ def get_embed_code(self, width, height):
def get_info(self):
raise NotImplementedError

def get_default_query(self):
# Derive backend name from class name
backend_name = self.__class__.__name__[:-7].upper()
default = getattr(self, 'default_query', {})
settings_key = 'EMBED_VIDEO_{0}_QUERY'.format(backend_name)
return getattr(settings, settings_key, default).copy()
def set_options(self, options):
for key in options:
setattr(self, key, options[key])


class YoutubeBackend(VideoBackend):
Expand All @@ -225,7 +225,7 @@ class YoutubeBackend(VideoBackend):

pattern_url = '{protocol}://www.youtube.com/embed/{code}'
pattern_thumbnail_url = '{protocol}://img.youtube.com/vi/{code}/hqdefault.jpg'
default_query = {'wmode': 'opaque'}
default_query = EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY

def get_code(self):
code = super(YoutubeBackend, self).get_code()
Expand Down
3 changes: 3 additions & 0 deletions embed_video/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@
))

EMBED_VIDEO_TIMEOUT = getattr(settings, 'EMBED_VIDEO_TIMEOUT', 10)

EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY = \
getattr(settings, 'EMBED_VIDEO_YOUTUBE_DEFAULT_QUERY', 'wmode=opaque')
3 changes: 1 addition & 2 deletions embed_video/templates/embed_video/embed_code.html
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
<iframe width="{{ width }}" height="{{ height }}" src="{{ backend.url }}"
frameborder="0" allowfullscreen></iframe>
<iframe width="{{ width }}" height="{{ height }}" src="{{ backend.url }}" frameborder="0" allowfullscreen></iframe>

0 comments on commit 1dcd874

Please sign in to comment.