From 241d445f302322c67f0697368e0df99784f55804 Mon Sep 17 00:00:00 2001 From: ichuang Date: Fri, 10 Aug 2012 19:00:50 -0400 Subject: [PATCH] changes to xml_module to allow problem metadata to be added via --- common/lib/xmodule/xmodule/xml_module.py | 29 +++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/common/lib/xmodule/xmodule/xml_module.py b/common/lib/xmodule/xmodule/xml_module.py index 69c770e279..968c42adcb 100644 --- a/common/lib/xmodule/xmodule/xml_module.py +++ b/common/lib/xmodule/xmodule/xml_module.py @@ -1,6 +1,7 @@ from xmodule.x_module import XModuleDescriptor from xmodule.modulestore import Location from lxml import etree +import json import copy import logging import traceback @@ -32,7 +33,15 @@ def is_pointer_tag(xml_obj): actual_attr = set(xml_obj.attrib.keys()) return len(xml_obj) == 0 and actual_attr == expected_attr - +def get_metadata_from_xml(xml_object, remove=True): + meta = xml_object.find('meta') + if meta is None: + return '' + dmdata = meta.text + log.debug('meta for %s loaded: %s' % (xml_object,dmdata)) + if remove: + xml_object.remove(meta) + return dmdata _AttrMapBase = namedtuple('_AttrMap', 'from_xml to_xml') @@ -180,8 +189,11 @@ class XmlDescriptor(XModuleDescriptor): definition_xml = cls.load_file(filepath, system.resources_fs, location) + definition_metadata = get_metadata_from_xml(definition_xml) cls.clean_metadata_from_xml(definition_xml) definition = cls.definition_from_xml(definition_xml, system) + if definition_metadata: + definition['definition_metadata'] = definition_metadata # TODO (ichuang): remove this after migration # for Fall 2012 LMS migration: keep filename (and unmangled filename) @@ -236,9 +248,9 @@ class XmlDescriptor(XModuleDescriptor): filepath = cls._format_filepath(xml_object.tag, url_name) definition_xml = cls.load_file(filepath, system.resources_fs, location) else: - definition_xml = xml_object + definition_xml = xml_object # this is just a pointer, not the real definition content - definition = cls.load_definition(definition_xml, system, location) + definition = cls.load_definition(definition_xml, system, location) # note this removes metadata # VS[compat] -- make Ike's github preview links work in both old and # new file layouts if is_pointer_tag(xml_object): @@ -246,6 +258,17 @@ class XmlDescriptor(XModuleDescriptor): definition['filename'] = [filepath, filepath] metadata = cls.load_metadata(definition_xml) + + # move definition metadata into dict + dmdata = definition.get('definition_metadata','') + if dmdata: + metadata['definition_metadata_raw'] = dmdata + try: + metadata.update(json.loads(dmdata)) + except Exception as err: + log.debug('Error %s in loading metadata %s' % (err,dmdata)) + metadata['definition_metadata_err'] = str(err) + return cls( system, definition,