the basic quiz workflow is ready
authornishanth
Tue, 20 Apr 2010 21:53:19 +0530
changeset 19 4911302379ac
parent 18 c66b4904ce5a
child 20 bcfa305cb32c
the basic quiz workflow is ready
quiz/views.py
templates/base.html
templates/display_question.html
--- a/quiz/views.py	Tue Apr 20 21:15:18 2010 +0530
+++ b/quiz/views.py	Tue Apr 20 21:53:19 2010 +0530
@@ -2,6 +2,7 @@
 
 from django.db import IntegrityError
 from django.http import Http404
+from django.utils.datastructures import MultiValueDictKeyError
 
 from django.contrib.auth.models import User
 from django.contrib.auth import login, logout, authenticate
@@ -11,7 +12,7 @@
 from offline.event.models import Event
 
 from offline.quiz.utils import gen_key
-from offline.quiz.models import Profile, QuestionBank, Quiz
+from offline.quiz.models import Profile, QuestionBank, Quiz, Answer
 from offline.quiz.forms import UserRegisterForm
 
 def start_page(request):
@@ -79,8 +80,6 @@
     """
 
     user = request.user
-    if not user.is_authenticated():
-        raise Http404
 
     try:
         event = Event.objects.all()[0]
@@ -105,3 +104,60 @@
 
         return redirect("/quiz/answer/%s"%(new_quiz.key))
 
+def answer(request, quiz_key):
+    """ see if username and quiz give a proper quiz object
+    then see if there are any available questions
+    then render the proper question.
+    """
+
+    user = request.user
+    if not user.is_authenticated():
+        raise Http404
+
+    try:
+        event = Event.objects.all()[0]
+    except IndexError:
+        raise Http404
+
+    if event.quiz_status == "00":
+        raise Http404
+
+    try:
+        quiz = Quiz.objects.get(event=event,user=user,key=quiz_key)
+    except Quiz.DoesNotExist:
+        raise Http404
+
+    remaining_que = quiz.que_remaining.split('|')
+    if not remaining_que:
+        raise Http404
+    
+    que_id = remaining_que[0]
+    question = QuestionBank.objects.get(id=que_id)
+    if request.method == "POST":
+        try:
+            answer = request.POST['answer']
+        except MultiValueDictKeyError:
+            answer = ""
+        new_answer = Answer(question=question)
+        new_answer.submitted_ans = answer
+        new_answer.save()
+
+        remaining_que.remove(que_id)
+        quiz.que_remaining = "|".join(remaining_que)
+        quiz.key = gen_key(10)
+        quiz.save()
+
+        if not remaining_que:
+            return redirect('/quiz/complete')
+        else:
+            return redirect('/quiz/answer/%s'%quiz.key)
+    else:
+        return render_to_response('display_question.html', {'question':question})
+
+def quiz_complete(request):
+    """ display thanq and log the user out.
+    """
+
+    user = request.user
+    logout(request)
+    render_to_response("quiz_complete.html")
--- a/templates/base.html	Tue Apr 20 21:15:18 2010 +0530
+++ b/templates/base.html	Tue Apr 20 21:53:19 2010 +0530
@@ -4,6 +4,8 @@
 {% block title %}
 {% endblock %}
 </title>
+{% block script %}
+{% endblock %}
 </head>
 <body>
 {% block content %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/templates/display_question.html	Tue Apr 20 21:53:19 2010 +0530
@@ -0,0 +1,15 @@
+{% extends 'base.html' %}
+{% block script %}
+    <script language="javascript">
+        function dipost (){
+            document.getElementById("queform").submit();
+        }
+		setTimeout("dipost()", {{question.time_limit}} * 1000)
+    </script>
+{% endblock %}
+{% block content %}
+<form action="" method="post" id="queform">
+		{{question.description}}	<br />
+<textarea name="answer"></textarea>
+</form>
+{% endblock %}