1 # This file is part of KOED-Quiz, licensed under GNU Affero GPLv3 or later.
2 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
4 from django.shortcuts import get_object_or_404, render, redirect
6 from quiz.forms import QuestionForm
7 from quiz.models import Quiz
10 def question(request, slug=None):
12 question = request.current_quiz.start()
13 request.session['ticket'] = [request.path]
15 question = get_object_or_404(request.current_quiz.question_set, slug=slug)
17 ticket = request.session.get('ticket', [])
18 valid = request.path in ticket
21 cur_index = ticket.index(request.path)
23 previous_url = ticket[cur_index - 1]
25 valid_url = ticket[-1]
27 if request.method == 'POST' and valid:
28 form = QuestionForm(question, request.POST)
31 answer = form.cleaned_data['answer']
32 where_to = answer.where_to()
34 del ticket[cur_index + 1:]
36 del ticket[ticket.index(where_to) + 1:]
38 ticket.append(where_to)
40 request.session['ticket'] = ticket
42 return redirect(where_to)
44 form = QuestionForm(question)
46 return render(request, "quiz/question_detail.html", locals())
49 def result(request, slug=None):
50 ticket = request.session.get('ticket', [])
51 valid = request.path in ticket
54 cur_index = ticket.index(request.path)
56 previous_url = ticket[cur_index - 1]
58 valid_url = ticket[-1]
60 result = get_object_or_404(request.current_quiz.result_set, slug=slug)
61 return render(request, "quiz/result_detail.html", locals())