diff options
author | ankitjavalkar | 2015-02-16 16:53:18 +0530 |
---|---|---|
committer | ankitjavalkar | 2015-04-26 19:37:06 +0530 |
commit | 591c261ebbbaa0a052ed7b82428a3627ddaa7b1a (patch) | |
tree | dca857718de55e1f829a1545fc7cacb472025873 /testapp/exam/views.py | |
parent | ecb202633d3b90c55128030adb7817387bf28c95 (diff) | |
download | online_test-591c261ebbbaa0a052ed7b82428a3627ddaa7b1a.tar.gz online_test-591c261ebbbaa0a052ed7b82428a3627ddaa7b1a.tar.bz2 online_test-591c261ebbbaa0a052ed7b82428a3627ddaa7b1a.zip |
Modify method of returning answers from code server
Diffstat (limited to 'testapp/exam/views.py')
-rw-r--r-- | testapp/exam/views.py | 70 |
1 files changed, 9 insertions, 61 deletions
diff --git a/testapp/exam/views.py b/testapp/exam/views.py index ac2668c..be0d292 100644 --- a/testapp/exam/views.py +++ b/testapp/exam/views.py @@ -853,44 +853,6 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): test = TestCase.objects.filter(question=question) test_parameter = question.consolidate_test_cases(test) - # #### - # for i in TestCase.objects.all(): - # print "=====$$$$$=====KEY", i.kw_args - # print "=====$$$$$=====POS", i.pos_args - # print "=====$$$$$=====FUNC", i.func_name - - # test_case_parameters = {} - # kw_args_dict = {} - # pos_args_list = [] - - # for i in TestCase.objects.all(): - # test_case_parameters.setdefault(i.id, {}) - # test_case_parameters[i.id]['func_name'] = i.func_name - # test_case_parameters[i.id]['expected_answer'] = i.expected_answer - - # for args in i.kw_args.split(","): - # key, val = args.split("=") - # kw_args_dict[key.strip()] = val.strip() - - - # for args in i.pos_args.split(","): - # pos_args_list.append(args.strip()) - - # test_case_parameters[i.id]['kw_args'] = kw_args_dict - # test_case_parameters[i.id]['pos_args'] = pos_args_list - - # print "######-----####-----", test_case_parameters - - # test_obj = TestCase.objects.filter(question=question) - # test_parameter = [] - # for i in test_obj: - # d = {} - # d['test_func_name'] = i.test_func_name - # d['test_keyword_args'] = i.test_keyword_args - # d['test_pos_args'] = i.test_pos_args - # d['test_expected_answer'] = i.test_expected_answer - # test_parameter.append(d) - # #### snippet_code = request.POST.get('snippet') user_code = request.POST.get('answer') skip = request.POST.get('skip', None) @@ -903,19 +865,6 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): next_q = paper.skip() return show_question(request, next_q, attempt_num, questionpaper_id) - # ### of the form addnum(1, 2, one=2, two=2) - # test_code_eval = "" - # for param in test_parameter: - # # print "TESTCASE----OUTLOOP", literal_eval(test_case['test_keyword_args']) - - # tcode = "assert {0}({1}, {2})\n""" \ - # .format(param.get('test_func_name'), param.get('test_pos_args'), param.get('test_keyword_args')) - # # .format(", ".join(str(i) for i in test_case['test_pos_args']), - # # ", ".join(str(key+"="+arg) for key, arg in eval(test_case['test_keyword_args']).iteritems())) - # test_code_eval = "\n".join(tcode) - - # print test_code_eval - # Add the answer submitted, regardless of it being correct or not. if question.type == 'mcq': user_answer = request.POST.get('answer') @@ -944,7 +893,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): # questions, we obtain the results via XML-RPC with the code executed # safely in a separate process (the code_server.py) running as nobody. if not question.type == 'upload': - correct, success, err_msg = validate_answer(user, user_answer, question, test_parameter) + correct, result = validate_answer(user, user_answer, question, test_parameter) if correct: new_answer.correct = correct new_answer.marks = question.points @@ -955,7 +904,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): new_answer.save() time_left = paper.time_left() - if not success: # Should only happen for non-mcq questions. + if not result.get('success'): # Should only happen for non-mcq questions. if time_left == 0: reason = 'Your time is up!' return complete(request, reason, attempt_num, questionpaper_id) @@ -970,8 +919,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): if old_answer: old_answer[0].answer = user_code old_answer[0].save() - context = {'question': question, 'questions': questions, - 'error_message': err_msg, + context = {'question': question, 'error_message': result.get('error'), 'paper': paper, 'last_attempt': user_code, 'quiz_name': paper.question_paper.quiz.description, 'time_left': time_left, 'to_attempt': to_attempt, @@ -990,7 +938,7 @@ def check(request, q_id, attempt_num=None, questionpaper_id=None): questionpaper_id, success_msg) -def validate_answer(user, user_answer, question, test_parameter):#### +def validate_answer(user, user_answer, question, test_parameter): """ Checks whether the answer submitted by the user is right or wrong. If right then returns correct = True, success and @@ -999,9 +947,9 @@ def validate_answer(user, user_answer, question, test_parameter):#### only one attempt are allowed for them. For code questions success is True only if the answer is correct. """ - success = True + + result = {'success': True, 'error': 'Incorrect answer'} correct = False - message = 'Incorrect answer' if user_answer is not None: if question.type == 'mcq': @@ -1015,11 +963,11 @@ def validate_answer(user, user_answer, question, test_parameter):#### message = 'Correct answer' elif question.type == 'code': user_dir = get_user_dir(user) - success, message = code_server.run_code(user_answer, question.test, test_parameter, user_dir, question.language) #### - if success: + result = code_server.run_code(user_answer, test_parameter, user_dir, question.language) + if result.get('success'): correct = True - return correct, success, message + return correct, result def quit(request, attempt_num=None, questionpaper_id=None): |