quiz/views.py
changeset 16 ad51f38d0339
parent 15 99af908a4174
child 17 68c2932660b7
equal deleted inserted replaced
15:99af908a4174 16:ad51f38d0339
       
     1 import random
       
     2 
     1 from django.db import IntegrityError
     3 from django.db import IntegrityError
     2 
       
     3 from django.http import Http404
     4 from django.http import Http404
     4 
     5 
     5 from django.contrib.auth.models import User
     6 from django.contrib.auth.models import User
     6 from django.contrib.auth import login, logout, authenticate
     7 from django.contrib.auth import login, logout, authenticate
     7 
     8 
     8 from django.shortcuts import redirect, render_to_response
     9 from django.shortcuts import redirect, render_to_response
     9 
    10 
    10 from offline.event.models import Event
    11 from offline.event.models import Event
    11 
    12 
    12 from offline.quiz.utils import gen_key
    13 from offline.quiz.utils import gen_key
    13 from offline.quiz.models import Profile
    14 from offline.quiz.models import Profile, QuestionBank, Quiz
    14 
       
    15 from offline.quiz.forms import UserRegisterForm
    15 from offline.quiz.forms import UserRegisterForm
    16 
    16 
    17 def start_page(request):
    17 def start_page(request):
    18     """ first see if user is authenticated.
    18     """ first see if user is authenticated.
    19     If he is, redirect to the page where quiz happens.
    19     If he is, redirect to the page where quiz happens.
    29         raise Http404
    29         raise Http404
    30 
    30 
    31     user = request.user
    31     user = request.user
    32     if user.is_authenticated():
    32     if user.is_authenticated():
    33         return redirect("/quiz/start/%s"%user.username)
    33         return redirect("/quiz/start/%s"%user.username)
       
    34     else:
       
    35         try:
       
    36             ip = request.META['REMOTE_ADDR']
       
    37             Quiz.objects.get(user_ip=ip, quiz_num=event.quiz_status)
       
    38             return redirect("/quiz/complete")
       
    39         except Quiz.DoesNotExist:
       
    40             pass
    34 
    41 
    35     if request.method == "POST":
    42     if request.method == "POST":
    36         form = UserRegisterForm(request.POST)
    43         form = UserRegisterForm(request.POST)
    37         if form.is_valid():
    44         if form.is_valid():
    38             data = form.cleaned_data
    45             data = form.cleaned_data
    69     then check for the event quiz status
    76     then check for the event quiz status
    70     then check if his ip has finished the quiz
    77     then check if his ip has finished the quiz
    71     then make his quiz paper and redirect to the right question.
    78     then make his quiz paper and redirect to the right question.
    72     """
    79     """
    73 
    80 
    74     user = repuest.user
    81     user = request.user
    75     if not user.username == username:
    82     if not user.username == username:
    76         raise Http404
    83         raise Http404
    77 
    84 
    78     try:
    85     try:
    79         event = Event.objects.all()[0]
    86         event = Event.objects.all()[0]
    81         raise Http404
    88         raise Http404
    82 
    89 
    83     if event.quiz_status == "00":
    90     if event.quiz_status == "00":
    84         raise Http404
    91         raise Http404
    85 
    92 
       
    93     try:
       
    94         old_quiz = Quiz.objects.get(event=event,user=user, quiz_num=event.quiz_status)
       
    95         return redirect("/quiz/answer/%s/%s"%(username,old_quiz.key))
       
    96     except Quiz.DoesNotExist:
       
    97         ip = request.META['REMOTE_ADDR']
       
    98         key = gen_key(10)
       
    99         new_quiz = Quiz(event=event, user=user, quiz_num=event.quiz_status, user_ip=ip, key=key)
    86 
   100 
       
   101         available_que_ids = [ str(_.id) for _ in QuestionBank.objects.filter(quiz_num=event.quiz_status) ]
       
   102         random.shuffle(available_que_ids)
       
   103         new_quiz.que_remaining = "|".join(available_que_ids)
       
   104         new_quiz.save()
    87 
   105 
       
   106         return redirect("/quiz/answer/%s/%s"%(username,new_quiz.key))
       
   107