author | nishanth |
Tue, 20 Apr 2010 22:05:32 +0530 | |
changeset 21 | ff6d34fc7137 |
parent 20 | bcfa305cb32c |
child 24 | 2368bb63ee39 |
permissions | -rw-r--r-- |
16 | 1 |
import random |
2 |
||
12 | 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 | 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 | 7 |
from django.contrib.auth.models import User |
8 |
from django.contrib.auth import login, logout, authenticate |
|
9 |
||
10 |
from django.shortcuts import redirect, render_to_response |
|
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 | 14 |
from offline.quiz.utils import gen_key |
19 | 15 |
from offline.quiz.models import Profile, QuestionBank, Quiz, Answer |
12 | 16 |
from offline.quiz.forms import UserRegisterForm |
17 |
||
18 |
def start_page(request): |
|
19 |
""" first see if user is authenticated. |
|
20 |
If he is, redirect to the page where quiz happens. |
|
21 |
Else register the user |
|
22 |
""" |
|
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 | 32 |
user = request.user |
33 |
if user.is_authenticated(): |
|
34 |
return redirect("/quiz/start/%s"%user.username) |
|
16 | 35 |
else: |
36 |
try: |
|
37 |
ip = request.META['REMOTE_ADDR'] |
|
38 |
Quiz.objects.get(user_ip=ip, quiz_num=event.quiz_status) |
|
39 |
return redirect("/quiz/complete") |
|
40 |
except Quiz.DoesNotExist: |
|
41 |
pass |
|
12 | 42 |
|
43 |
if request.method == "POST": |
|
44 |
form = UserRegisterForm(request.POST) |
|
45 |
if form.is_valid(): |
|
46 |
data = form.cleaned_data |
|
47 |
||
48 |
while True: |
|
49 |
try: |
|
50 |
username = gen_key(20) |
|
51 |
new_user = User.objects.create_user(username, "temp@temp.com", "123") |
|
52 |
break |
|
53 |
except IntegrityError: |
|
54 |
pass |
|
55 |
||
56 |
new_user.first_name = data['first_name'] |
|
57 |
new_user.last_name = data['last_name'] |
|
58 |
new_user.save() |
|
59 |
||
60 |
new_profile = Profile(user=new_user) |
|
61 |
new_profile.profession = data['profession'] |
|
62 |
new_profile.affiliated_to = data['affiliated_to'] |
|
63 |
new_profile.save() |
|
64 |
||
65 |
user = authenticate(username=username, password="123") |
|
66 |
login(request, user) |
|
67 |
return redirect("/quiz/start/%s"%username) |
|
68 |
||
69 |
else: |
|
70 |
return render_to_response('register.html',{'form':form}) |
|
71 |
else: |
|
72 |
form = UserRegisterForm() |
|
73 |
return render_to_response('register.html',{'form':form}) |
|
74 |
||
18 | 75 |
def start_quiz(request): |
15 | 76 |
""" get the user by his username. |
77 |
then check for the event quiz status |
|
78 |
then check if his ip has finished the quiz |
|
79 |
then make his quiz paper and redirect to the right question. |
|
80 |
""" |
|
81 |
||
16 | 82 |
user = request.user |
15 | 83 |
|
84 |
try: |
|
85 |
event = Event.objects.all()[0] |
|
86 |
except IndexError: |
|
87 |
raise Http404 |
|
88 |
||
89 |
if event.quiz_status == "00": |
|
90 |
raise Http404 |
|
91 |
||
16 | 92 |
try: |
93 |
old_quiz = Quiz.objects.get(event=event,user=user, quiz_num=event.quiz_status) |
|
17 | 94 |
return redirect("/quiz/answer/%s"%(old_quiz.key)) |
16 | 95 |
except Quiz.DoesNotExist: |
96 |
ip = request.META['REMOTE_ADDR'] |
|
97 |
key = gen_key(10) |
|
98 |
new_quiz = Quiz(event=event, user=user, quiz_num=event.quiz_status, user_ip=ip, key=key) |
|
15 | 99 |
|
16 | 100 |
available_que_ids = [ str(_.id) for _ in QuestionBank.objects.filter(quiz_num=event.quiz_status) ] |
101 |
random.shuffle(available_que_ids) |
|
102 |
new_quiz.que_remaining = "|".join(available_que_ids) |
|
103 |
new_quiz.save() |
|
15 | 104 |
|
17 | 105 |
return redirect("/quiz/answer/%s"%(new_quiz.key)) |
16 | 106 |
|
19 | 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 |
||
20 | 145 |
quiz.que_answered.add(new_answer) |
19 | 146 |
remaining_que.remove(que_id) |
147 |
quiz.que_remaining = "|".join(remaining_que) |
|
148 |
quiz.key = gen_key(10) |
|
149 |
quiz.save() |
|
150 |
||
151 |
if not remaining_que: |
|
152 |
return redirect('/quiz/complete') |
|
153 |
else: |
|
154 |
return redirect('/quiz/answer/%s'%quiz.key) |
|
155 |
else: |
|
156 |
return render_to_response('display_question.html', {'question':question}) |
|
157 |
||
158 |
def quiz_complete(request): |
|
159 |
""" display thanq and log the user out. |
|
160 |
""" |
|
161 |
||
162 |
user = request.user |
|
163 |
logout(request) |
|
21 | 164 |
return render_to_response("quiz_complete.html") |