Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions cms/djangoapps/contentstore/tests/test_transcripts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -959,3 +959,43 @@ def test_get_transcript_no_en_transcript(self):

exception_message = text_type(no_en_transcript_exception.exception)
self.assertEqual(exception_message, 'No transcript for `en` language')

@ddt.data(
transcripts_utils.TranscriptsGenerationException,
UnicodeDecodeError('aliencodec', b'\x02\x01', 1, 2, 'alien codec found!')
)
@patch('xmodule.video_module.transcripts_utils.Transcript')
def test_get_transcript_val_exceptions(self, exception_to_raise, mock_Transcript):
"""
Verify that `get_transcript_from_val` function raises `NotFoundError` when specified exceptions raised.
"""
mock_Transcript.convert.side_effect = exception_to_raise
transcripts_info = self.video.get_transcripts_info()
lang = self.video.get_default_transcript_language(transcripts_info)
edx_video_id = transcripts_utils.clean_video_id(self.video.edx_video_id)
with self.assertRaises(NotFoundError):
transcripts_utils.get_transcript_from_val(
edx_video_id,
lang=lang,
output_format=transcripts_utils.Transcript.SRT
)

@ddt.data(
transcripts_utils.TranscriptsGenerationException,
UnicodeDecodeError('aliencodec', b'\x02\x01', 1, 2, 'alien codec found!')
)
@patch('xmodule.video_module.transcripts_utils.Transcript')
def test_get_transcript_content_store_exceptions(self, exception_to_raise, mock_Transcript):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: test_get_transcript_contentstore_exceptions

"""
Verify that `get_transcript_from_contentstore` function raises `NotFoundError` when specified exceptions raised.
"""
mock_Transcript.asset.side_effect = exception_to_raise
transcripts_info = self.video.get_transcripts_info()
lang = self.video.get_default_transcript_language(transcripts_info)
with self.assertRaises(NotFoundError):
transcripts_utils.get_transcript_from_contentstore(
self.video,
language=lang,
output_format=transcripts_utils.Transcript.SRT,
transcripts_info=transcripts_info
)
24 changes: 23 additions & 1 deletion common/lib/xmodule/xmodule/video_module/transcripts_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Utility functions for transcripts.
++++++++++++++++++++++++++++++++++
"""
from functools import wraps
from django.conf import settings
import os
import copy
Expand Down Expand Up @@ -49,6 +50,26 @@ class TranscriptsRequestValidationException(Exception): # pylint: disable=missi
pass


def exception_decorator(func):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: handle_transcript_exceptions

"""
Generate NotFoundError for TranscriptsGenerationException, UnicodeDecodeError.

Args:
`func`: Input function

Returns:
'wrapper': Decorated function
"""
@wraps(func)
def wrapper(*args, **kwds):
try:
return func(*args, **kwds)
except (TranscriptsGenerationException, UnicodeDecodeError) as ex:
log.exception(text_type(ex))
raise NotFoundError
return wrapper


def generate_subs(speed, source_speed, source_subs):
"""
Generate transcripts from one speed to another speed.
Expand Down Expand Up @@ -855,6 +876,7 @@ def get_transcripts_info(self, is_bumper=False):
}


@exception_decorator
def get_transcript_from_val(edx_video_id, lang=None, output_format=Transcript.SRT):
"""
Get video transcript from edx-val.
Expand Down Expand Up @@ -909,6 +931,7 @@ def get_transcript_for_video(video_location, subs_id, file_name, language):
return input_format, base_name, content


@exception_decorator
def get_transcript_from_contentstore(video, language, output_format, transcripts_info, youtube_id=None):
"""
Get video transcript from content store.
Expand Down Expand Up @@ -954,7 +977,6 @@ def get_transcript_from_contentstore(video, language, output_format, transcripts
language_prefix = '{}_'.format(language) if language else ''
transcript_name = u'{}{}.{}'.format(language_prefix, base_name, output_format)
transcript_content = Transcript.convert(transcript_content, input_format=input_format, output_format=output_format)

if not transcript_content.strip():
raise NotFoundError('No transcript content')

Expand Down