From d9d4a007b912b9e581fcbbae5cf6141b5b0fb1ec Mon Sep 17 00:00:00 2001 From: Nimisha Asthagiri Date: Wed, 1 Apr 2015 17:39:21 -0400 Subject: [PATCH] PLAT-520 Fix Asset store import of null values --- .../contentstore/views/tests/test_videos.py | 10 +++++----- cms/djangoapps/contentstore/views/videos.py | 5 +++-- common/lib/xmodule/xmodule/assetstore/__init__.py | 6 +++--- .../xmodule/assetstore/tests/test_asset_xml.py | 12 ++++++++++++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/cms/djangoapps/contentstore/views/tests/test_videos.py b/cms/djangoapps/contentstore/views/tests/test_videos.py index ca55255090..24b504305b 100644 --- a/cms/djangoapps/contentstore/views/tests/test_videos.py +++ b/cms/djangoapps/contentstore/views/tests/test_videos.py @@ -289,12 +289,12 @@ class VideosHandlerTestCase(VideoUploadTestMixin, CourseTestCase): headers={"Content-Type": file_info["content_type"]} ) - # Ensure asset store was updated - self.assertIsNotNone( - modulestore().find_asset_metadata( - self.course.id.make_asset_key(VIDEO_ASSET_TYPE, video_id) - ) + # Ensure asset store was updated and the created_by field was set + asset_metadata = modulestore().find_asset_metadata( + self.course.id.make_asset_key(VIDEO_ASSET_TYPE, video_id) ) + self.assertIsNotNone(asset_metadata) + self.assertEquals(asset_metadata.created_by, self.user.id) # Ensure VAL was updated val_info = get_video_info(video_id) diff --git a/cms/djangoapps/contentstore/views/videos.py b/cms/djangoapps/contentstore/views/videos.py index 8cf5c2e152..d1091881ef 100644 --- a/cms/djangoapps/contentstore/views/videos.py +++ b/cms/djangoapps/contentstore/views/videos.py @@ -334,8 +334,9 @@ def videos_post(course, request): ) # persist edx_video_id as uploaded through this course - video_meta_data = AssetMetadata(course.id.make_asset_key(VIDEO_ASSET_TYPE, edx_video_id)) - modulestore().save_asset_metadata(video_meta_data, request.user.id) + user_id = request.user.id + video_meta_data = AssetMetadata(course.id.make_asset_key(VIDEO_ASSET_TYPE, edx_video_id), created_by=user_id) + modulestore().save_asset_metadata(video_meta_data, user_id) # persist edx_video_id in VAL create_video({ diff --git a/common/lib/xmodule/xmodule/assetstore/__init__.py b/common/lib/xmodule/xmodule/assetstore/__init__.py index 6a98fa40ec..dd1e0acd63 100644 --- a/common/lib/xmodule/xmodule/assetstore/__init__.py +++ b/common/lib/xmodule/xmodule/assetstore/__init__.py @@ -195,6 +195,9 @@ class AssetMetadata(object): elif tag == 'locked': # Boolean. value = True if value == "true" else False + elif value == 'None': + # None. + value = None elif tag in ('created_on', 'edited_on'): # ISO datetime. value = dateutil.parser.parse(value) @@ -204,9 +207,6 @@ class AssetMetadata(object): elif tag == 'fields': # Dictionary. value = json.loads(value) - elif value == 'None': - # None. - value = None setattr(self, tag, value) @contract(node='AssetElement') diff --git a/common/lib/xmodule/xmodule/assetstore/tests/test_asset_xml.py b/common/lib/xmodule/xmodule/assetstore/tests/test_asset_xml.py index 703fa103c7..8ef84f355a 100644 --- a/common/lib/xmodule/xmodule/assetstore/tests/test_asset_xml.py +++ b/common/lib/xmodule/xmodule/assetstore/tests/test_asset_xml.py @@ -57,6 +57,18 @@ class TestAssetXml(unittest.TestCase): new_value = getattr(new_asset_md, attr) self.assertEqual(orig_value, new_value) + def test_export_with_None_value(self): + """ + Export and import a single AssetMetadata to XML with a None created_by field, without causing an exception. + """ + asset_md = AssetMetadata( + self.course_id.make_asset_key('asset', 'none_value'), + created_by=None, + ) + asset = etree.Element("asset") + asset_md.to_xml(asset) + asset_md.from_xml(asset) + def test_export_all_assets_to_xml(self): """ Export all AssetMetadatas to XML and verify the structure and fields.