Commit c8bc46b6 by Victor Shnayder

Convert FileSubmission input to be a class.

- add test
parent c2682273
...@@ -448,36 +448,44 @@ class TextLine(InputTypeBase): ...@@ -448,36 +448,44 @@ class TextLine(InputTypeBase):
register_input_class(TextLine) register_input_class(TextLine)
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
def filesubmission(element, value, status, render_template, msg=''):
'''
Upload a single file (e.g. for programming assignments)
'''
eid = element.get('id')
escapedict = {'"': '"'}
allowed_files = json.dumps(element.get('allowed_files', '').split())
allowed_files = saxutils.escape(allowed_files, escapedict)
required_files = json.dumps(element.get('required_files', '').split())
required_files = saxutils.escape(required_files, escapedict)
# Check if problem has been queued class FileSubmission(InputTypeBase):
queue_len = 0 """
# Flag indicating that the problem has been queued, 'msg' is length of queue Upload some files (e.g. for programming assignments)
if status == 'incomplete': """
status = 'queued'
queue_len = msg
msg = 'Submitted to grader.'
context = { 'id': eid, template = "filesubmission.html"
'state': status, tags = ['filesubmission']
'msg': msg,
'value': value, def __init__(self, system, xml, state):
'queue_len': queue_len, super(FileSubmission, self).__init__(system, xml, state)
'allowed_files': allowed_files, escapedict = {'"': '"'}
'required_files': required_files,} self.allowed_files = json.dumps(xml.get('allowed_files', '').split())
html = render_template("filesubmission.html", context) self.allowed_files = saxutils.escape(self.allowed_files, escapedict)
return etree.XML(html) self.required_files = json.dumps(xml.get('required_files', '').split())
self.required_files = saxutils.escape(self.required_files, escapedict)
# Check if problem has been queued
queue_len = 0
# Flag indicating that the problem has been queued, 'msg' is length of queue
if self.status == 'incomplete':
self.status = 'queued'
self.queue_len = self.msg
self.msg = 'Submitted to grader.'
def _get_render_context(self):
context = {'id': self.id,
'state': self.status,
'msg': self.msg,
'value': self.value,
'queue_len': self.queue_len,
'allowed_files': self.allowed_files,
'required_files': self.required_files,}
return context
_reg(filesubmission) register_input_class(FileSubmission)
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
......
...@@ -24,6 +24,9 @@ def tst_render_template(template, context): ...@@ -24,6 +24,9 @@ def tst_render_template(template, context):
system = Mock(render_template=tst_render_template) system = Mock(render_template=tst_render_template)
def quote_attr(s):
return saxutils.quoteattr(s)[1:-1] # don't want the outer quotes
class OptionInputTest(unittest.TestCase): class OptionInputTest(unittest.TestCase):
''' '''
Make sure option inputs work Make sure option inputs work
...@@ -150,7 +153,7 @@ class JavascriptInputTest(unittest.TestCase): ...@@ -150,7 +153,7 @@ class JavascriptInputTest(unittest.TestCase):
xml_str = """<javascriptinput id="prob_1_2" params="{params}" problem_state="{ps}" xml_str = """<javascriptinput id="prob_1_2" params="{params}" problem_state="{ps}"
display_class="{dc}" display_file="{df}"/>""".format( display_class="{dc}" display_file="{df}"/>""".format(
params=params, params=params,
ps=saxutils.quoteattr(problem_state)[1:-1], # don't want the outer quotes ps=quote_attr(problem_state),
dc=display_class, df=display_file) dc=display_class, df=display_file)
element = etree.fromstring(xml_str) element = etree.fromstring(xml_str)
...@@ -226,3 +229,43 @@ class TextLineTest(unittest.TestCase): ...@@ -226,3 +229,43 @@ class TextLineTest(unittest.TestCase):
'preprocessor': {'class_name': preprocessorClass, 'preprocessor': {'class_name': preprocessorClass,
'script_src': script}} 'script_src': script}}
self.assertEqual(context, expected) self.assertEqual(context, expected)
class FileSubmissionTest(unittest.TestCase):
'''
Check that file submission inputs work
'''
def test_rendering(self):
allowed_files = "runme.py nooooo.rb ohai.java"
required_files = "cookies.py"
xml_str = """<filesubmission id="prob_1_2"
allowed_files="{af}"
required_files="{rf}"
/>""".format(af=allowed_files,
rf=required_files,)
element = etree.fromstring(xml_str)
escapedict = {'"': '&quot;'}
esc = lambda s: saxutils.escape(s, escapedict)
state = {'value': 'BumbleBee.py',
'status': 'incomplete',
'feedback' : {'message': '3'}, }
the_input = inputtypes.get_class_for_tag('filesubmission')(system, element, state)
context = the_input._get_render_context()
expected = {'id': 'prob_1_2',
'state': 'queued',
'msg': 'Submitted to grader.',
'value': 'BumbleBee.py',
'queue_len': '3',
'allowed_files': esc('["runme.py", "nooooo.rb", "ohai.java"]'),
'required_files': esc('["cookies.py"]')}
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