author | nishanth |
Wed, 30 Jun 2010 10:29:38 +0530 | |
changeset 71 | 1279f0094687 |
parent 65 | 88a36f434284 |
child 72 | 665287ab8e3a |
permissions | -rw-r--r-- |
41 | 1 |
from django.db.models import Sum |
6 | 2 |
from django.http import Http404 |
9 | 3 |
from django.shortcuts import render_to_response, redirect |
6 | 4 |
|
39
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
5 |
from offline.settings import ADMIN_KEY |
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
6 |
|
6 | 7 |
from offline.event.models import Event |
63
21cdadb1b98e
replaced the old form with new one in view and template for opening quiz
nishanth
parents:
48
diff
changeset
|
8 |
from offline.event.forms import EventCreateForm, OpenQuizForm |
39
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
9 |
|
44 | 10 |
from offline.quiz.models import Quiz, QuestionBank |
39
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
11 |
from offline.quiz.utils import correct_quiz |
7
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
12 |
|
34
8d0d82c981b3
added "return to admin page" link in open_quiz page
nishanth
parents:
26
diff
changeset
|
13 |
num2name = {"11" : "Day 1 Quiz 1", |
8d0d82c981b3
added "return to admin page" link in open_quiz page
nishanth
parents:
26
diff
changeset
|
14 |
"12" : "Day 1 Quiz 2", |
8d0d82c981b3
added "return to admin page" link in open_quiz page
nishanth
parents:
26
diff
changeset
|
15 |
"21" : "Day 2 Quiz 1", |
8d0d82c981b3
added "return to admin page" link in open_quiz page
nishanth
parents:
26
diff
changeset
|
16 |
} |
8d0d82c981b3
added "return to admin page" link in open_quiz page
nishanth
parents:
26
diff
changeset
|
17 |
|
6 | 18 |
def event_home(request): |
19 |
""" The page for people to view. |
|
20 |
""" |
|
21 |
||
22 |
try: |
|
23 |
event = Event.objects.all()[0] |
|
24 |
except IndexError: |
|
25 |
raise Http404 |
|
26 |
||
27 |
ip = request.META['REMOTE_ADDR'] |
|
28 |
||
29 |
can_submit_feedback = True if event.feedback_status != '0' and \ |
|
30 |
not event.feedback.filter(day=event.feedback_status,user_ip=ip) else False |
|
23 | 31 |
can_take_quiz = True if event.quiz_status != "00" else False |
6 | 32 |
|
33 |
return render_to_response('home.html', {'event':event, 'can_submit_feedback':can_submit_feedback, 'can_take_quiz':can_take_quiz}) |
|
34 |
||
9 | 35 |
def event_admin(request, admin_key): |
7
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
36 |
""" see if the key is correct and then display options. |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
37 |
""" |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
38 |
|
9 | 39 |
if not admin_key == ADMIN_KEY: |
7
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
40 |
raise Http404 |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
41 |
|
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
42 |
try: |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
43 |
event = Event.objects.all()[0] |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
44 |
except IndexError: |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
45 |
return redirect('/event/create/%s'%ADMIN_KEY) |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
46 |
|
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
47 |
return render_to_response('admin.html', {'event':event, 'admin_key':ADMIN_KEY}) |
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
48 |
|
9 | 49 |
def event_create(request, admin_key): |
50 |
||
51 |
if not admin_key == ADMIN_KEY: |
|
52 |
raise Http404 |
|
53 |
||
54 |
try: |
|
55 |
event = Event.objects.all()[0] |
|
56 |
return redirect("/event/admin/%s"%admin_key) |
|
57 |
except IndexError: |
|
58 |
new_event = Event() |
|
7
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
59 |
|
9 | 60 |
if request.method == "POST": |
61 |
form = EventCreateForm(request.POST) |
|
62 |
if form.is_valid(): |
|
63 |
new_event.title = form.cleaned_data['title'] |
|
64 |
new_event.start_date = form.cleaned_data['start_date'] |
|
65 |
new_event.stop_date = form.cleaned_data['stop_date'] |
|
7
dfedb369f32e
first fixed on the urls and then gave the admin_key in settings and then created main admin page.
nishanth
parents:
6
diff
changeset
|
66 |
|
9 | 67 |
new_event.save() |
68 |
return redirect('/event/admin/%s'%ADMIN_KEY) |
|
69 |
else: |
|
70 |
return render_to_response('create_event.html',{'form':form}) |
|
71 |
else: |
|
72 |
form = EventCreateForm() |
|
73 |
return render_to_response('create_event.html',{'form':form}) |
|
23 | 74 |
|
75 |
def open_quiz(request, admin_key): |
|
25 | 76 |
""" check for admin and then for quiz status. |
77 |
""" |
|
78 |
||
79 |
if not admin_key == ADMIN_KEY: |
|
80 |
raise Http404 |
|
81 |
||
82 |
try: |
|
83 |
event = Event.objects.all()[0] |
|
84 |
except IndexError: |
|
85 |
return redirect('/event/create/%s'%ADMIN_KEY) |
|
23 | 86 |
|
25 | 87 |
if event.quiz_status != "00": |
88 |
return redirect('/event/create/%s'%ADMIN_KEY) |
|
89 |
||
90 |
if request.method=="POST": |
|
65
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
91 |
form = OpenQuizForm(request.POST) |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
92 |
if form.is_valid(): |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
93 |
data = form.cleaned_data |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
94 |
quiz_num = data['quiz_num'] |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
95 |
topics = data['topics'] |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
96 |
|
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
97 |
for topic in topics: |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
98 |
QuestionBank.objects.filter(topic=topic).update(quiz_num=quiz_num) |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
99 |
|
25 | 100 |
event.quiz_status = quiz_num |
101 |
event.save() |
|
34
8d0d82c981b3
added "return to admin page" link in open_quiz page
nishanth
parents:
26
diff
changeset
|
102 |
return render_to_response("open_quiz.html",{"admin_key":ADMIN_KEY, "quiz_name":num2name[quiz_num], 'success':True}) |
65
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
103 |
else: |
88a36f434284
added the required changes in open_quiz view. the design is now final and ready for use
nishanth
parents:
63
diff
changeset
|
104 |
return render_to_response("open_quiz.html", {"form":form}) |
25 | 105 |
else: |
63
21cdadb1b98e
replaced the old form with new one in view and template for opening quiz
nishanth
parents:
48
diff
changeset
|
106 |
form = OpenQuizForm() |
21cdadb1b98e
replaced the old form with new one in view and template for opening quiz
nishanth
parents:
48
diff
changeset
|
107 |
return render_to_response("open_quiz.html", {"form":form}) |
25 | 108 |
|
26 | 109 |
def close_quiz(request, admin_key): |
110 |
""" check for admin and then for quiz status. |
|
111 |
""" |
|
112 |
||
113 |
if not admin_key == ADMIN_KEY: |
|
114 |
raise Http404 |
|
115 |
||
116 |
try: |
|
117 |
event = Event.objects.all()[0] |
|
118 |
except IndexError: |
|
119 |
return redirect('/event/create/%s'%ADMIN_KEY) |
|
120 |
||
121 |
if event.quiz_status == "00": |
|
122 |
return redirect('/event/create/%s'%ADMIN_KEY) |
|
123 |
||
38 | 124 |
quiz_num = event.quiz_status |
125 |
||
26 | 126 |
event.quiz_status = "00" |
127 |
event.save() |
|
128 |
||
39
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
129 |
event_quizzes = event.quiz.filter(quiz_num=quiz_num) |
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
130 |
for quiz in event_quizzes: |
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
131 |
correct_quiz(quiz) |
0fa055b8ea98
implemented correct_quiz function and added it at the time of closing quiz and modified question bank
nishanth
parents:
38
diff
changeset
|
132 |
|
38 | 133 |
return render_to_response("close_quiz.html", {"admin_key":ADMIN_KEY, "quiz_name":num2name[quiz_num]}) |
134 |
||
41 | 135 |
def list_toppers(request, admin_key, quiz_num): |
136 |
""" check for admin key |
|
137 |
Then check for quiz_num in ['11', ..] |
|
138 |
then give appropriate response. |
|
139 |
""" |
|
140 |
||
141 |
if not admin_key == ADMIN_KEY: |
|
142 |
raise Http404 |
|
143 |
||
144 |
try: |
|
145 |
event = Event.objects.all()[0] |
|
146 |
except IndexError: |
|
147 |
return redirect('/event/create/%s'%ADMIN_KEY) |
|
148 |
||
149 |
if quiz_num not in ["11", "12", "21"]: |
|
150 |
raise Http404 |
|
151 |
||
152 |
quizzes = Quiz.objects.filter(event=event,quiz_num=quiz_num) |
|
153 |
quizzes_with_scores = quizzes.annotate(score=Sum('que_answered__is_correct')) |
|
154 |
sorted_quizzes = quizzes_with_scores.order_by("score").reverse() |
|
155 |
||
42
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
156 |
return render_to_response("list_toppers.html", {"sorted_quizzes":sorted_quizzes,"admin_key":ADMIN_KEY, "event":event, "quiz_name":num2name[quiz_num]}) |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
157 |
|
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
158 |
def user_dump(request, admin_key, quiz_key, user_name): |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
159 |
""" check for admin_key |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
160 |
then get quiz by quiz_key and user_name. |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
161 |
then display the dump |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
162 |
""" |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
163 |
|
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
164 |
if not admin_key == ADMIN_KEY: |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
165 |
raise Http404 |
41 | 166 |
|
42
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
167 |
try: |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
168 |
event = Event.objects.all()[0] |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
169 |
except IndexError: |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
170 |
return redirect('/event/create/%s'%ADMIN_KEY) |
44 | 171 |
|
42
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
172 |
try: |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
173 |
quiz = Quiz.objects.get(event=event, key=quiz_key) |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
174 |
except Quiz.DoesNotExist: |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
175 |
raise Http404 |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
176 |
|
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
177 |
if not quiz.user.username == user_name: |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
178 |
raise Http404 |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
179 |
|
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
180 |
quiz_name = num2name[quiz.quiz_num] |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
181 |
if request.method == "POST": |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
182 |
for ans in quiz.que_answered.all(): |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
183 |
ans.is_correct = True if str(ans.question.id) in request.POST else False |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
184 |
ans.save() |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
185 |
return redirect("/quiz/toppers/%s/%s"%(ADMIN_KEY, quiz.quiz_num)) |
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
186 |
else: |
44 | 187 |
return render_to_response("user_dump.html", {"quiz":quiz, "quiz_name":quiz_name, "admin_key":ADMIN_KEY}) |
188 |
||
189 |
def que_dump(request, admin_key, que_id): |
|
190 |
""" check for admin key |
|
191 |
then simply render all answers corresponding to the question. |
|
192 |
""" |
|
193 |
||
194 |
if not admin_key == ADMIN_KEY: |
|
195 |
raise Http404 |
|
196 |
||
197 |
try: |
|
198 |
event = Event.objects.all()[0] |
|
199 |
except IndexError: |
|
200 |
return redirect('/event/create/%s'%ADMIN_KEY) |
|
42
007d87112911
created a user_dump page that shows all the answers entered by user at one place
nishanth
parents:
41
diff
changeset
|
201 |
|
44 | 202 |
try: |
203 |
question = QuestionBank.objects.get(id=que_id) |
|
204 |
except QuestionBank.DoesNotExist: |
|
205 |
raise Http404 |
|
206 |
||
207 |
quiz_num = question.quiz_num |
|
208 |
quiz_name = num2name[quiz_num] |
|
209 |
||
210 |
answers = question.answer.all() |
|
211 |
||
212 |
if request.method == "POST": |
|
213 |
for ans in answers: |
|
214 |
ans.is_correct = True if str(ans.id) in request.POST else False |
|
215 |
ans.save() |
|
216 |
return redirect("/quiz/toppers/%s/%s"%(ADMIN_KEY, quiz_num)) |
|
217 |
else: |
|
48
aae2f69ba89c
updated the que_dump template to show the question properly.
nishanth
parents:
44
diff
changeset
|
218 |
return render_to_response("que_dump.html", {"quiz_name":quiz_name, "question":question, "answers":answers}) |