diff --git a/common/lib/xmodule/xmodule/video_module.py b/common/lib/xmodule/xmodule/video_module.py
index 0eb5a5fa2e..e0cf534513 100644
--- a/common/lib/xmodule/xmodule/video_module.py
+++ b/common/lib/xmodule/xmodule/video_module.py
@@ -6,6 +6,8 @@ import logging
from lxml import etree
from pkg_resources import resource_string, resource_listdir
+import datetime
+import time
from django.http import Http404
@@ -137,10 +139,10 @@ class VideoDescriptor(VideoFields,
tag = _get_first_external(xml, 'tag')
if tag:
video.tag = tag
- start_time = xml.get('from')
+ start_time = _parse_time(xml.get('from'))
if start_time:
video.start_time = start_time
- end_time = xml.get('to')
+ end_time = _parse_time(xml.get('to'))
if end_time:
video.end_time = end_time
return video
@@ -177,3 +179,17 @@ def _parse_youtube(data):
# properly.
ret['%.2f' % float(pieces[0])] = pieces[1]
return ret
+
+
+def _parse_time(str_time):
+ """Converts s in '12:34:45' format to seconds. If s is
+ None, returns empty string"""
+ if str_time is None:
+ return ''
+ else:
+ obj_time = time.strptime(str_time, '%H:%M:%S')
+ return datetime.timedelta(
+ hours=obj_time.tm_hour,
+ minutes=obj_time.tm_min,
+ seconds=obj_time.tm_sec
+ ).total_seconds()
diff --git a/lms/djangoapps/courseware/tests/test_video_xml.py b/lms/djangoapps/courseware/tests/test_video_xml.py
index 35437348d9..aa36d5be89 100644
--- a/lms/djangoapps/courseware/tests/test_video_xml.py
+++ b/lms/djangoapps/courseware/tests/test_video_xml.py
@@ -18,7 +18,7 @@ import unittest
from mock import Mock
from lxml import etree
-from xmodule.video_module import VideoDescriptor, VideoModule
+from xmodule.video_module import VideoDescriptor, VideoModule, _parse_time, _parse_youtube
from xmodule.modulestore import Location
from xmodule.tests import test_system
from xmodule.tests.test_logic import LogicTest
@@ -67,30 +67,31 @@ class VideoModuleLogicTest(LogicTest):
'data': ''
}
- def test_get_timeframe_no_parameters(self):
- """Make sure that timeframe() works correctly w/o parameters"""
- xmltree = etree.fromstring('')
- output = self.xmodule.get_timeframe(xmltree)
- self.assertEqual(output, ('', ''))
+ def test_parse_time(self):
+ """Ensure that times are parse correctly into seconds."""
+ output = _parse_time('00:04:07')
+ self.assertEqual(output, 247)
- def test_get_timeframe_with_one_parameter(self):
- """Make sure that timeframe() works correctly with one parameter"""
- xmltree = etree.fromstring(
- ''
- )
- output = self.xmodule.get_timeframe(xmltree)
- self.assertEqual(output, (247, ''))
+ def test_parse_youtube(self):
+ """Test parsing old-style Youtube ID strings into a dict."""
+ youtube_str = '0.75:jNCf2gIqpeE,1.0:ZwkTiUPN0mg,1.25:rsq9auxASqI,1.50:kMyNdzVHHgg'
+ output = _parse_youtube(youtube_str)
+ self.assertEqual(output, {'0.75': 'jNCf2gIqpeE',
+ '1.00': 'ZwkTiUPN0mg',
+ '1.25': 'rsq9auxASqI',
+ '1.50': 'kMyNdzVHHgg'})
- def test_get_timeframe_with_two_parameters(self):
- """Make sure that timeframe() works correctly with two parameters"""
- xmltree = etree.fromstring(
- ''''''
- )
- output = self.xmodule.get_timeframe(xmltree)
- self.assertEqual(output, (247, 47079))
+ def test_parse_youtube_one_video(self):
+ """
+ Ensure that all keys are present and missing speeds map to the
+ empty string.
+ """
+ youtube_str = '0.75:jNCf2gIqpeE'
+ output = _parse_youtube(youtube_str)
+ self.assertEqual(output, {'0.75': 'jNCf2gIqpeE',
+ '1.00': '',
+ '1.25': '',
+ '1.50': ''})
class VideoModuleUnitTest(unittest.TestCase):