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 | |
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.
-rw-r--r-- | exam/models.py | 29 | ||||
-rw-r--r-- | exam/urls.py | 1 | ||||
-rw-r--r-- | exam/views.py | 30 | ||||
-rw-r--r-- | templates/exam/question.html | 6 | ||||
-rw-r--r-- | templates/exam/quit.html | 8 |
5 files changed, 54 insertions, 20 deletions
diff --git a/exam/models.py b/exam/models.py index 3a8121b..226146a 100644 --- a/exam/models.py +++ b/exam/models.py @@ -1,13 +1,13 @@ from django.db import models from django.contrib.auth.models import User -# Create your models here. class Profile(models.Model): - """Profile for a user to store roll number etc.""" + """Profile for a user to store roll number and other details.""" user = models.ForeignKey(User) roll_number = models.CharField(max_length=20) + class Question(models.Model): """A question in the database.""" # An optional one-line summary of the question. @@ -16,7 +16,7 @@ class Question(models.Model): description = models.TextField() # Number of points for the question. - points = models.IntegerField() + points = models.IntegerField(default=1) # Test cases for the question in the form of code that is run. # This is simple Python code. @@ -24,33 +24,42 @@ class Question(models.Model): def __unicode__(self): return self.summary - + class Answer(models.Model): """Answers submitted by users. """ + # The question for which we are an answer. question = models.ForeignKey(Question) + # The last answer submitted by the user. answer = models.TextField() - attempts = models.IntegerField() - - # Is the question correct. - correct = models.BooleanField() - # Marks obtained. - marks = models.IntegerField() + # Is the answer correct. + correct = models.BooleanField(default=False) + def __unicode__(self): return self.answer + class Quiz(models.Model): """A quiz for a student. """ + # The user taking this quiz. user = models.ForeignKey(User) + # User's IP which is logged. user_ip = models.CharField(max_length=15) + # Unused currently. key = models.CharField(max_length=10) + + # The questions (a list of ids separated by '|') questions = models.CharField(max_length=128) + # The questions successfully answered (a list of ids separated by '|') questions_answered = models.CharField(max_length=128) + # All the submitted answers. + answers = models.ManyToManyField(Answer) + def current_question(self): """Returns the current active question to display.""" qs = self.questions.split('|') diff --git a/exam/urls.py b/exam/urls.py index 7922255..cfb4764 100644 --- a/exam/urls.py +++ b/exam/urls.py @@ -3,6 +3,7 @@ from django.conf.urls.defaults import patterns, include, url urlpatterns = patterns('exam.views', url(r'^$', 'index'), url(r'^start/$', 'start'), + url(r'^quit/$', 'quit'), url(r'^complete/$', 'complete'), url(r'^(?P<q_id>\d+)/$', 'question'), url(r'^(?P<q_id>\d+)/check/$', 'check'), 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/') diff --git a/templates/exam/question.html b/templates/exam/question.html index 896a8aa..4cfda27 100644 --- a/templates/exam/question.html +++ b/templates/exam/question.html @@ -18,3 +18,9 @@ </form> <p> You have {{quiz.questions_left}} question(s) left. </p> + +<hr/> +<form action="/exam/quit/" method="post"> +{% csrf_token %} +<input type="submit" name="quit" value="Quit exam and logout" /> +</form> diff --git a/templates/exam/quit.html b/templates/exam/quit.html new file mode 100644 index 0000000..ea4b8df --- /dev/null +++ b/templates/exam/quit.html @@ -0,0 +1,8 @@ +<p>Your current answers are saved.</p> +<p> Are you sure you wish to quit the exam?</p> + +<form action="/exam/complete/" method="post"> +{% csrf_token %} +<input type="submit" name="yes" value="Yes!" /> +<input type="submit" name="no" value="No!" /> +</form> |