From a31f4b976b96f338583fa0bb155cde5d95573f60 Mon Sep 17 00:00:00 2001 From: Juda Kaleta Date: Tue, 3 Sep 2013 13:47:39 +0200 Subject: [PATCH] Closes #7. Return back raising UnknownIdException in Youtube backend to fix form validation. --- docs/.gitignore | 1 + embed_video/backends.py | 9 +++++++-- embed_video/tests/tests_backend.py | 9 ++++++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/.gitignore b/docs/.gitignore index 04bc145..ccbcdf9 100644 --- a/docs/.gitignore +++ b/docs/.gitignore @@ -1 +1,2 @@ _* +!__init__.py diff --git a/embed_video/backends.py b/embed_video/backends.py index 800e132..969af34 100644 --- a/embed_video/backends.py +++ b/embed_video/backends.py @@ -20,6 +20,7 @@ if EMBED_VIDEO_CACHE: class VideoDoesntExistException(Exception): pass + class UnknownBackendException(Exception): pass @@ -160,7 +161,7 @@ class YoutubeBackend(VideoBackend): (\S*[^\w\-\s])? (?P[\w\-]{11})[a-z0-9;:@?&%=+/\$_.-]* # match and extract ''', - re.I|re.X + re.I | re.X ) pattern_url = 'http://www.youtube.com/embed/%s?wmode=opaque' @@ -171,7 +172,11 @@ class YoutubeBackend(VideoBackend): if not code: parse_data = urlparse.urlparse(self._url) - code = urlparse.parse_qs(parse_data.query)['v'][0] + + try: + code = urlparse.parse_qs(parse_data.query)['v'][0] + except KeyError: + raise UnknownIdException return code diff --git a/embed_video/tests/tests_backend.py b/embed_video/tests/tests_backend.py index f005d05..9859236 100644 --- a/embed_video/tests/tests_backend.py +++ b/embed_video/tests/tests_backend.py @@ -1,7 +1,8 @@ from unittest import TestCase from ..backends import detect_backend, YoutubeBackend, VimeoBackend, \ - SoundCloudBackend, UnknownBackendException, VideoDoesntExistException + SoundCloudBackend, UnknownBackendException, \ + VideoDoesntExistException, UnknownIdException class BackendsTestCase(TestCase): @@ -91,3 +92,9 @@ class BackendsTestCase(TestCase): def test_vimeo_get_info_exception(self): self.assertRaises(VideoDoesntExistException, VimeoBackend, 'http://vimeo.com/123') + + def test_youtube_keyerror(self): + """ Test for issue #7 """ + self.assertRaises(UnknownIdException, YoutubeBackend, + 'http://youtube.com/watch?id=5') +