From 6fdf44fe8d621ead310ea9de7b7674fd6adc8779 Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Tue, 26 Jun 2012 14:35:21 -0400 Subject: [PATCH] Make import work via mako again, to unblock others while I work on making the LMS work using XModuleDescriptors --- .../management/commands/import.py | 50 ++++++++++--------- 1 file changed, 26 insertions(+), 24 deletions(-) diff --git a/cms/djangoapps/contentstore/management/commands/import.py b/cms/djangoapps/contentstore/management/commands/import.py index 18dd321467..e6ace4b66c 100644 --- a/cms/djangoapps/contentstore/management/commands/import.py +++ b/cms/djangoapps/contentstore/management/commands/import.py @@ -7,6 +7,7 @@ from keystore.django import keystore from raw_module import RawDescriptor from lxml import etree from fs.osfs import OSFS +from mako.lookup import TemplateLookup from path import path from x_module import XModuleDescriptor, XMLParsingSystem @@ -23,30 +24,31 @@ class Command(BaseCommand): def handle(self, *args, **options): 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): + self.load_item = keystore().get_item + self.fs = OSFS(data_dir) - class ImportSystem(XMLParsingSystem): - def __init__(self): - self.load_item = keystore().get_item - self.fs = OSFS(data_dir) + def process_xml(self, xml): + try: + xml_data = etree.fromstring(xml) + except: + print xml + raise + 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)) - def process_xml(self, xml): - try: - xml_data = etree.fromstring(xml) - except: - print xml - raise - 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 - 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 - - ImportSystem().process_xml(course_file.read()) + lookup = TemplateLookup(directories=[data_dir]) + template = lookup.get_template("course.xml") + course_string = template.render(groups=[]) + ImportSystem().process_xml(course_string)