Commit d92cce76 by Stephen Sanchez

Merge pull request #429 from edx/sanchez/include_prompt_flag

Sanchez/include prompt flag
parents 30f052e0 c0aadeb7
...@@ -124,16 +124,15 @@ class TestSerializeContent(TestCase): ...@@ -124,16 +124,15 @@ class TestSerializeContent(TestCase):
parsed_expected = etree.fromstring("".join(data['expected_xml'])) parsed_expected = etree.fromstring("".join(data['expected_xml']))
# Pretty-print and reparse the expected XML # Pretty-print and reparse the expected XML
pretty_expected = etree.tostring(parsed_expected, pretty_print=True, encoding='utf-8') pretty_expected = etree.tostring(parsed_expected, pretty_print=True, encoding='unicode')
parsed_expected = etree.fromstring(pretty_expected) parsed_expected = etree.fromstring(pretty_expected)
# Walk both trees, comparing elements and attributes # Walk both trees, comparing elements and attributes
actual_elements = [el for el in parsed_actual.getiterator()] actual_elements = [el for el in parsed_actual.getiterator()]
expected_elements = [el for el in parsed_expected.getiterator()] expected_elements = [el for el in parsed_expected.getiterator()]
self.assertEqual( self.assertEqual(
len(actual_elements), len(expected_elements), len(actual_elements), len(expected_elements),
msg="Incorrect XML output:\nActual: {}\nExpected: {}".format(xml, pretty_expected) msg=u"Incorrect XML output:\nActual: {}\nExpected: {}".format(xml, pretty_expected)
) )
for actual, expected in zip(actual_elements, expected_elements): for actual, expected in zip(actual_elements, expected_elements):
...@@ -155,18 +154,23 @@ class TestSerializeContent(TestCase): ...@@ -155,18 +154,23 @@ class TestSerializeContent(TestCase):
def test_serialize_rubric(self, data): def test_serialize_rubric(self, data):
self._configure_xblock(data) self._configure_xblock(data)
xml_str = serialize_rubric_to_xml_str(self.oa_block) xml_str = serialize_rubric_to_xml_str(self.oa_block)
self.assertIn("<rubric>", xml_str)
if data['prompt']:
self.assertNotIn(data['prompt'], xml_str)
@ddt.file_data('data/serialize.json') @ddt.file_data('data/serialize.json')
def test_serialize_examples(self, data): def test_serialize_examples(self, data):
self._configure_xblock(data) self._configure_xblock(data)
for assessment in data['assessments']: for assessment in data['assessments']:
if 'student-training' == assessment['name']: if 'student-training' == assessment['name'] and assessment['examples']:
xml_str = serialize_examples_to_xml_str(assessment) xml_str = serialize_examples_to_xml_str(assessment)
self.assertIn(assessment['examples'][0]['answer'], xml_str)
@ddt.file_data('data/serialize.json') @ddt.file_data('data/serialize.json')
def test_serialize_assessments(self, data): def test_serialize_assessments(self, data):
self._configure_xblock(data) self._configure_xblock(data)
xml_str = serialize_assessments_to_xml_str(self.oa_block) xml_str = serialize_assessments_to_xml_str(self.oa_block)
self.assertIn(data['assessments'][0]['name'], xml_str)
def test_mutated_criteria_dict(self): def test_mutated_criteria_dict(self):
self.oa_block.title = "Test title" self.oa_block.title = "Test title"
......
...@@ -124,7 +124,7 @@ def _serialize_criteria(criteria_root, criteria_list): ...@@ -124,7 +124,7 @@ def _serialize_criteria(criteria_root, criteria_list):
_serialize_options(criterion_el, options_list) _serialize_options(criterion_el, options_list)
def serialize_rubric(rubric_root, oa_block): def serialize_rubric(rubric_root, oa_block, include_prompt=True):
""" """
Serialize a rubric dictionary as XML, adding children to the XML Serialize a rubric dictionary as XML, adding children to the XML
with root node `rubric_root`. with root node `rubric_root`.
...@@ -138,11 +138,14 @@ def serialize_rubric(rubric_root, oa_block): ...@@ -138,11 +138,14 @@ def serialize_rubric(rubric_root, oa_block):
rubric_dict (dict): A dictionary representation of the rubric, of the form rubric_dict (dict): A dictionary representation of the rubric, of the form
described in the serialized Rubric model (peer grading serializers). described in the serialized Rubric model (peer grading serializers).
Kwargs:
include_prompt (bool): Whether or not to include the prompt in the
serialized format for a rubric. Defaults to True.
Returns: Returns:
None None
""" """
# Rubric prompt (default to empty text); None indicates no input element # Rubric prompt (default to empty text); None indicates no input element
if oa_block.prompt is not None: if include_prompt and oa_block.prompt is not None:
prompt = etree.SubElement(rubric_root, 'prompt') prompt = etree.SubElement(rubric_root, 'prompt')
prompt.text = unicode(oa_block.prompt) prompt.text = unicode(oa_block.prompt)
...@@ -555,12 +558,15 @@ def serialize_content(oa_block): ...@@ -555,12 +558,15 @@ def serialize_content(oa_block):
serialize_content_to_xml(oa_block, root) serialize_content_to_xml(oa_block, root)
# Return a UTF-8 representation of the XML # Return a UTF-8 representation of the XML
return etree.tostring(root, pretty_print=True, encoding='utf-8') return etree.tostring(root, pretty_print=True, encoding='unicode')
def serialize_rubric_to_xml_str(oa_block): def serialize_rubric_to_xml_str(oa_block):
""" """
Serialize the OpenAssessment XBlock's rubric into an XML string. Serialize the OpenAssessment XBlock's rubric into an XML string. This is
designed to serialize the XBlock's rubric specifically for authoring. Since
the authoring view splits the prompt from the rubric, the serialized format
for the rubric does not contain the prompt.
Args: Args:
oa_block (OpenAssessmentBlock): The open assessment block to serialize oa_block (OpenAssessmentBlock): The open assessment block to serialize
...@@ -571,8 +577,8 @@ def serialize_rubric_to_xml_str(oa_block): ...@@ -571,8 +577,8 @@ def serialize_rubric_to_xml_str(oa_block):
""" """
rubric_root = etree.Element('rubric') rubric_root = etree.Element('rubric')
serialize_rubric(rubric_root, oa_block) serialize_rubric(rubric_root, oa_block, include_prompt=False)
return etree.tostring(rubric_root, pretty_print=True, encoding='utf-8') return etree.tostring(rubric_root, pretty_print=True, encoding='unicode')
def serialize_examples_to_xml_str(assessment): def serialize_examples_to_xml_str(assessment):
...@@ -594,7 +600,7 @@ def serialize_examples_to_xml_str(assessment): ...@@ -594,7 +600,7 @@ def serialize_examples_to_xml_str(assessment):
examples = [] examples = []
examples_root = etree.Element('examples') examples_root = etree.Element('examples')
serialize_training_examples(examples, examples_root) serialize_training_examples(examples, examples_root)
return etree.tostring(examples_root, pretty_print=True, encoding='utf-8') return etree.tostring(examples_root, pretty_print=True, encoding='unicode')
def serialize_assessments_to_xml_str(oa_block): def serialize_assessments_to_xml_str(oa_block):
...@@ -607,7 +613,7 @@ def serialize_assessments_to_xml_str(oa_block): ...@@ -607,7 +613,7 @@ def serialize_assessments_to_xml_str(oa_block):
""" """
assessments_root = etree.Element('assessments') assessments_root = etree.Element('assessments')
serialize_assessments(assessments_root, oa_block) serialize_assessments(assessments_root, oa_block)
return etree.tostring(assessments_root, pretty_print=True, encoding='utf-8') return etree.tostring(assessments_root, pretty_print=True, encoding='unicode')
def parse_from_xml(root): def parse_from_xml(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