Commit 80d0952d by Victor Shnayder

Refactor schematic input

parent 935e3701
......@@ -546,29 +546,37 @@ register_input_class(CodeInput)
#-----------------------------------------------------------------------------
def schematic(element, value, status, render_template, msg=''):
eid = element.get('id')
height = element.get('height')
width = element.get('width')
parts = element.get('parts')
analyses = element.get('analyses')
initial_value = element.get('initial_value')
submit_analyses = element.get('submit_analyses')
context = {
'id': eid,
'value': value,
'initial_value': initial_value,
'state': status,
'width': width,
'height': height,
'parts': parts,
'analyses': analyses,
'submit_analyses': submit_analyses,
}
html = render_template("schematicinput.html", context)
return etree.XML(html)
class Schematic(InputTypeBase):
"""
"""
template = "schematicinput.html"
tags = ['schematic']
def __init__(self, system, xml, state):
super(Schematic, self).__init__(system, xml, state)
self.height = xml.get('height')
self.width = xml.get('width')
self.parts = xml.get('parts')
self.analyses = xml.get('analyses')
self.initial_value = xml.get('initial_value')
self.submit_analyses = xml.get('submit_analyses')
def _get_render_context(self):
context = {'id': self.id,
'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
......
"""
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
......@@ -318,3 +322,52 @@ class CodeInputTest(unittest.TestCase):
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