1 from django.shortcuts import get_object_or_404, render, redirect
3 from quiz.forms import QuestionForm
4 from quiz.models import Quiz
7 def question(request, slug=None):
9 question = Quiz.current().start()
10 request.session['ticket'] = [request.path]
12 question = get_object_or_404(Quiz.current().question_set, slug=slug)
14 ticket = request.session.get('ticket', [])
15 valid = request.path in ticket
18 cur_index = ticket.index(request.path)
20 previous_url = ticket[cur_index - 1]
22 valid_url = ticket[-1]
24 if request.method == 'POST' and valid:
25 form = QuestionForm(question, request.POST)
28 answer = form.cleaned_data['answer']
29 where_to = answer.where_to()
31 del ticket[cur_index + 1:]
33 del ticket[ticket.index(where_to) + 1:]
35 ticket.append(where_to)
37 request.session['ticket'] = ticket
39 return redirect(where_to)
41 form = QuestionForm(question)
43 return render(request, "quiz/question_detail.html", locals())
46 def result(request, slug=None):
47 ticket = request.session.get('ticket', [])
48 valid = request.path in ticket
51 cur_index = ticket.index(request.path)
53 previous_url = ticket[cur_index - 1]
55 valid_url = ticket[-1]
58 result = get_object_or_404(Quiz.current().result_set, slug=slug)
59 return render(request, "quiz/result_detail.html", locals())