author | nishanth |
Tue, 20 Apr 2010 21:06:54 +0530 | |
changeset 16 | ad51f38d0339 |
parent 15 | 99af908a4174 |
child 17 | 68c2932660b7 |
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 |
ea7d372bfbff
implemented more constraints on the register for test page
nishanth
parents:
13
diff
changeset
|
5 |
|
12 | 6 |
from django.contrib.auth.models import User |
7 |
from django.contrib.auth import login, logout, authenticate |
|
8 |
||
9 |
from django.shortcuts import redirect, render_to_response |
|
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 | 13 |
from offline.quiz.utils import gen_key |
16 | 14 |
from offline.quiz.models import Profile, QuestionBank, Quiz |
12 | 15 |
from offline.quiz.forms import UserRegisterForm |
16 |
||
17 |
def start_page(request): |
|
18 |
""" first see if user is authenticated. |
|
19 |
If he is, redirect to the page where quiz happens. |
|
20 |
Else register the user |
|
21 |
""" |
|
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 | 31 |
user = request.user |
32 |
if user.is_authenticated(): |
|
33 |
return redirect("/quiz/start/%s"%user.username) |
|
16 | 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 |
|
12 | 41 |
|
42 |
if request.method == "POST": |
|
43 |
form = UserRegisterForm(request.POST) |
|
44 |
if form.is_valid(): |
|
45 |
data = form.cleaned_data |
|
46 |
||
47 |
while True: |
|
48 |
try: |
|
49 |
username = gen_key(20) |
|
50 |
new_user = User.objects.create_user(username, "temp@temp.com", "123") |
|
51 |
break |
|
52 |
except IntegrityError: |
|
53 |
pass |
|
54 |
||
55 |
new_user.first_name = data['first_name'] |
|
56 |
new_user.last_name = data['last_name'] |
|
57 |
new_user.save() |
|
58 |
||
59 |
new_profile = Profile(user=new_user) |
|
60 |
new_profile.profession = data['profession'] |
|
61 |
new_profile.affiliated_to = data['affiliated_to'] |
|
62 |
new_profile.save() |
|
63 |
||
64 |
user = authenticate(username=username, password="123") |
|
65 |
login(request, user) |
|
66 |
return redirect("/quiz/start/%s"%username) |
|
67 |
||
68 |
else: |
|
69 |
return render_to_response('register.html',{'form':form}) |
|
70 |
else: |
|
71 |
form = UserRegisterForm() |
|
72 |
return render_to_response('register.html',{'form':form}) |
|
73 |
||
74 |
def start_quiz(request, username): |
|
15 | 75 |
""" get the user by his username. |
76 |
then check for the event quiz status |
|
77 |
then check if his ip has finished the quiz |
|
78 |
then make his quiz paper and redirect to the right question. |
|
79 |
""" |
|
80 |
||
16 | 81 |
user = request.user |
15 | 82 |
if not user.username == username: |
83 |
raise Http404 |
|
84 |
||
85 |
try: |
|
86 |
event = Event.objects.all()[0] |
|
87 |
except IndexError: |
|
88 |
raise Http404 |
|
89 |
||
90 |
if event.quiz_status == "00": |
|
91 |
raise Http404 |
|
92 |
||
16 | 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) |
|
15 | 100 |
|
16 | 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() |
|
15 | 105 |
|
16 | 106 |
return redirect("/quiz/answer/%s/%s"%(username,new_quiz.key)) |
107 |