Commit 4d3e410b by Calen Pennington

Make mapping from xml attributes to metadata values less manual

parent 94af3ce8
...@@ -71,6 +71,13 @@ class XmlDescriptor(XModuleDescriptor): ...@@ -71,6 +71,13 @@ class XmlDescriptor(XModuleDescriptor):
metadata_attributes = ('format', 'graceperiod', 'showanswer', 'rerandomize', metadata_attributes = ('format', 'graceperiod', 'showanswer', 'rerandomize',
'due', 'graded', 'name', 'slug') 'due', 'graded', 'name', 'slug')
# A dictionary mapping xml attribute names to function of the value
# that return the metadata key and value
xml_attribute_map = {
'graded': lambda val: ('graded', val == 'true'),
'name': lambda val: ('display_name', val),
}
@classmethod @classmethod
def definition_from_xml(cls, xml_object, system): def definition_from_xml(cls, xml_object, system):
""" """
...@@ -116,16 +123,15 @@ class XmlDescriptor(XModuleDescriptor): ...@@ -116,16 +123,15 @@ class XmlDescriptor(XModuleDescriptor):
def metadata_loader(): def metadata_loader():
metadata = {} metadata = {}
for attr in ('format', 'graceperiod', 'showanswer', 'rerandomize', 'due'): for attr in cls.metadata_attributes:
from_xml = xml_object.get(attr) val = xml_object.get(attr)
if from_xml is not None: if val is not None:
metadata[attr] = from_xml map_fn = cls.xml_attribute_map.get(attr)
if map_fn is None:
if xml_object.get('graded') is not None: metadata[attr] = val
metadata['graded'] = xml_object.get('graded') == 'true' else:
key, val = map_fn(val)
if xml_object.get('name') is not None: metadata[key] = val
metadata['display_name'] = xml_object.get('name')
return metadata return metadata
......
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