summaryrefslogtreecommitdiff
path: root/exam
diff options
context:
space:
mode:
authorPrabhu Ramachandran2011-11-10 15:13:55 +0530
committerPrabhu Ramachandran2011-11-10 15:13:55 +0530
commitd5cd760142772eb91d3c9ce01b3ae8ea86d160da (patch)
tree0a0b778f3748aba0966ccb4823ad459f287b1dff /exam
parent812f79bd79c54b3b362ef616c121fb500b0b8a11 (diff)
downloadonline_test-d5cd760142772eb91d3c9ce01b3ae8ea86d160da.tar.gz
online_test-d5cd760142772eb91d3c9ce01b3ae8ea86d160da.tar.bz2
online_test-d5cd760142772eb91d3c9ce01b3ae8ea86d160da.zip
ENH: Cleaning up test code add rollno to monitor
Adding a rollnumber to monitor table and cleaning up the test code to be a separate function. This should be more reusable as we could technically run this in a separate process with an unpriviledged user.
Diffstat (limited to 'exam')
-rw-r--r--exam/views.py69
1 files changed, 41 insertions, 28 deletions
diff --git a/exam/views.py b/exam/views.py
index 2a79b24..a4c611d 100644
--- a/exam/views.py
+++ b/exam/views.py
@@ -96,52 +96,64 @@ def question(request, q_id):
return render_to_response('exam/question.html', context,
context_instance=ci)
-def test_answer(func_code, test_code):
- obj = compile(func_code, '<string>', mode='exec')
- g = {}
- exec obj in g
- t = compile(test_code, '<string>', mode='exec')
- exec t in g
+def test_python(answer, test_code):
+ """Tests given Python function with the test code supplied.
-def check(request, q_id):
- user = request.user
- question = get_object_or_404(Question, pk=q_id)
- quiz = Quiz.objects.get(user=user)
- answer = request.POST.get('answer')
- skip = request.POST.get('skip', None)
+ Returns
+ -------
- if skip is not None:
- next_q = quiz.skip()
- return show_question(request, next_q)
-
- # Otherwise we were asked to check.
- retry = True
+ A tuple: (success, error message).
+
+ """
+ success = False
tb = None
try:
- test_answer(answer, question.test)
+ submitted = compile(answer, '<string>', mode='exec')
+ g = {}
+ exec submitted in g
+ _tests = compile(test_code, '<string>', mode='exec')
+ exec _tests in g
except AssertionError:
type, value, tb = sys.exc_info()
info = traceback.extract_tb(tb)
fname, lineno, func, text = info[-1]
- text = str(question.test).splitlines()[lineno-1]
+ text = str(test_code).splitlines()[lineno-1]
err = "{0} {1} in: {2}".format(type.__name__, str(value), text)
except:
type, value = sys.exc_info()[:2]
err = "Error: {0}".format(repr(value))
else:
- retry = False
+ success = True
err = 'Correct answer'
finally:
- # Add the answer submitted.
- new_answer = Answer(question=question, answer=answer.strip())
- new_answer.correct = not retry
- new_answer.save()
- quiz.answers.add(new_answer)
del tb
+ return success, err
+
+
+def check(request, q_id):
+ user = request.user
+ question = get_object_or_404(Question, pk=q_id)
+ quiz = Quiz.objects.get(user=user)
+ answer = request.POST.get('answer')
+ skip = request.POST.get('skip', None)
+
+ if skip is not None:
+ next_q = quiz.skip()
+ return show_question(request, next_q)
+
+ # Otherwise we were asked to check.
+ success, err_msg = test_python(answer, question.test)
+
+ # Add the answer submitted.
+ new_answer = Answer(question=question, answer=answer.strip())
+ new_answer.correct = success
+ new_answer.save()
+ quiz.answers.add(new_answer)
+
ci = RequestContext(request)
- if retry:
- context = {'question': question, 'error_message': err,
+ if not success:
+ context = {'question': question, 'error_message': err_msg,
'last_attempt': answer}
return render_to_response('exam/question.html', context,
context_instance=ci)
@@ -174,6 +186,7 @@ def monitor(request):
paper = {}
user = quiz.user
paper['username'] = str(user.first_name) + ' ' + str(user.last_name)
+ paper['rollno'] = str(Profile.objects.get(user=user).roll_number)
qa = quiz.questions_answered.split('|')
answered = ', '.join(sorted(qa))
paper['answered'] = answered if answered else 'None'