From ab6c82342e5297713208075157fad143b1175eef Mon Sep 17 00:00:00 2001 From: Carson Gee Date: Wed, 2 Apr 2014 16:10:07 -0400 Subject: [PATCH 1/2] Add last resort method to download and showing of video transcript --- .../xmodule/video_module/video_handlers.py | 36 ++++++++++- .../courseware/tests/test_video_handlers.py | 59 ++++++++++++++++++- 2 files changed, 92 insertions(+), 3 deletions(-) diff --git a/common/lib/xmodule/xmodule/video_module/video_handlers.py b/common/lib/xmodule/xmodule/video_module/video_handlers.py index fc06701d02..722cefacf7 100644 --- a/common/lib/xmodule/xmodule/video_module/video_handlers.py +++ b/common/lib/xmodule/xmodule/video_module/video_handlers.py @@ -11,6 +11,7 @@ from webob import Response from xblock.core import XBlock +from xmodule.course_module import CourseDescriptor from xmodule.exceptions import NotFoundError from xmodule.fields import RelativeTime @@ -22,6 +23,7 @@ from .transcripts_utils import ( youtube_speed_dict, Transcript, save_to_store, + subs_filename ) @@ -171,6 +173,33 @@ class VideoStudentViewHandlers(object): return content, filename, Transcript.mime_types[transcript_format] + def get_static_transcript(self, request): + """ + Return URL for static transcript if it isn't available in the content store + """ + # Try to return static redirect to the transcript as a last + # resort, but return 404 if we don't + response = Response(status=404) + vid_id = request.GET.get('videoId', None) + + if vid_id: + transcript_name = vid_id + else: + transcript_name = self.sub + + if transcript_name: + course_location = CourseDescriptor.id_to_location(self.course_id) + course = self.descriptor.runtime.modulestore.get_item(course_location) + if course.static_asset_path: + response = Response( + status=307, + location='/static/{0}/{1}'.format( + course.static_asset_path, + subs_filename(transcript_name, self.transcript_language) + ) + ) + return response + @XBlock.handler def transcript(self, request, dispatch): """ @@ -206,6 +235,7 @@ class VideoStudentViewHandlers(object): if language != self.transcript_language: self.transcript_language = language + try: transcript = self.translation(request.GET.get('videoId', None)) except ( @@ -216,7 +246,8 @@ class VideoStudentViewHandlers(object): TranscriptsGenerationException ) as ex: log.info(ex.message) - response = Response(status=404) + # Try to return static URL redirection as last resort + return self.get_static_transcript(request) else: response = Response(transcript, headerlist=[('Content-Language', language)]) response.content_type = Transcript.mime_types['sjson'] @@ -226,7 +257,8 @@ class VideoStudentViewHandlers(object): transcript_content, transcript_filename, transcript_mime_type = self.get_transcript(self.transcript_download_format) except (NotFoundError, ValueError, KeyError, UnicodeDecodeError): log.debug("Video@download exception") - return Response(status=404) + # Return static URL or 404 + return self.get_static_transcript(request) else: response = Response( transcript_content, diff --git a/lms/djangoapps/courseware/tests/test_video_handlers.py b/lms/djangoapps/courseware/tests/test_video_handlers.py index 5598e5a3b4..296a81acb5 100644 --- a/lms/djangoapps/courseware/tests/test_video_handlers.py +++ b/lms/djangoapps/courseware/tests/test_video_handlers.py @@ -10,8 +10,9 @@ from datetime import timedelta from webob import Request from xmodule.contentstore.content import StaticContent -from xmodule.modulestore import Location from xmodule.contentstore.django import contentstore +from xmodule.modulestore import Location +from xmodule.modulestore.django import editable_modulestore from . import BaseTestXmodule from .test_video_xml import SOURCE_XML from cache_toolbox.core import del_cached_content @@ -225,6 +226,7 @@ class TestTranscriptDownloadDispatch(TestVideo): DATA = """