quiz/views.py
changeset 19 4911302379ac
parent 18 c66b4904ce5a
child 20 bcfa305cb32c
equal deleted inserted replaced
18:c66b4904ce5a 19:4911302379ac
     1 import random
     1 import random
     2 
     2 
     3 from django.db import IntegrityError
     3 from django.db import IntegrityError
     4 from django.http import Http404
     4 from django.http import Http404
       
     5 from django.utils.datastructures import MultiValueDictKeyError
     5 
     6 
     6 from django.contrib.auth.models import User
     7 from django.contrib.auth.models import User
     7 from django.contrib.auth import login, logout, authenticate
     8 from django.contrib.auth import login, logout, authenticate
     8 
     9 
     9 from django.shortcuts import redirect, render_to_response
    10 from django.shortcuts import redirect, render_to_response
    10 
    11 
    11 from offline.event.models import Event
    12 from offline.event.models import Event
    12 
    13 
    13 from offline.quiz.utils import gen_key
    14 from offline.quiz.utils import gen_key
    14 from offline.quiz.models import Profile, QuestionBank, Quiz
    15 from offline.quiz.models import Profile, QuestionBank, Quiz, Answer
    15 from offline.quiz.forms import UserRegisterForm
    16 from offline.quiz.forms import UserRegisterForm
    16 
    17 
    17 def start_page(request):
    18 def start_page(request):
    18     """ first see if user is authenticated.
    19     """ first see if user is authenticated.
    19     If he is, redirect to the page where quiz happens.
    20     If he is, redirect to the page where quiz happens.
    77     then check if his ip has finished the quiz
    78     then check if his ip has finished the quiz
    78     then make his quiz paper and redirect to the right question.
    79     then make his quiz paper and redirect to the right question.
    79     """
    80     """
    80 
    81 
    81     user = request.user
    82     user = request.user
    82     if not user.is_authenticated():
       
    83         raise Http404
       
    84 
    83 
    85     try:
    84     try:
    86         event = Event.objects.all()[0]
    85         event = Event.objects.all()[0]
    87     except IndexError:
    86     except IndexError:
    88         raise Http404
    87         raise Http404
   103         new_quiz.que_remaining = "|".join(available_que_ids)
   102         new_quiz.que_remaining = "|".join(available_que_ids)
   104         new_quiz.save()
   103         new_quiz.save()
   105 
   104 
   106         return redirect("/quiz/answer/%s"%(new_quiz.key))
   105         return redirect("/quiz/answer/%s"%(new_quiz.key))
   107 
   106 
       
   107 def answer(request, quiz_key):
       
   108     """ see if username and quiz give a proper quiz object
       
   109     then see if there are any available questions
       
   110     then render the proper question.
       
   111     """
       
   112 
       
   113     user = request.user
       
   114     if not user.is_authenticated():
       
   115         raise Http404
       
   116 
       
   117     try:
       
   118         event = Event.objects.all()[0]
       
   119     except IndexError:
       
   120         raise Http404
       
   121 
       
   122     if event.quiz_status == "00":
       
   123         raise Http404
       
   124 
       
   125     try:
       
   126         quiz = Quiz.objects.get(event=event,user=user,key=quiz_key)
       
   127     except Quiz.DoesNotExist:
       
   128         raise Http404
       
   129 
       
   130     remaining_que = quiz.que_remaining.split('|')
       
   131     if not remaining_que:
       
   132         raise Http404
       
   133     
       
   134     que_id = remaining_que[0]
       
   135     question = QuestionBank.objects.get(id=que_id)
       
   136     if request.method == "POST":
       
   137         try:
       
   138             answer = request.POST['answer']
       
   139         except MultiValueDictKeyError:
       
   140             answer = ""
       
   141         new_answer = Answer(question=question)
       
   142         new_answer.submitted_ans = answer
       
   143         new_answer.save()
       
   144 
       
   145         remaining_que.remove(que_id)
       
   146         quiz.que_remaining = "|".join(remaining_que)
       
   147         quiz.key = gen_key(10)
       
   148         quiz.save()
       
   149 
       
   150         if not remaining_que:
       
   151             return redirect('/quiz/complete')
       
   152         else:
       
   153             return redirect('/quiz/answer/%s'%quiz.key)
       
   154     else:
       
   155         return render_to_response('display_question.html', {'question':question})
       
   156 
       
   157 def quiz_complete(request):
       
   158     """ display thanq and log the user out.
       
   159     """
       
   160 
       
   161     user = request.user
       
   162     logout(request)
       
   163     render_to_response("quiz_complete.html")