From 682a80adddfca953f86b8fb7464f06b6e9d34357 Mon Sep 17 00:00:00 2001 From: Irtaza Akram Date: Wed, 3 Apr 2024 16:03:56 +0500 Subject: [PATCH] fix: review changes --- cms/djangoapps/contentstore/helpers.py | 9 +++++++++ lms/djangoapps/courseware/tests/test_video_mongo.py | 7 +++---- xmodule/tests/test_import.py | 1 + xmodule/tests/test_poll.py | 4 +--- xmodule/tests/test_video.py | 8 ++------ 5 files changed, 16 insertions(+), 13 deletions(-) diff --git a/cms/djangoapps/contentstore/helpers.py b/cms/djangoapps/contentstore/helpers.py index f16198dcd9..a4ece6c85d 100644 --- a/cms/djangoapps/contentstore/helpers.py +++ b/cms/djangoapps/contentstore/helpers.py @@ -319,6 +319,11 @@ def _import_xml_node_to_parent( parent_key = parent_xblock.scope_ids.usage_id block_type = node.tag + # Modulestore's IdGenerator here is SplitMongoIdManager which is assigned + # by CachingDescriptorSystem Runtime and since we need our custom ImportIdGenerator + # here we are temporaraliy swtiching it. + original_id_generator = runtime.id_generator + # Generate the new ID: runtime.id_generator = ImportIdGenerator(parent_key.context_key) def_id = runtime.id_generator.create_definition(block_type, slug_hint) @@ -360,6 +365,10 @@ def _import_xml_node_to_parent( node_without_children = etree.Element(node.tag, **node.attrib) temp_xblock = xblock_class.parse_xml(node_without_children, runtime, keys) child_nodes = list(node) + + # Restore the original id_generator + runtime.id_generator = original_id_generator + if xblock_class.has_children and temp_xblock.children: raise NotImplementedError("We don't yet support pasting XBlocks with children") temp_xblock.parent = parent_key diff --git a/lms/djangoapps/courseware/tests/test_video_mongo.py b/lms/djangoapps/courseware/tests/test_video_mongo.py index 4667fa941a..d45bb94816 100644 --- a/lms/djangoapps/courseware/tests/test_video_mongo.py +++ b/lms/djangoapps/courseware/tests/test_video_mongo.py @@ -2025,9 +2025,8 @@ class VideoBlockTest(TestCase, VideoBlockTestBase): val_transcript_provider=val_transcript_provider ) xml_object = etree.fromstring(xml_data) - id_generator = Mock() - id_generator.target_course_id = "test_course_id" - module_system.id_generator = id_generator + module_system.id_generator.target_course_id = "test_course_id" + video = self.block.parse_xml(xml_object, module_system, None) assert video.edx_video_id == 'test_edx_video_id' @@ -2035,7 +2034,7 @@ class VideoBlockTest(TestCase, VideoBlockTestBase): assert video_data['client_video_id'] == 'test_client_video_id' assert video_data['duration'] == 111.0 assert video_data['status'] == 'imported' - assert video_data['courses'] == [{id_generator.target_course_id: None}] + assert video_data['courses'] == [{module_system.id_generator.target_course_id: None}] assert video_data['encoded_videos'][0]['profile'] == 'mobile' assert video_data['encoded_videos'][0]['url'] == 'http://example.com/video' assert video_data['encoded_videos'][0]['file_size'] == 222 diff --git a/xmodule/tests/test_import.py b/xmodule/tests/test_import.py index 95feedeb9c..3052a90acf 100644 --- a/xmodule/tests/test_import.py +++ b/xmodule/tests/test_import.py @@ -50,6 +50,7 @@ class DummySystem(ImportSystem): # lint-amnesty, pylint: disable=abstract-metho mixins=(InheritanceMixin, XModuleMixin), services={'field-data': KvsFieldData(DictKeyValueStore())}, ) + self.id_generator = Mock() class BaseCourseTestCase(TestCase): diff --git a/xmodule/tests/test_poll.py b/xmodule/tests/test_poll.py index b9cb5d8250..f265c97c6c 100644 --- a/xmodule/tests/test_poll.py +++ b/xmodule/tests/test_poll.py @@ -62,8 +62,7 @@ class PollBlockTest(unittest.TestCase): unescaped characters. """ module_system = DummySystem(load_error_blocks=True) - id_generator = Mock() - id_generator.target_course_id = self.xblock.course_id + module_system.id_generator.target_course_id = self.xblock.course_id sample_poll_xml = '''

How old are you?

@@ -72,7 +71,6 @@ class PollBlockTest(unittest.TestCase): ''' node = etree.fromstring(sample_poll_xml) - module_system.id_generator = id_generator output = PollBlock.parse_xml(node, module_system, self.scope_ids) # Update the answer with invalid character. invalid_characters_poll_answer = output.answers[0] diff --git a/xmodule/tests/test_video.py b/xmodule/tests/test_video.py index c2c7ac0846..5e95f77082 100644 --- a/xmodule/tests/test_video.py +++ b/xmodule/tests/test_video.py @@ -344,10 +344,8 @@ class VideoBlockImportTestCase(TestCase): ''' xml_object = etree.fromstring(xml_data) - id_generator = Mock() - id_generator.target_course_id = course_id + module_system.id_generator.target_course_id = course_id - module_system.id_generator = id_generator output = VideoBlock.parse_xml(xml_object, module_system, None) self.assert_attributes_equal(output, { 'youtube_id_0_75': 'izygArpw-Qo', @@ -644,9 +642,7 @@ class VideoBlockImportTestCase(TestCase): edx_video_id=edx_video_id ) xml_object = etree.fromstring(xml_data) - id_generator = Mock() - id_generator.target_course_id = 'test_course_id' - module_system.id_generator = id_generator + module_system.id_generator.target_course_id = 'test_course_id' video = VideoBlock.parse_xml(xml_object, module_system, None) self.assert_attributes_equal(video, {'edx_video_id': edx_video_id})