From 745e48159719fb50a159e64e6275dea40c345cb7 Mon Sep 17 00:00:00 2001 From: Syed Hassan Raza Date: Wed, 1 Jun 2016 15:51:38 +0500 Subject: [PATCH] Fixed logging and unicode handling --- .../lib/xmodule/xmodule/tests/test_video.py | 8 ++++++++ .../xmodule/video_module/video_module.py | 20 ++++++++++--------- .../xmodule/video_module/video_utils.py | 13 ++++++++++++ 3 files changed, 32 insertions(+), 9 deletions(-) diff --git a/common/lib/xmodule/xmodule/tests/test_video.py b/common/lib/xmodule/xmodule/tests/test_video.py index 9b5b49eaba..e11147a43b 100644 --- a/common/lib/xmodule/xmodule/tests/test_video.py +++ b/common/lib/xmodule/xmodule/tests/test_video.py @@ -758,6 +758,14 @@ class VideoExportTestCase(VideoDescriptorTestBase): with self.assertRaises(ValueError): self.descriptor.definition_to_xml(None) + def test_export_to_xml_unicode_characters(self): + """ + Test XML export handles the unicode characters. + """ + self.descriptor.display_name = '这是文' + xml = self.descriptor.definition_to_xml(None) + self.assertEqual(xml.get('display_name'), u'\u8fd9\u662f\u6587') + class VideoDescriptorIndexingTestCase(unittest.TestCase): """ diff --git a/common/lib/xmodule/xmodule/video_module/video_module.py b/common/lib/xmodule/xmodule/video_module/video_module.py index 59a54ff785..31f9ecc4ad 100644 --- a/common/lib/xmodule/xmodule/video_module/video_module.py +++ b/common/lib/xmodule/xmodule/video_module/video_module.py @@ -38,7 +38,7 @@ from xmodule.exceptions import NotFoundError from xmodule.contentstore.content import StaticContent from .transcripts_utils import VideoTranscriptsMixin, Transcript, get_html5_ids -from .video_utils import create_youtube_string, get_poster, rewrite_video_url +from .video_utils import create_youtube_string, get_poster, rewrite_video_url, format_xml_exception_message from .bumper_utils import bumperize from .video_xfields import VideoFields from .video_handlers import VideoStudentViewHandlers, VideoStudioViewHandlers @@ -563,14 +563,16 @@ class VideoDescriptor(VideoFields, VideoTranscriptsMixin, VideoStudioViewHandler if key in self.fields and self.fields[key].is_set_on(self): try: xml.set(key, unicode(value)) - except ValueError as exception: - exception_message = "{message}, Block-location:{location}, Key:{key}, Value:{value}".format( - message=exception.message, - location=unicode(self.location), - key=key, - value=unicode(value) - ) - raise ValueError(exception_message) + except UnicodeDecodeError: + exception_message = format_xml_exception_message(self.location, key, value) + log.exception(exception_message) + # If exception is UnicodeDecodeError set value using unicode 'utf-8' scheme. + log.info("Setting xml value using 'utf-8' scheme.") + xml.set(key, unicode(value, 'utf-8')) + except ValueError: + exception_message = format_xml_exception_message(self.location, key, value) + log.exception(exception_message) + raise for source in self.html5_sources: ele = etree.Element('source') diff --git a/common/lib/xmodule/xmodule/video_module/video_utils.py b/common/lib/xmodule/xmodule/video_module/video_utils.py index 0749b54f2d..1c24303ada 100644 --- a/common/lib/xmodule/xmodule/video_module/video_utils.py +++ b/common/lib/xmodule/xmodule/video_module/video_utils.py @@ -98,6 +98,19 @@ def get_poster(video): return poster +def format_xml_exception_message(location, key, value): + """ + Generate exception message for VideoDescriptor class which will use for ValueError and UnicodeDecodeError + when setting xml attributes. + """ + exception_message = "Block-location:{location}, Key:{key}, Value:{value}".format( + location=unicode(location), + key=key, + value=value + ) + return exception_message + + def set_query_parameter(url, param_name, param_value): """ Given a URL, set or replace a query parameter and return the