Commit d750d945 by Victor Shnayder

Remove malformed tags when contents aren't malformed anymore.

parent c53ed6a2
from path import path
import unittest
from lxml import etree
from xmodule.x_module import XMLParsingSystem, XModuleDescriptor
from xmodule.errorhandlers import ignore_errors_handler
from xmodule.modulestore import Location
......@@ -61,3 +62,26 @@ class ImportTestCase(unittest.TestCase):
self.assertEqual(descriptor.definition['data'],
re_import_descriptor.definition['data'])
def test_fixed_xml_tag(self):
"""Make sure a tag that's been fixed exports as the original tag type"""
# create a malformed tag with valid xml contents
root = etree.Element('malformed')
good_xml = '''<sequential display_name="fixed"><video url="hi"/></sequential>'''
root.text = good_xml
xml_str_in = etree.tostring(root)
# load it
system = self.get_system()
descriptor = XModuleDescriptor.load_from_xml(xml_str_in, system, 'org', 'course',
None)
# export it
resource_fs = None
xml_str_out = descriptor.export_to_xml(resource_fs)
# Now make sure the exported xml is a sequential
xml_out = etree.fromstring(xml_str_out)
self.assertEqual(xml_out.tag, 'sequential')
from pkg_resources import resource_string
from lxml import etree
from xmodule.x_module import XModule
from xmodule.mako_module import MakoModuleDescriptor
from xmodule.xml_module import XmlDescriptor
from xmodule.editing_module import EditingDescriptor
......@@ -8,10 +9,18 @@ import logging
log = logging.getLogger(__name__)
class MalformedModule(XModule):
def get_html(self):
'''Show an error.
TODO (vshnayder): proper style, divs, etc.
'''
return "Malformed content--not showing through get_html()"
class MalformedDescriptor(EditingDescriptor):
"""
Module that provides a raw editing view of broken xml.
"""
module_class = MalformedModule
@classmethod
def from_xml(cls, xml_data, system, org=None, course=None):
......@@ -20,8 +29,8 @@ class MalformedDescriptor(EditingDescriptor):
Does not try to parse the data--just stores it.
'''
#log.debug("processing '{0}'".format(xml_data))
try:
# If this is already a malformed tag, don't want to re-wrap it.
xml_obj = etree.fromstring(xml_data)
if xml_obj.tag == 'malformed':
xml_data = xml_obj.text
......@@ -40,9 +49,18 @@ class MalformedDescriptor(EditingDescriptor):
def export_to_xml(self, resource_fs):
'''
Export as a string wrapped in xml
If the definition data is invalid xml, export it wrapped in a malformed
tag. If it is valid, export without the wrapper.
NOTE: There may still be problems with the valid xml--it could be
missing required attributes, could have the wrong tags, refer to missing
files, etc.
'''
try:
xml = etree.fromstring(self.definition['data'])
return etree.tostring(xml)
except etree.XMLSyntaxError:
# still not valid.
root = etree.Element('malformed')
root.text = self.definition['data']
return etree.tostring(root)
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