Commit 60cba3d9 by Victor Shnayder

Move import_from_xml to xmodule/modulestore

parent 3cc601b3
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
...@@ -3,7 +3,9 @@ ...@@ -3,7 +3,9 @@
### ###
from django.core.management.base import BaseCommand, CommandError 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 unnamed_modules = 0
...@@ -21,4 +23,4 @@ class Command(BaseCommand): ...@@ -21,4 +23,4 @@ class Command(BaseCommand):
course_dirs = args[1:] course_dirs = args[1:]
else: else:
course_dirs = None course_dirs = None
import_from_xml(data_dir, course_dirs) import_from_xml(modulestore(), data_dir, course_dirs)
...@@ -12,7 +12,7 @@ from django.contrib.auth.models import User ...@@ -12,7 +12,7 @@ from django.contrib.auth.models import User
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
import xmodule.modulestore.django import xmodule.modulestore.django
from xmodule.modulestore import Location from xmodule.modulestore import Location
from contentstore import import_from_xml from xmodule.modulestore.xml_importer import import_from_xml
import copy import copy
...@@ -196,7 +196,7 @@ class EditTestCase(ContentStoreTestCase): ...@@ -196,7 +196,7 @@ class EditTestCase(ContentStoreTestCase):
xmodule.modulestore.django.modulestore().collection.drop() xmodule.modulestore.django.modulestore().collection.drop()
def check_edit_item(self, test_course_name): 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)): for descriptor in modulestore().get_items(Location(None, None, None, None, None)):
print "Checking ", descriptor.location.url() print "Checking ", descriptor.location.url()
......
...@@ -5,7 +5,8 @@ from django.conf import settings ...@@ -5,7 +5,8 @@ from django.conf import settings
from fs.osfs import OSFS from fs.osfs import OSFS
from git import Repo, PushInfo 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 xmodule.modulestore import Location
from .exceptions import GithubSyncError from .exceptions import GithubSyncError
...@@ -56,7 +57,8 @@ def import_from_github(repo_settings): ...@@ -56,7 +57,8 @@ def import_from_github(repo_settings):
git_repo = setup_repo(repo_settings) git_repo = setup_repo(repo_settings)
git_repo.head.reset('origin/%s' % repo_settings['branch'], index=True, working_tree=True) 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] return git_repo.head.commit.hexsha, module_store.courses[course_dir]
......
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
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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment