diff options
author | Prabhu Ramachandran | 2011-11-09 03:05:03 +0530 |
---|---|---|
committer | Prabhu Ramachandran | 2011-11-09 03:05:03 +0530 |
commit | 6cf7eb9f1a69f596ea0dcc418c1b90d3fb8ec513 (patch) | |
tree | de2f47ad2dd6d7f7f1d26ecb33e3f210a96371cc /exam/views.py | |
parent | 4694d217dd7d5659afdad7ba5937adb85c15371c (diff) | |
download | online_test-6cf7eb9f1a69f596ea0dcc418c1b90d3fb8ec513.tar.gz online_test-6cf7eb9f1a69f596ea0dcc418c1b90d3fb8ec513.tar.bz2 online_test-6cf7eb9f1a69f596ea0dcc418c1b90d3fb8ec513.zip |
ENH: Saving answers and added quit page/button.
Cleaned up the models so the answers submitted are all saved. Also
added a quit button to each question page so a user can easily quit the
exam in order that another may start.
Diffstat (limited to 'exam/views.py')
-rw-r--r-- | exam/views.py | 30 |
1 files changed, 20 insertions, 10 deletions
diff --git a/exam/views.py b/exam/views.py index 69d7838..0dc02cc 100644 --- a/exam/views.py +++ b/exam/views.py @@ -8,19 +8,13 @@ from django.contrib.auth.models import User from django.contrib.auth import login, logout, authenticate from django.shortcuts import render_to_response, get_object_or_404, redirect from django.template import RequestContext -from exam.models import Question, Quiz, Profile +from exam.models import Question, Quiz, Profile, Answer from exam.forms import UserRegisterForm def gen_key(no_of_chars): + """Generate a random key of the number of characters.""" allowed_chars = string.digits+string.uppercase return ''.join([random.choice(allowed_chars) for i in range(no_of_chars)]) - -def index_old(request): - """The start page. - """ - question_list = Question.objects.all() - context = {'question_list': question_list} - return render_to_response('exam/index.html', context) def index(request): """The start page. @@ -64,6 +58,7 @@ def index(request): context_instance=RequestContext(request)) def show_question(request, q_id): + """Show a question if possible.""" if len(q_id) == 0: return redirect("/exam/complete") else: @@ -137,6 +132,11 @@ def check(request, q_id): retry = False 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 ci = RequestContext(request) @@ -148,8 +148,18 @@ def check(request, q_id): else: next_q = quiz.answered_question(question.id) return show_question(request, next_q) + +def quit(request): + return render_to_response('exam/quit.html', + context_instance=RequestContext(request)) def complete(request): - logout(request) - return render_to_response('exam/complete.html') + yes = True + if request.method == 'POST': + yes = request.POST.get('yes', None) + if yes: + logout(request) + return render_to_response('exam/complete.html') + else: + return redirect('/exam/') |