Skip to content

Commit

Permalink
Merge be87966 into 38e8330
Browse files Browse the repository at this point in the history
  • Loading branch information
yetty committed Jul 19, 2014
2 parents 38e8330 + be87966 commit d87b3b9
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
18 changes: 10 additions & 8 deletions embed_video/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class UnknownBackendException(EmbedVideoException):
pass


class UnknownIdException(EmbedVideoException):
class UnknownIdException(VideoDoesntExistException):
"""
Exception thrown if backend is detected, but video ID cannot be parsed.
"""
Expand Down Expand Up @@ -231,13 +231,15 @@ def get_code(self):
code = super(YoutubeBackend, self).get_code()

if not code:
parse_data = urlparse.urlparse(self._url)

try:
code = urlparse.parse_qs(parse_data.query)['v'][0]
except KeyError:
raise UnknownIdException(
'Cannot get ID from `{0}`'.format(self._url))
parsed_url = urlparse.urlparse(self._url)
parsed_qs = urlparse.parse_qs(parsed_url.query)

if 'v' in parsed_qs:
code = parsed_qs['v'][0]
elif 'video_id' in parsed_qs:
code = parsed_qs['video_id'][0]
else:
raise UnknownIdException('Cannot get ID from `{0}`'.format(self._url))

return code

Expand Down
5 changes: 3 additions & 2 deletions embed_video/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ class EmbedVideoFormField(forms.URLField):
def validate(self, url):
# if empty url is not allowed throws an exception
super(EmbedVideoFormField, self).validate(url)

if not url:
return

try:
detect_backend(url)
backend = detect_backend(url)
backend.get_code()
except UnknownBackendException:
raise forms.ValidationError(_(u'URL could not be recognized.'))
except UnknownIdException:
Expand Down
3 changes: 2 additions & 1 deletion embed_video/tests/tests_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class YoutubeBackendTestCase(BackendTestMixin, TestCase):
('https://www.youtube.com/watch?v=XPk521voaOE&feature=youtube_gdata_player', 'XPk521voaOE'),
('http://www.youtube.com/watch?v=6xu00J3-g2s&list=PLb5n6wzDlPakFKvJ69rJ9AJW24Aaaki2z', '6xu00J3-g2s'),
('https://m.youtube.com/#/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ'),
('https://m.youtube.com/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ')
('https://m.youtube.com/watch?v=IAooXLAPoBQ', 'IAooXLAPoBQ'),
('http://www.youtube.com/edit?video_id=eBea01qmnOE', 'eBea01qmnOE')
)

instance = YoutubeBackend
Expand Down
15 changes: 10 additions & 5 deletions embed_video/tests/tests_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from django.forms import ValidationError

from ..fields import EmbedVideoField, EmbedVideoFormField
from ..backends import UnknownBackendException, UnknownIdException
from ..backends import UnknownBackendException, UnknownIdException, \
YoutubeBackend


class EmbedVideoFieldTestCase(TestCase):
Expand All @@ -27,26 +28,30 @@ class EmbedVideoFormFieldTestCase(TestCase):
def setUp(self):
self.formfield = EmbedVideoFormField()

def test_validation_unknownbackend(self):
def test_validation_unknown_backend(self):
with patch('embed_video.fields.detect_backend') as mock_detect_backend:
mock_detect_backend.return_value = True
mock_detect_backend.side_effect = UnknownBackendException
self.assertRaises(ValidationError, self.formfield.validate,
('http://youtube.com/v/123/',))

def test_validation_unknownid(self):
def test_validation_unknown_id(self):
with patch('embed_video.fields.detect_backend') as mock_detect_backend:
mock_detect_backend.return_value = True
mock_detect_backend.side_effect = UnknownIdException
self.assertRaises(ValidationError, self.formfield.validate,
('http://youtube.com/v/123/',))

def test_validation_correct(self):
url = 'http://my-testing.url.com'
url = 'http://www.youtube.com/watch?v=gauN0gzxTcU'
with patch('embed_video.fields.detect_backend') as mock_detect_backend:
mock_detect_backend.return_value = True
mock_detect_backend.return_value = YoutubeBackend(url)
self.assertEqual(url, self.formfield.validate(url))

def test_validation_unknown_code(self):
url = 'http://www.youtube.com/edit?abcd=abcd'
self.assertRaises(ValidationError, self.formfield.validate, url)

def test_validation_super(self):
self.assertRaises(ValidationError, self.formfield.validate, '')

Expand Down
9 changes: 9 additions & 0 deletions embed_video/tests/tests_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ def test_tag_youtube(self):
rendered = 'http://www.youtube.com/embed/jsrRJyHBvzw?wmode=opaque'
self.assertEqual(template.render(self._grc()).strip(), rendered)

def test_tag_youtube_invalid_url(self):
template = Template("""
{% load embed_video_tags %}
{% video 'http://www.youtube.com/edit?abcd=efgh' as ytb %}
{{ ytb.url }}
{% endvideo %}
""")
self.assertEqual(template.render(self._grc()).strip(), '')

def test_tag_vimeo(self):
template = Template("""
{% load embed_video_tags %}
Expand Down

0 comments on commit d87b3b9

Please sign in to comment.