From 60cba3d95f555ec1bd7bc2208fd414e76a0fe48b Mon Sep 17 00:00:00 2001 From: Victor Shnayder Date: Thu, 19 Jul 2012 18:19:00 -0400 Subject: [PATCH] Move import_from_xml to xmodule/modulestore --- cms/djangoapps/contentstore/__init__.py | 34 ----------------- .../management/commands/import.py | 6 ++- cms/djangoapps/contentstore/tests/tests.py | 4 +- cms/djangoapps/github_sync/__init__.py | 6 ++- .../xmodule/modulestore/tests/test_mongo.py | 28 ++++++++++++++ .../xmodule/modulestore/xml_importer.py | 38 +++++++++++++++++++ 6 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py create mode 100644 common/lib/xmodule/xmodule/modulestore/xml_importer.py diff --git a/cms/djangoapps/contentstore/__init__.py b/cms/djangoapps/contentstore/__init__.py index ef4e31614e..e69de29bb2 100644 --- a/cms/djangoapps/contentstore/__init__.py +++ b/cms/djangoapps/contentstore/__init__.py @@ -1,34 +0,0 @@ -from xmodule.modulestore.django import modulestore -from xmodule.modulestore.xml import XMLModuleStore -import logging - -log = logging.getLogger(__name__) - - -def import_from_xml(data_dir, course_dirs=None): - """ - Import the specified xml data_dir into the django defined modulestore, - using org and course as the location org and course. - """ - module_store = XMLModuleStore( - data_dir, - default_class='xmodule.raw_module.RawDescriptor', - eager=True, - course_dirs=course_dirs - ) - for module in module_store.modules.itervalues(): - - # TODO (cpennington): This forces import to overrite the same items. - # This should in the future create new revisions of the items on import - try: - modulestore().create_item(module.location) - except: - log.exception('Item already exists at %s' % module.location.url()) - pass - if 'data' in module.definition: - modulestore().update_item(module.location, module.definition['data']) - if 'children' in module.definition: - modulestore().update_children(module.location, module.definition['children']) - modulestore().update_metadata(module.location, dict(module.metadata)) - - return module_store diff --git a/cms/djangoapps/contentstore/management/commands/import.py b/cms/djangoapps/contentstore/management/commands/import.py index 75d4e2618c..0e4dc5cc89 100644 --- a/cms/djangoapps/contentstore/management/commands/import.py +++ b/cms/djangoapps/contentstore/management/commands/import.py @@ -3,7 +3,9 @@ ### from django.core.management.base import BaseCommand, CommandError -from contentstore import import_from_xml +from xmodule.modulestore.xml_importer import import_from_xml +from xmodule.modulestore.django import modulestore + unnamed_modules = 0 @@ -21,4 +23,4 @@ class Command(BaseCommand): course_dirs = args[1:] else: course_dirs = None - import_from_xml(data_dir, course_dirs) + import_from_xml(modulestore(), data_dir, course_dirs) diff --git a/cms/djangoapps/contentstore/tests/tests.py b/cms/djangoapps/contentstore/tests/tests.py index 9dfe5d614a..50eba43795 100644 --- a/cms/djangoapps/contentstore/tests/tests.py +++ b/cms/djangoapps/contentstore/tests/tests.py @@ -12,7 +12,7 @@ from django.contrib.auth.models import User from xmodule.modulestore.django import modulestore import xmodule.modulestore.django from xmodule.modulestore import Location -from contentstore import import_from_xml +from xmodule.modulestore.xml_importer import import_from_xml import copy @@ -196,7 +196,7 @@ class EditTestCase(ContentStoreTestCase): xmodule.modulestore.django.modulestore().collection.drop() def check_edit_item(self, test_course_name): - import_from_xml('common/test/data/', [test_course_name]) + import_from_xml(modulestore(), 'common/test/data/', [test_course_name]) for descriptor in modulestore().get_items(Location(None, None, None, None, None)): print "Checking ", descriptor.location.url() diff --git a/cms/djangoapps/github_sync/__init__.py b/cms/djangoapps/github_sync/__init__.py index 149b92670a..1089db7efd 100644 --- a/cms/djangoapps/github_sync/__init__.py +++ b/cms/djangoapps/github_sync/__init__.py @@ -5,7 +5,8 @@ from django.conf import settings from fs.osfs import OSFS from git import Repo, PushInfo -from contentstore import import_from_xml +from xmodule.modulestore.xml_importer import import_from_xml +from xmodule.modulestore.django import modulestore from xmodule.modulestore import Location from .exceptions import GithubSyncError @@ -56,7 +57,8 @@ def import_from_github(repo_settings): git_repo = setup_repo(repo_settings) git_repo.head.reset('origin/%s' % repo_settings['branch'], index=True, working_tree=True) - module_store = import_from_xml(settings.GITHUB_REPO_ROOT, course_dirs=[course_dir]) + module_store = import_from_xml(modulestore(), + settings.GITHUB_REPO_ROOT, course_dirs=[course_dir]) return git_repo.head.commit.hexsha, module_store.courses[course_dir] diff --git a/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py new file mode 100644 index 0000000000..ffcbe1717d --- /dev/null +++ b/common/lib/xmodule/xmodule/modulestore/tests/test_mongo.py @@ -0,0 +1,28 @@ +from nose.tools import assert_equals, assert_raises, assert_not_equals, with_setup +from xmodule.modulestore import Location +from xmodule.modulestore.exceptions import InvalidLocationError +from xmodule.modulestore.mongo import MongoModuleStore + + +host = 'localhost' +db = 'xmodule' +collection = 'modulestore' +fs_root = None # TODO (vshnayder): will need a real fs_root for testing load_item +default_class = 'xmodule.raw_module.RawDescriptor' + + + +def setup_func(): + # connect to the db + global store + store = MongoModuleStore(host, db, collection, fs_root, default_class=default_class) + +def teardown_func(): + global store + store = None + +@with_setup(setup_func, teardown_func) +def test_init(): + '''Just make sure the db loads''' + pass + diff --git a/common/lib/xmodule/xmodule/modulestore/xml_importer.py b/common/lib/xmodule/xmodule/modulestore/xml_importer.py new file mode 100644 index 0000000000..a21777b65f --- /dev/null +++ b/common/lib/xmodule/xmodule/modulestore/xml_importer.py @@ -0,0 +1,38 @@ +import logging + +from .xml import XMLModuleStore + +log = logging.getLogger(__name__) + + +def import_from_xml(store, data_dir, course_dirs=None): + """ + Import the specified xml data_dir into the "store" modulestore, + using org and course as the location org and course. + + course_dirs: If specified, the list of course_dirs to load. Otherwise, load + all course dirs + + """ + module_store = XMLModuleStore( + data_dir, + default_class='xmodule.raw_module.RawDescriptor', + eager=True, + course_dirs=course_dirs + ) + for module in module_store.modules.itervalues(): + + # TODO (cpennington): This forces import to overrite the same items. + # This should in the future create new revisions of the items on import + try: + store.create_item(module.location) + except: + log.exception('Item already exists at %s' % module.location.url()) + pass + if 'data' in module.definition: + store.update_item(module.location, module.definition['data']) + if 'children' in module.definition: + store.update_children(module.location, module.definition['children']) + store.update_metadata(module.location, dict(module.metadata)) + + return module_store