check.py 2.08 KB
Newer Older
1 2 3 4 5 6 7
#!/usr/bin/python

from random import choice
import string
import traceback

from django.conf import settings
8
import capa.capa_problem as lcp
9 10
from dogfood.views import update_problem

11

12 13 14 15 16
def GenID(length=8, chars=string.letters + string.digits):
    return ''.join([choice(chars) for i in range(length)])

randomid = GenID()

17 18

def check_problem_code(ans, the_lcp, correct_answers, false_answers):
19 20 21
    """
    ans = student's answer
    the_lcp = LoncapaProblem instance
22

23 24 25
    returns dict {'ok':is_ok,'msg': message with iframe}
    """
    pfn = "dog%s" % randomid
26 27
    pfn += the_lcp.problem_id.replace('filename', '')    # add problem ID to dogfood problem name
    update_problem(pfn, ans, filestore=the_lcp.system.filestore)
28
    msg = '<hr width="100%"/>'
29
    msg += '<iframe src="%s/dogfood/filename%s" width="95%%" height="400" frameborder="1">No iframe support!</iframe>' % (settings.MITX_ROOT_URL, pfn)
30 31
    msg += '<hr width="100%"/>'

32
    endmsg = """<p><font size="-1" color="purple">Note: if the code text box disappears after clicking on "Check",
33
        please type something in the box to make it refresh properly.  This is a
34 35 36
                bug with Chrome; it does not happen with Firefox.  It is being fixed.
                </font></p>"""

37
    is_ok = True
38
    if (not correct_answers) or (not false_answers):
39 40
        ret = {'ok': is_ok,
               'msg': msg + endmsg,
41 42 43
               }
        return ret

44 45 46
    try:
        # check correctness
        fp = the_lcp.system.filestore.open('problems/%s.xml' % pfn)
ichuang committed
47
        test_lcp = lcp.LoncapaProblem(fp, '1', system=the_lcp.system)
48 49

        if not (test_lcp.grade_answers(correct_answers).get_correctness('1_2_1') == 'correct'):
50
            is_ok = False
51
        if (test_lcp.grade_answers(false_answers).get_correctness('1_2_1') == 'correct'):
52
            is_ok = False
53
    except Exception, err:
54
        is_ok = False
55 56 57 58 59
        msg += "<p>Error: %s</p>" % str(err).replace('<', '&#60;')
        msg += "<p><pre>%s</pre></p>" % traceback.format_exc().replace('<', '&#60;')

    ret = {'ok': is_ok,
           'msg': msg + endmsg,
60 61
           }
    return ret
62 63