From c676046341d8858f49c21e7229b1163a471256c7 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Wed, 30 Jan 2013 13:25:17 -0500 Subject: [PATCH 1/2] adding exporting of tabs and custom_tags. Also added unit tests --- cms/djangoapps/contentstore/tests/tests.py | 24 +++++++++++++++++++ .../xmodule/modulestore/xml_exporter.py | 22 +++++++++++++++++ 2 files changed, 46 insertions(+) diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index 3025ee78a4..8c520eb39c 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -8,6 +8,8 @@ from django.core.urlresolvers import reverse from path import path from tempfile import mkdtemp import json +from fs.osfs import OSFS + from student.models import Registration from django.contrib.auth.models import User @@ -430,6 +432,28 @@ class ContentStoreTest(TestCase): # export out to a tempdir export_to_xml(ms, cs, location, root_dir, 'test_export') + # check for static tabs + fs = OSFS(root_dir / 'test_export') + self.assertTrue(fs.exists('tabs')) + + static_tabs_query_loc = Location('i4x', location.org, location.course, 'static_tab', None) + static_tabs = ms.get_items(static_tabs_query_loc) + + for static_tab in static_tabs: + fs = OSFS(root_dir / 'test_export/tabs') + self.assertTrue(fs.exists(static_tab.location.name + '.html')) + + # check for custom_tags + fs = OSFS(root_dir / 'test_export') + self.assertTrue(fs.exists('custom_tags')) + + custom_tags_query_loc = Location('i4x', location.org, location.course, 'custom_tag_template', None) + custom_tags = ms.get_items(custom_tags_query_loc) + + for custom_tag in custom_tags: + fs = OSFS(root_dir / 'test_export/custom_tags') + self.assertTrue(fs.exists(custom_tag.location.name)) + # remove old course delete_course(ms, cs, location) diff --git a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py index e0bf0ec1d3..98d0d52818 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py @@ -17,4 +17,26 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d # export the static assets contentstore.export_all_for_course(course_location, root_dir + '/' + course_dir + '/static/') + # export the static tabs + static_tabs_query_loc = Location('i4x', course_location.org, course_location.course, 'static_tab', None) + static_tabs = modulestore.get_items(static_tabs_query_loc) + + if len(static_tabs) > 0: + tab_dir = export_fs.makeopendir('tabs') + for tab in static_tabs: + with tab_dir.open(tab.location.name + '.html', 'w') as tab_file: + tab_file.write(tab.definition['data'].encode('utf8')) + + # export custom tags + custom_tags_query_loc = Location('i4x', course_location.org, course_location.course, 'custom_tag_template', None) + custom_tags = modulestore.get_items(custom_tags_query_loc) + + if len(custom_tags) > 0: + tab_dir = export_fs.makeopendir('custom_tags') + for tag in custom_tags: + with tab_dir.open(tag.location.name, 'w') as tag_file: + tag_file.write(tag.definition['data'].encode('utf8')) + + + \ No newline at end of file From cc09a90245786e016f2d70ae15089b0043ab6196 Mon Sep 17 00:00:00 2001 From: Chris Dodge Date: Wed, 30 Jan 2013 13:50:30 -0500 Subject: [PATCH 2/2] DRY things up a bit --- .../xmodule/modulestore/xml_exporter.py | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py index 98d0d52818..5e85cd6fc5 100644 --- a/common/lib/xmodule/xmodule/modulestore/xml_exporter.py +++ b/common/lib/xmodule/xmodule/modulestore/xml_exporter.py @@ -18,24 +18,21 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d contentstore.export_all_for_course(course_location, root_dir + '/' + course_dir + '/static/') # export the static tabs - static_tabs_query_loc = Location('i4x', course_location.org, course_location.course, 'static_tab', None) - static_tabs = modulestore.get_items(static_tabs_query_loc) + export_extra_content(export_fs, modulestore, course_location, 'static_tab', 'tabs', '.html') - if len(static_tabs) > 0: - tab_dir = export_fs.makeopendir('tabs') - for tab in static_tabs: - with tab_dir.open(tab.location.name + '.html', 'w') as tab_file: - tab_file.write(tab.definition['data'].encode('utf8')) + # export the custom tags + export_extra_content(export_fs, modulestore, course_location, 'custom_tag_template', 'custom_tags') - # export custom tags - custom_tags_query_loc = Location('i4x', course_location.org, course_location.course, 'custom_tag_template', None) - custom_tags = modulestore.get_items(custom_tags_query_loc) - if len(custom_tags) > 0: - tab_dir = export_fs.makeopendir('custom_tags') - for tag in custom_tags: - with tab_dir.open(tag.location.name, 'w') as tag_file: - tag_file.write(tag.definition['data'].encode('utf8')) +def export_extra_content(export_fs, modulestore, course_location, category_type, dirname, file_suffix = ''): + query_loc = Location('i4x', course_location.org, course_location.course, category_type, None) + items = modulestore.get_items(query_loc) + + if len(items) > 0: + item_dir = export_fs.makeopendir(dirname) + for item in items: + with item_dir.open(item.location.name + file_suffix, 'w') as item_file: + item_file.write(item.definition['data'].encode('utf8'))