Commit 0ff4af9a by Calen Pennington

Have the CMS use the same XMLModuleStore for import that the LMS uses for reading content

parent 5c916104
...@@ -4,12 +4,8 @@ ...@@ -4,12 +4,8 @@
from django.core.management.base import BaseCommand, CommandError from django.core.management.base import BaseCommand, CommandError
from keystore.django import keystore from keystore.django import keystore
from raw_module import RawDescriptor
from lxml import etree from lxml import etree
from fs.osfs import OSFS from keystore.xml import XMLModuleStore
from path import path
from x_module import XModuleDescriptor, XMLParsingSystem
unnamed_modules = 0 unnamed_modules = 0
...@@ -26,30 +22,11 @@ class Command(BaseCommand): ...@@ -26,30 +22,11 @@ class Command(BaseCommand):
raise CommandError("import requires 3 arguments: <org> <course> <data directory>") raise CommandError("import requires 3 arguments: <org> <course> <data directory>")
org, course, data_dir = args org, course, data_dir = args
data_dir = path(data_dir)
with open(data_dir / "course.xml") as course_file: module_store = XMLModuleStore(org, course, data_dir, 'xmodule.raw_module.RawDescriptor')
for module in module_store.modules.itervalues():
class ImportSystem(XMLParsingSystem): keystore().create_item(module.url)
def __init__(self): if 'data' in module.definition:
def process_xml(xml): keystore().update_item(module.url, module.definition['data'])
try: if 'children' in module.definition:
xml_data = etree.fromstring(xml) keystore().update_children(module.url, module.definition['children'])
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())
...@@ -10,7 +10,7 @@ def index(request): ...@@ -10,7 +10,7 @@ def index(request):
# TODO (cpennington): These need to be read in from the active user # TODO (cpennington): These need to be read in from the active user
org = 'mit.edu' org = 'mit.edu'
course = '6002xs12' course = '6002xs12'
name = '6.002 Spring 2012' name = '6.002_Spring_2012'
course = keystore().get_item(['i4x', org, course, 'course', name]) course = keystore().get_item(['i4x', org, course, 'course', name])
weeks = course.get_children() weeks = course.get_children()
return render_to_response('index.html', {'weeks': weeks}) return render_to_response('index.html', {'weeks': weeks})
......
...@@ -15,7 +15,7 @@ URL_RE = re.compile(""" ...@@ -15,7 +15,7 @@ URL_RE = re.compile("""
(/(?P<revision>[^/]+))? (/(?P<revision>[^/]+))?
""", re.VERBOSE) """, re.VERBOSE)
INVALID_CHARS = re.compile(r"[^\w-]") INVALID_CHARS = re.compile(r"[^\w.-]")
class Location(object): class Location(object):
...@@ -55,7 +55,7 @@ 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. 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 Components may be set to None, which may be interpreted by some contexts to mean
wildcard selection wildcard selection
......
...@@ -10,6 +10,8 @@ from .exceptions import ItemNotFoundError ...@@ -10,6 +10,8 @@ from .exceptions import ItemNotFoundError
etree.set_default_parser(etree.XMLParser(dtd_validation=False, load_dtd=False, etree.set_default_parser(etree.XMLParser(dtd_validation=False, load_dtd=False,
remove_comments=True)) remove_comments=True))
log = logging.getLogger(__name__)
class XMLModuleStore(ModuleStore): class XMLModuleStore(ModuleStore):
""" """
...@@ -23,7 +25,7 @@ class XMLModuleStore(ModuleStore): ...@@ -23,7 +25,7 @@ class XMLModuleStore(ModuleStore):
class_ = getattr(import_module(module_path), class_name) class_ = getattr(import_module(module_path), class_name)
self.default_class = class_ 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): class ImportSystem(XMLParsingSystem):
def __init__(self, keystore): def __init__(self, keystore):
self.unnamed_modules = 0 self.unnamed_modules = 0
...@@ -32,7 +34,7 @@ class XMLModuleStore(ModuleStore): ...@@ -32,7 +34,7 @@ class XMLModuleStore(ModuleStore):
try: try:
xml_data = etree.fromstring(xml) xml_data = etree.fromstring(xml)
except: except:
print xml log.exception("Unable to parse xml:" + xml)
raise raise
if xml_data.get('name'): if xml_data.get('name'):
xml_data.set('slug', Location.clean(xml_data.get('name'))) xml_data.set('slug', Location.clean(xml_data.get('name')))
......
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