quiz/views.py
author nishanth
Wed, 21 Apr 2010 19:17:47 +0530
changeset 33 d28d0957a5ab
parent 29 ea1c0110e989
child 39 0fa055b8ea98
permissions -rw-r--r--
added intro page and manual submit button in question page
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():
24
2368bb63ee39 may be we forgot to remove username while we were removing previously. so removed it now
nishanth
parents: 21
diff changeset
    34
        return redirect("/quiz/start/")
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)
24
2368bb63ee39 may be we forgot to remove username while we were removing previously. so removed it now
nishanth
parents: 21
diff changeset
    67
            return redirect("/quiz/start/")
12
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
33
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   105
        return redirect("/quiz/intro/%s"%(new_quiz.key))
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   106
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   107
def quiz_intro(request, quiz_key):
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   108
    """ simply give intro to user
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   109
    if there are questions redirect.
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   110
    else raise Http404
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   111
    """
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   112
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   113
    user = request.user
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   114
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   115
    try:
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   116
        event = Event.objects.all()[0]
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   117
    except IndexError:
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   118
        raise Http404
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   119
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   120
    if event.quiz_status == "00":
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   121
        raise Http404
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   122
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   123
    try:
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   124
        old_quiz = Quiz.objects.get(event=event,user=user, quiz_num=event.quiz_status)
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   125
        return render_to_response("quiz_intro.html", {"user":user, "quiz":old_quiz}) 
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   126
    except Quiz.DoesNotExist:
d28d0957a5ab added intro page and manual submit button in question page
nishanth
parents: 29
diff changeset
   127
        raise Http404
16
ad51f38d0339 implemented the start quiz functionality
nishanth
parents: 15
diff changeset
   128
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   129
def answer(request, quiz_key):
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   130
    """ see if username and quiz give a proper quiz object
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   131
    then see if there are any available questions
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   132
    then render the proper question.
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
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   135
    user = request.user
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   136
    if not user.is_authenticated():
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   137
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   138
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   139
    try:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   140
        event = Event.objects.all()[0]
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   141
    except IndexError:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   142
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   143
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   144
    if event.quiz_status == "00":
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   145
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   146
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   147
    try:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   148
        quiz = Quiz.objects.get(event=event,user=user,key=quiz_key)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   149
    except Quiz.DoesNotExist:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   150
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   151
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   152
    remaining_que = quiz.que_remaining.split('|')
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   153
    if not remaining_que:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   154
        raise Http404
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   155
    
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   156
    que_id = remaining_que[0]
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   157
    question = QuestionBank.objects.get(id=que_id)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   158
    if request.method == "POST":
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   159
        try:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   160
            answer = request.POST['answer']
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   161
        except MultiValueDictKeyError:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   162
            answer = ""
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   163
        new_answer = Answer(question=question)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   164
        new_answer.submitted_ans = answer
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   165
        new_answer.save()
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   166
20
bcfa305cb32c fixed a bug
nishanth
parents: 19
diff changeset
   167
        quiz.que_answered.add(new_answer)
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   168
        remaining_que.remove(que_id)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   169
        quiz.que_remaining = "|".join(remaining_que)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   170
        quiz.key = gen_key(10)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   171
        quiz.save()
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   172
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   173
        if not remaining_que:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   174
            return redirect('/quiz/complete')
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   175
        else:
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   176
            return redirect('/quiz/answer/%s'%quiz.key)
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   177
    else:
29
ea1c0110e989 fixed the display of question
nishanth
parents: 24
diff changeset
   178
        options = [ _.strip() for _ in question.options.split('\n') ] if question.options else []
ea1c0110e989 fixed the display of question
nishanth
parents: 24
diff changeset
   179
        return render_to_response('display_question.html', {'question':question, 'options':options})
19
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   180
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   181
def quiz_complete(request):
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   182
    """ display thanq and log the user out.
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   183
    """
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   184
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   185
    user = request.user
4911302379ac the basic quiz workflow is ready
nishanth
parents: 18
diff changeset
   186
    logout(request)
21
ff6d34fc7137 added the quiz complete functionality
nishanth
parents: 20
diff changeset
   187
    return render_to_response("quiz_complete.html")