quiz/views.py
author nishanth
Tue, 20 Apr 2010 21:06:54 +0530
changeset 16 ad51f38d0339
parent 15 99af908a4174
child 17 68c2932660b7
permissions -rw-r--r--
implemented the start quiz functionality
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
     1
import random
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
     2
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     3
from django.db import IntegrityError
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
     4
from django.http import Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
     5
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     6
from django.contrib.auth.models import User
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     7
from django.contrib.auth import login, logout, authenticate
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     8
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     9
from django.shortcuts import redirect, render_to_response
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    10
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    11
from offline.event.models import Event
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    12
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    13
from offline.quiz.utils import gen_key
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    14
from offline.quiz.models import Profile, QuestionBank, Quiz
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    15
from offline.quiz.forms import UserRegisterForm
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    16
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    17
def start_page(request):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    18
    """ first see if user is authenticated.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    19
    If he is, redirect to the page where quiz happens.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    20
    Else register the user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    21
    """
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    22
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    23
    try:
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    24
        event = Event.objects.all()[0]
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    25
    except IndexError:
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    26
        raise Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    27
    
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    28
    if event.quiz_status == '00':
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    29
        raise Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    30
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    31
    user = request.user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    32
    if user.is_authenticated():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    33
        return redirect("/quiz/start/%s"%user.username)
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    34
    else:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    35
        try:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    36
            ip = request.META['REMOTE_ADDR']
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    37
            Quiz.objects.get(user_ip=ip, quiz_num=event.quiz_status)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    38
            return redirect("/quiz/complete")
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    39
        except Quiz.DoesNotExist:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    40
            pass
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    41
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    42
    if request.method == "POST":
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    43
        form = UserRegisterForm(request.POST)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    44
        if form.is_valid():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    45
            data = form.cleaned_data
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    46
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    47
            while True:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    48
                try:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    49
                    username = gen_key(20)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    50
                    new_user = User.objects.create_user(username, "temp@temp.com", "123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    51
                    break
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    52
                except IntegrityError:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    53
                    pass
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    54
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    55
            new_user.first_name = data['first_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    56
            new_user.last_name = data['last_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    57
            new_user.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    58
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    59
            new_profile = Profile(user=new_user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    60
            new_profile.profession = data['profession']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    61
            new_profile.affiliated_to = data['affiliated_to']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    62
            new_profile.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    63
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    64
            user = authenticate(username=username, password="123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    65
            login(request, user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    66
            return redirect("/quiz/start/%s"%username)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    67
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    68
        else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    69
            return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    70
    else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    71
        form = UserRegisterForm()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    72
        return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    73
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    74
def start_quiz(request, username):
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    75
    """ get the user by his username.
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    76
    then check for the event quiz status
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    77
    then check if his ip has finished the quiz
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    78
    then make his quiz paper and redirect to the right question.
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    79
    """
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    80
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    81
    user = request.user
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    82
    if not user.username == username:
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    83
        raise Http404
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    84
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    85
    try:
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    86
        event = Event.objects.all()[0]
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    87
    except IndexError:
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    88
        raise Http404
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    89
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    90
    if event.quiz_status == "00":
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    91
        raise Http404
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    92
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    93
    try:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    94
        old_quiz = Quiz.objects.get(event=event,user=user, quiz_num=event.quiz_status)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    95
        return redirect("/quiz/answer/%s/%s"%(username,old_quiz.key))
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    96
    except Quiz.DoesNotExist:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    97
        ip = request.META['REMOTE_ADDR']
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    98
        key = gen_key(10)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    99
        new_quiz = Quiz(event=event, user=user, quiz_num=event.quiz_status, user_ip=ip, key=key)
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
   100
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   101
        available_que_ids = [ str(_.id) for _ in QuestionBank.objects.filter(quiz_num=event.quiz_status) ]
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   102
        random.shuffle(available_que_ids)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   103
        new_quiz.que_remaining = "|".join(available_que_ids)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   104
        new_quiz.save()
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
   105
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   106
        return redirect("/quiz/answer/%s/%s"%(username,new_quiz.key))
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   107