Commit 80d0952d by Victor Shnayder

Refactor schematic input

parent 935e3701
...@@ -546,29 +546,37 @@ register_input_class(CodeInput) ...@@ -546,29 +546,37 @@ register_input_class(CodeInput)
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
def schematic(element, value, status, render_template, msg=''): class Schematic(InputTypeBase):
eid = element.get('id') """
height = element.get('height') """
width = element.get('width')
parts = element.get('parts') template = "schematicinput.html"
analyses = element.get('analyses') tags = ['schematic']
initial_value = element.get('initial_value')
submit_analyses = element.get('submit_analyses') def __init__(self, system, xml, state):
context = { super(Schematic, self).__init__(system, xml, state)
'id': eid, self.height = xml.get('height')
'value': value, self.width = xml.get('width')
'initial_value': initial_value, self.parts = xml.get('parts')
'state': status, self.analyses = xml.get('analyses')
'width': width, self.initial_value = xml.get('initial_value')
'height': height, self.submit_analyses = xml.get('submit_analyses')
'parts': parts,
'analyses': analyses,
'submit_analyses': submit_analyses, def _get_render_context(self):
}
html = render_template("schematicinput.html", context) context = {'id': self.id,
return etree.XML(html) 'value': self.value,
'initial_value': self.initial_value,
'state': self.status,
'width': self.width,
'height': self.height,
'parts': self.parts,
'analyses': self.analyses,
'submit_analyses': self.submit_analyses, }
return context
_reg(schematic) register_input_class(Schematic)
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
### TODO: Move out of inputtypes ### TODO: Move out of inputtypes
......
""" """
Tests of input types (and actually responsetypes too) Tests of input types (and actually responsetypes too).
TODO:
- test unicode in values, parameters, etc.
- test various html escapes
""" """
from datetime import datetime from datetime import datetime
...@@ -318,3 +322,52 @@ class CodeInputTest(unittest.TestCase): ...@@ -318,3 +322,52 @@ class CodeInputTest(unittest.TestCase):
self.assertEqual(context, expected) self.assertEqual(context, expected)
class SchematicTest(unittest.TestCase):
'''
Check that schematic inputs work
'''
def test_rendering(self):
height = '12'
width = '33'
parts = 'resistors, capacitors, and flowers'
analyses = 'fast, slow, and pink'
initial_value = 'two large batteries'
submit_analyses = 'maybe'
xml_str = """<schematic id="prob_1_2"
height="{h}"
width="{w}"
parts="{p}"
analyses="{a}"
initial_value="{iv}"
submit_analyses="{sa}"
/>""".format(h=height, w=width, p=parts, a=analyses,
iv=initial_value, sa=submit_analyses)
element = etree.fromstring(xml_str)
value = 'three resistors and an oscilating pendulum'
state = {'value': value,
'status': 'unsubmitted',
'feedback' : {'message': '3'}, }
the_input = inputtypes.get_class_for_tag('schematic')(system, element, state)
context = the_input._get_render_context()
expected = {'id': 'prob_1_2',
'value': value,
'initial_value': initial_value,
'state': 'unsubmitted',
'width': width,
'height': height,
'parts': parts,
'analyses': analyses,
'submit_analyses': submit_analyses,
}
self.assertEqual(context, expected)
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