Diplay changes
[koed-quiz.git] / apps / quiz / views.py
1 from django.shortcuts import get_object_or_404, render, redirect
2
3 from quiz.forms import QuestionForm
4 from quiz.models import Quiz
5
6
7 def question(request, slug=None):
8     if slug is None:
9         question = Quiz.current().start()
10         request.session['ticket'] = [request.path]
11     else:
12         question = get_object_or_404(Quiz.current().question_set, slug=slug)
13
14     ticket = request.session.get('ticket')
15     valid = request.path in ticket
16     print ticket, valid
17     if valid:
18         cur_index = ticket.index(request.path)
19         if cur_index:
20             previous_url = ticket[cur_index - 1]
21     else:
22         valid_url = ticket[-1]
23
24     if request.method == 'POST' and valid:
25         form = QuestionForm(question, request.POST)
26         if form.is_valid():
27
28             answer = form.cleaned_data['answer']
29             where_to = answer.where_to()
30
31             del ticket[cur_index + 1:]
32             try:
33                 del ticket[ticket.index(where_to) + 1:]
34             except ValueError:
35                 ticket.append(where_to)
36
37             request.session['ticket'] = ticket
38
39             return redirect(where_to)
40     else:
41         form = QuestionForm(question)
42
43     return render(request, "quiz/question_detail.html", locals())
44
45
46 def result(request, slug=None):
47     ticket = request.session['ticket']
48     valid = request.path in ticket
49
50     if valid:
51         cur_index = ticket.index(request.path)
52         if cur_index:
53             previous_url = ticket[cur_index - 1]
54     else:
55         valid_url = ticket[-1]
56
57
58     result = get_object_or_404(Quiz.current().result_set, slug=slug)
59     return render(request, "quiz/result_detail.html", locals())
60