Commit 202e467b by Will Daly

Allow export of OpenAssessmentBlock to XML

parent dcecf0ed
......@@ -19,7 +19,7 @@ from openassessment.xblock.peer_assessment_mixin import PeerAssessmentMixin
from openassessment.xblock.self_assessment_mixin import SelfAssessmentMixin
from openassessment.xblock.submission_mixin import SubmissionMixin
from openassessment.xblock.studio_mixin import StudioMixin
from openassessment.xblock.xml import update_from_xml
from openassessment.xblock.xml import update_from_xml, serialize_content_to_xml
from openassessment.xblock.workflow_mixin import WorkflowMixin
from openassessment.workflow import api as workflow_api
from openassessment.xblock.validation import validator
......@@ -380,6 +380,12 @@ class OpenAssessmentBlock(
context = Context(context_dict)
return Response(template.render(context), content_type='application/html', charset='UTF-8')
def add_xml_to_node(self, node):
"""
Serialize the XBlock to XML for exporting.
"""
serialize_content_to_xml(self, node)
def render_error(self, error_msg):
"""
Render an error message.
......
"""
Test that we can export a block from the runtime (to XML) and re-import it without error.
"""
import copy
from StringIO import StringIO
from .base import XBlockHandlerTestCase, scenario
class TestExportImport(XBlockHandlerTestCase):
@scenario('data/basic_scenario.xml')
def test_export_import(self, xblock):
# Store the fields of the XBlock
old_fields = copy.deepcopy(xblock.fields)
# Export the XBlock from the runtime
output_buffer = StringIO()
self.runtime.export_to_xml(xblock, output_buffer)
# Re-import the XBlock
block_id = self.runtime.parse_xml_string(output_buffer.getvalue(), self.runtime.id_generator)
new_block = self.runtime.get_block(block_id)
# Check that the values of all fields are the same
self.assertItemsEqual(new_block.fields, old_fields)
......@@ -365,17 +365,19 @@ def _parse_assessments_xml(assessments_root, start, due):
return assessments_list
def serialize_content(oa_block):
def serialize_content_to_xml(oa_block, root):
"""
Serialize the OpenAssessment XBlock's content to XML.
Args:
oa_block (OpenAssessmentBlock): The open assessment block to serialize.
root (etree.Element): The XML root node to update.
Returns:
xml (unicode)
etree.Element
"""
root = etree.Element('openassessment')
root.tag = 'openassessment'
# Set submission due date
if oa_block.submission_due is not None:
......@@ -410,6 +412,20 @@ def serialize_content(oa_block):
rubric_root = etree.SubElement(root, 'rubric')
_serialize_rubric(rubric_root, oa_block)
def serialize_content(oa_block):
"""
Serialize the OpenAssessment XBlock's content to an XML string.
Args:
oa_block (OpenAssessmentBlock): The open assessment block to serialize.
Returns:
xml (unicode)
"""
root = etree.Element('openassessment')
serialize_content_to_xml(oa_block, root)
# Return a UTF-8 representation of the XML
return etree.tostring(root, pretty_print=True, encoding='utf-8')
......
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