diff --git a/cms/djangoapps/contentstore/management/commands/import.py b/cms/djangoapps/contentstore/management/commands/import.py index 1cfdf24e2d..a7f95ea3c0 100644 --- a/cms/djangoapps/contentstore/management/commands/import.py +++ b/cms/djangoapps/contentstore/management/commands/import.py @@ -4,12 +4,8 @@ from django.core.management.base import BaseCommand, CommandError from keystore.django import keystore -from raw_module import RawDescriptor from lxml import etree -from fs.osfs import OSFS - -from path import path -from x_module import XModuleDescriptor, XMLParsingSystem +from keystore.xml import XMLModuleStore unnamed_modules = 0 @@ -26,30 +22,11 @@ class Command(BaseCommand): raise CommandError("import requires 3 arguments: ") org, course, data_dir = args - data_dir = path(data_dir) - with open(data_dir / "course.xml") as course_file: - class ImportSystem(XMLParsingSystem): - def __init__(self): - def process_xml(xml): - try: - xml_data = etree.fromstring(xml) - except: - raise CommandError("Unable to parse xml: " + xml) - - if not xml_data.get('name'): - global unnamed_modules - unnamed_modules += 1 - xml_data.set('name', '{tag}_{count}'.format(tag=xml_data.tag, count=unnamed_modules)) - - module = XModuleDescriptor.load_from_xml(etree.tostring(xml_data), self, org, course, RawDescriptor) - keystore().create_item(module.url) - if 'data' in module.definition: - keystore().update_item(module.url, module.definition['data']) - if 'children' in module.definition: - keystore().update_children(module.url, module.definition['children']) - return module - - XMLParsingSystem.__init__(self, keystore().get_item, OSFS(data_dir), process_xml) - - ImportSystem().process_xml(course_file.read()) + module_store = XMLModuleStore(org, course, data_dir, 'xmodule.raw_module.RawDescriptor') + for module in module_store.modules.itervalues(): + keystore().create_item(module.url) + if 'data' in module.definition: + keystore().update_item(module.url, module.definition['data']) + if 'children' in module.definition: + keystore().update_children(module.url, module.definition['children']) diff --git a/cms/djangoapps/contentstore/views.py b/cms/djangoapps/contentstore/views.py index 9cc7eec9b2..b85e9c05bf 100644 --- a/cms/djangoapps/contentstore/views.py +++ b/cms/djangoapps/contentstore/views.py @@ -10,7 +10,7 @@ def index(request): # TODO (cpennington): These need to be read in from the active user org = 'mit.edu' course = '6002xs12' - name = '6.002 Spring 2012' + name = '6.002_Spring_2012' course = keystore().get_item(['i4x', org, course, 'course', name]) weeks = course.get_children() return render_to_response('index.html', {'weeks': weeks}) diff --git a/common/lib/keystore/__init__.py b/common/lib/keystore/__init__.py index df9b72839e..14716fbc2d 100644 --- a/common/lib/keystore/__init__.py +++ b/common/lib/keystore/__init__.py @@ -15,7 +15,7 @@ URL_RE = re.compile(""" (/(?P[^/]+))? """, re.VERBOSE) -INVALID_CHARS = re.compile(r"[^\w-]") +INVALID_CHARS = re.compile(r"[^\w.-]") class Location(object): @@ -55,7 +55,7 @@ class Location(object): In both the dict and list forms, the revision is optional, and can be ommitted. - Components must be composed of alphanumeric characters, or the characters _, and - + Components must be composed of alphanumeric characters, or the characters '_', '-', and '.' Components may be set to None, which may be interpreted by some contexts to mean wildcard selection diff --git a/common/lib/keystore/xml.py b/common/lib/keystore/xml.py index 988916ed39..dcddb2718e 100644 --- a/common/lib/keystore/xml.py +++ b/common/lib/keystore/xml.py @@ -10,6 +10,8 @@ from .exceptions import ItemNotFoundError etree.set_default_parser(etree.XMLParser(dtd_validation=False, load_dtd=False, remove_comments=True)) +log = logging.getLogger(__name__) + class XMLModuleStore(ModuleStore): """ @@ -23,7 +25,7 @@ class XMLModuleStore(ModuleStore): class_ = getattr(import_module(module_path), class_name) self.default_class = class_ - with open(data_dir / "course.xml") as course_file: + with open(self.data_dir / "course.xml") as course_file: class ImportSystem(XMLParsingSystem): def __init__(self, keystore): self.unnamed_modules = 0 @@ -32,7 +34,7 @@ class XMLModuleStore(ModuleStore): try: xml_data = etree.fromstring(xml) except: - print xml + log.exception("Unable to parse xml:" + xml) raise if xml_data.get('name'): xml_data.set('slug', Location.clean(xml_data.get('name')))