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