From 15852cd816aa0e9d8372003c183a87119ee2baf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Andr=C3=A9s=20Rocha?= Date: Mon, 12 Aug 2013 10:10:43 -0400 Subject: [PATCH] Modify export_to_xml to take an optional contentstore and work with xml courses --- .../contentstore/tests/test_contentstore.py | 41 +++++++++++++++++++ .../xmodule/modulestore/xml_exporter.py | 22 ++++++---- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/cms/djangoapps/contentstore/tests/test_contentstore.py b/cms/djangoapps/contentstore/tests/test_contentstore.py index 57e32429af..7f09e4dd60 100644 --- a/cms/djangoapps/contentstore/tests/test_contentstore.py +++ b/cms/djangoapps/contentstore/tests/test_contentstore.py @@ -1273,6 +1273,47 @@ class ContentStoreToyCourseTest(ModuleStoreTestCase): # export out to a tempdir export_to_xml(module_store, content_store, location, root_dir, 'test_export') + def test_export_course_without_content_store(self): + module_store = modulestore('direct') + content_store = contentstore() + + # Create toy course + + import_from_xml(module_store, 'common/test/data/', ['toy']) + location = CourseDescriptor.id_to_location('edX/toy/2012_Fall') + + # Add a sequence + + stub_location = Location(['i4x', 'edX', 'toy', 'sequential', 'vertical_sequential']) + sequential = module_store.get_item(stub_location) + module_store.update_children(sequential.location, sequential.children) + + # Get course and export it without a content_store + + course = module_store.get_item(location) + course.save() + + root_dir = path(mkdtemp_clean()) + + print 'Exporting to tempdir = {0}'.format(root_dir) + export_to_xml(module_store, None, location, root_dir, 'test_export_no_content_store') + + # Delete the course from module store and reimport it + + delete_course(module_store, content_store, location, commit=True) + + import_from_xml( + module_store, root_dir, ['test_export_no_content_store'], + draft_store=None, + static_content_store=None, + target_location_namespace=course.location + ) + + # Verify reimported course + + items = module_store.get_items(stub_location) + self.assertEqual(len(items), 1) + @override_settings(CONTENTSTORE=TEST_DATA_CONTENTSTORE, MODULESTORE=TEST_MODULESTORE) class ContentStoreTest(ModuleStoreTestCase): diff --git a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py index a54d188b61..feb0a2ccfa 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py @@ -38,7 +38,7 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d Export all modules from `modulestore` and content from `contentstore` as xml to `root_dir`. `modulestore`: A `ModuleStore` object that is the source of the modules to export - `contentstore`: A `ContentStore` object that is the source of the content to export + `contentstore`: A `ContentStore` object that is the source of the content to export, can be None `course_location`: The `Location` of the `CourseModuleDescriptor` to export `root_dir`: The directory to write the exported xml to `course_dir`: The name of the directory inside `root_dir` to write the course content to @@ -46,7 +46,12 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d alongside the public content in the course. """ - course = modulestore.get_item(course_location) + # we use get_instance instead of get_item to support modulestores + # that can't guarantee that definitions are unique + course = modulestore.get_instance( + course_location.course_id, + course_location + ) fs = OSFS(root_dir) export_fs = fs.makeopendir(course_dir) @@ -55,13 +60,14 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d with export_fs.open('course.xml', 'w') as course_xml: course_xml.write(xml) - policies_dir = export_fs.makeopendir('policies') # export the static assets - contentstore.export_all_for_course( - course_location, - root_dir + '/' + course_dir + '/static/', - root_dir + '/' + course_dir + '/policies/assets.json', - ) + policies_dir = export_fs.makeopendir('policies') + if contentstore: + contentstore.export_all_for_course( + course_location, + root_dir + '/' + course_dir + '/static/', + root_dir + '/' + course_dir + '/policies/assets.json', + ) # export the static tabs export_extra_content(export_fs, modulestore, course_location, 'static_tab', 'tabs', '.html')