quiz/views.py
author nishanth
Tue, 20 Apr 2010 22:05:32 +0530
changeset 21 ff6d34fc7137
parent 20 bcfa305cb32c
child 24 2368bb63ee39
permissions -rw-r--r--
added the quiz complete 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
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
     5
from django.utils.datastructures import MultiValueDictKeyError
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
     6
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     7
from django.contrib.auth.models import User
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     8
from django.contrib.auth import login, logout, authenticate
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
     9
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    10
from django.shortcuts import redirect, render_to_response
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    11
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    12
from offline.event.models import Event
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    13
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    14
from offline.quiz.utils import gen_key
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
    15
from offline.quiz.models import Profile, QuestionBank, Quiz, Answer
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    16
from offline.quiz.forms import UserRegisterForm
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    17
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    18
def start_page(request):
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    19
    """ first see if user is authenticated.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    20
    If he is, redirect to the page where quiz happens.
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    21
    Else register the user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    22
    """
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    23
14
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    24
    try:
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    25
        event = Event.objects.all()[0]
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    26
    except IndexError:
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    27
        raise Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    28
    
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    29
    if event.quiz_status == '00':
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    30
        raise Http404
ea7d372bfbff implemented more constraints on the register for test page
nishanth
parents: 13
diff changeset
    31
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    32
    user = request.user
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    33
    if user.is_authenticated():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    34
        return redirect("/quiz/start/%s"%user.username)
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    35
    else:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    36
        try:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    37
            ip = request.META['REMOTE_ADDR']
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    38
            Quiz.objects.get(user_ip=ip, quiz_num=event.quiz_status)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    39
            return redirect("/quiz/complete")
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    40
        except Quiz.DoesNotExist:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    41
            pass
12
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    42
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    43
    if request.method == "POST":
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    44
        form = UserRegisterForm(request.POST)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    45
        if form.is_valid():
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    46
            data = form.cleaned_data
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    47
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    48
            while True:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    49
                try:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    50
                    username = gen_key(20)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    51
                    new_user = User.objects.create_user(username, "temp@temp.com", "123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    52
                    break
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    53
                except IntegrityError:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    54
                    pass
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    55
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    56
            new_user.first_name = data['first_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    57
            new_user.last_name = data['last_name']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    58
            new_user.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    59
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    60
            new_profile = Profile(user=new_user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    61
            new_profile.profession = data['profession']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    62
            new_profile.affiliated_to = data['affiliated_to']
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    63
            new_profile.save()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    64
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    65
            user = authenticate(username=username, password="123")
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    66
            login(request, user)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    67
            return redirect("/quiz/start/%s"%username)
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    68
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    69
        else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    70
            return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    71
    else:
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    72
        form = UserRegisterForm()
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    73
        return render_to_response('register.html',{'form':form})
81cd0140a0f2 created the register user functionality.
nishanth
parents:
diff changeset
    74
18
c66b4904ce5a ditchaxed username from start_quiz url
nishanth
parents: 17
diff changeset
    75
def start_quiz(request):
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    76
    """ get the user by his username.
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    77
    then check for the event quiz status
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    78
    then check if his ip has finished the quiz
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    79
    then make his quiz paper and redirect to the right question.
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    80
    """
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    81
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    82
    user = request.user
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    83
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    84
    try:
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    85
        event = Event.objects.all()[0]
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    86
    except IndexError:
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    87
        raise Http404
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    88
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    89
    if event.quiz_status == "00":
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    90
        raise Http404
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
    91
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    92
    try:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    93
        old_quiz = Quiz.objects.get(event=event,user=user, quiz_num=event.quiz_status)
17
68c2932660b7 removed username from answer url
nishanth
parents: 16
diff changeset
    94
        return redirect("/quiz/answer/%s"%(old_quiz.key))
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    95
    except Quiz.DoesNotExist:
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    96
        ip = request.META['REMOTE_ADDR']
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    97
        key = gen_key(10)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
    98
        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
    99
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   100
        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
   101
        random.shuffle(available_que_ids)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   102
        new_quiz.que_remaining = "|".join(available_que_ids)
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   103
        new_quiz.save()
15
99af908a4174 added questions thro seed_db
nishanth
parents: 14
diff changeset
   104
17
68c2932660b7 removed username from answer url
nishanth
parents: 16
diff changeset
   105
        return redirect("/quiz/answer/%s"%(new_quiz.key))
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   106
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   107
def answer(request, quiz_key):
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   108
    """ see if username and quiz give a proper quiz object
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   109
    then see if there are any available questions
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   110
    then render the proper question.
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   111
    """
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   112
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   113
    user = request.user
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   114
    if not user.is_authenticated():
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   115
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   116
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   117
    try:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   118
        event = Event.objects.all()[0]
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   119
    except IndexError:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   120
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   121
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   122
    if event.quiz_status == "00":
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   123
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   124
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   125
    try:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   126
        quiz = Quiz.objects.get(event=event,user=user,key=quiz_key)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   127
    except Quiz.DoesNotExist:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   128
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   129
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   130
    remaining_que = quiz.que_remaining.split('|')
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   131
    if not remaining_que:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   132
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   133
    
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   134
    que_id = remaining_que[0]
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   135
    question = QuestionBank.objects.get(id=que_id)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   136
    if request.method == "POST":
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   137
        try:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   138
            answer = request.POST['answer']
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   139
        except MultiValueDictKeyError:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   140
            answer = ""
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   141
        new_answer = Answer(question=question)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   142
        new_answer.submitted_ans = answer
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   143
        new_answer.save()
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   144
20
bcfa305cb32c fixed a bug
nishanth
parents: 19
diff changeset
   145
        quiz.que_answered.add(new_answer)
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   146
        remaining_que.remove(que_id)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   147
        quiz.que_remaining = "|".join(remaining_que)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   148
        quiz.key = gen_key(10)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   149
        quiz.save()
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   150
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   151
        if not remaining_que:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   152
            return redirect('/quiz/complete')
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   153
        else:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   154
            return redirect('/quiz/answer/%s'%quiz.key)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   155
    else:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   156
        return render_to_response('display_question.html', {'question':question})
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   157
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   158
def quiz_complete(request):
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   159
    """ display thanq and log the user out.
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   160
    """
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   161
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   162
    user = request.user
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   163
    logout(request)
21
ff6d34fc7137 added the quiz complete functionality
nishanth
parents: 20
diff changeset
   164
    return render_to_response("quiz_complete.html")