Handling timeout in template tags

This commit is contained in:
Juda Kaleta 2014-02-22 10:04:41 +01:00
parent 84b2df8d4e
commit 0331a0e030
6 changed files with 40 additions and 10 deletions

View file

@ -12,6 +12,7 @@ install:
- pip install mock --use-mirrors
- pip install south --use-mirrors
- pip install nose
- pip install testfixtures
script:
- python setup.py build
- nosetests --with-coverage --cover-package=embed_video --cover-tests --cover-erase

View file

@ -3,7 +3,11 @@ Release 0.8 (dev)
- Add ``EMBED_VIDEO_TIMEOUT`` to settings.
- Fix renderering template tag if no url is provided (`#18 <https://github.com/yetty/django-embed-video/issues/18>`_)
- Fix renderering template tag if no url is provided
(`#18 <https://github.com/yetty/django-embed-video/issues/18>`_)
- If ``EMBED_VIDEO_TIMEOUT`` timeout is reached in templates, no exception is
raised, error is just logged.
Release 0.7 (Dec. 21, 2013)

View file

@ -4,8 +4,8 @@ Testing
Requirements
------------
The library needs ``Django`` and ``requests`` and ``nose``, ``mock`` and
``south`` libraries to run tests.
The library needs ``Django`` and ``requests`` and ``nose``, ``mock``,
``south`` and ``textfixtures`` libraries to run tests.
::
@ -14,6 +14,7 @@ The library needs ``Django`` and ``requests`` and ``nose``, ``mock`` and
pip install nose
pip install mock
pip install south
pip install textfixtures
Running tests

View file

@ -238,7 +238,7 @@ class SoundCloudBackend(VideoBackend):
}
r = requests.get(self.base_url, data=params,
timeout=EMBED_VIDEO_TIMEOUT)
return json.loads(r.text)
def get_thumbnail_url(self):

View file

@ -1,11 +1,15 @@
from django.template import Library, Node, TemplateSyntaxError
from django.utils.safestring import mark_safe
import re
import logging
import requests
from ..backends import detect_backend, VideoBackend
register = Library()
logger = logging.getLogger(__name__)
@register.tag('video')
class VideoNode(Node):
@ -82,11 +86,18 @@ class VideoNode(Node):
return self.embed(url, size, context=context)
def __render_block(self, url, context):
output = ''
as_var = self.bits[-1]
context.push()
context[as_var] = self.get_backend(url, context=context)
output = self.nodelist_file.render(context)
context.pop()
try:
context.push()
context[as_var] = self.get_backend(url, context=context)
output = self.nodelist_file.render(context)
context.pop()
except requests.Timeout:
logger.exception('Timeout reached during rendering embed '
'video (`{0}`)'.format(url))
return output
@staticmethod

View file

@ -1,11 +1,12 @@
from unittest import TestCase
from django.test.client import RequestFactory
from mock import Mock
from mock import Mock, patch
from django.template import TemplateSyntaxError
from django.http import HttpRequest
from django.template.base import Template
from django.template.context import RequestContext
from testfixtures import LogCapture
from embed_video.templatetags.embed_video_tags import VideoNode
@ -160,4 +161,16 @@ class EmbedVideoNodeTestCase(TestCase):
""")
self.assertEqual(template.render(self._grc()).strip(), '')
@patch('embed_video.backends.EMBED_VIDEO_TIMEOUT', 0.000001)
def test_empty_if_timeout(self):
template = Template("""
{% load embed_video_tags %}
{% video "http://vimeo.com/72304002" as my_video %}
{{ my_video.thumbnail }}
{% endvideo %}
""")
with LogCapture() as logs:
self.assertEqual(template.render(self._grc()).strip(), '')
log = logs.records[-1]
self.assertEqual(log.name, 'embed_video.templatetags.embed_video_tags')
self.assertEqual(log.msg, 'Timeout reached during rendering embed video (`http://vimeo.com/72304002`)')