Merge pull request #32 from yetty/I31-validation

Closes #31 - improve validation and parsing YouTube urls.
This commit is contained in:
Juda Kaleta 2014-07-19 10:11:21 +02:00
commit 3c46dca8b8
5 changed files with 33 additions and 15 deletions

View file

@ -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.
"""
@ -231,13 +231,15 @@ class YoutubeBackend(VideoBackend):
code = super(YoutubeBackend, self).get_code()
if not code:
parse_data = urlparse.urlparse(self._url)
parsed_url = urlparse.urlparse(self._url)
parsed_qs = urlparse.parse_qs(parsed_url.query)
try:
code = urlparse.parse_qs(parse_data.query)['v'][0]
except KeyError:
raise UnknownIdException(
'Cannot get ID from `{0}`'.format(self._url))
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

View file

@ -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:

View file

@ -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

View file

@ -4,7 +4,8 @@ from unittest import TestCase
from django.forms import ValidationError
from ..fields import EmbedVideoField, EmbedVideoFormField
from ..backends import UnknownBackendException, UnknownIdException
from ..backends import UnknownBackendException, UnknownIdException, \
YoutubeBackend
class EmbedVideoFieldTestCase(TestCase):
@ -27,14 +28,14 @@ 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
@ -42,11 +43,15 @@ class EmbedVideoFormFieldTestCase(TestCase):
('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, '')

View file

@ -97,6 +97,15 @@ class EmbedVideoNodeTestCase(TestCase):
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 %}