Commit c8bc46b6 by Victor Shnayder

Convert FileSubmission input to be a class.

- add test
parent c2682273
......@@ -448,36 +448,44 @@ class TextLine(InputTypeBase):
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
queue_len = 0
# Flag indicating that the problem has been queued, 'msg' is length of queue
if status == 'incomplete':
status = 'queued'
queue_len = msg
msg = 'Submitted to grader.'
class FileSubmission(InputTypeBase):
"""
Upload some files (e.g. for programming assignments)
"""
context = { 'id': eid,
'state': status,
'msg': msg,
'value': value,
'queue_len': queue_len,
'allowed_files': allowed_files,
'required_files': required_files,}
html = render_template("filesubmission.html", context)
return etree.XML(html)
template = "filesubmission.html"
tags = ['filesubmission']
def __init__(self, system, xml, state):
super(FileSubmission, self).__init__(system, xml, state)
escapedict = {'"': '"'}
self.allowed_files = json.dumps(xml.get('allowed_files', '').split())
self.allowed_files = saxutils.escape(self.allowed_files, escapedict)
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):
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):
'''
Make sure option inputs work
......@@ -150,7 +153,7 @@ class JavascriptInputTest(unittest.TestCase):
xml_str = """<javascriptinput id="prob_1_2" params="{params}" problem_state="{ps}"
display_class="{dc}" display_file="{df}"/>""".format(
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)
element = etree.fromstring(xml_str)
......@@ -226,3 +229,43 @@ class TextLineTest(unittest.TestCase):
'preprocessor': {'class_name': preprocessorClass,
'script_src': script}}
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