1 # -*- coding: utf-8 -*-
 
   2 # This file is part of KOED-Quiz, licensed under GNU Affero GPLv3 or later.
 
   3 # Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
 
   5 from django.shortcuts import get_object_or_404, render, redirect
 
   7 from quiz.forms import QuestionForm
 
   8 from quiz.models import Quiz
 
  11 def question(request, slug=None):
 
  13         question = Quiz.current().start()
 
  14         request.session['ticket'] = [request.path]
 
  16         question = get_object_or_404(Quiz.current().question_set, slug=slug)
 
  18     ticket = request.session.get('ticket', [])
 
  19     valid = request.path in ticket
 
  22         cur_index = ticket.index(request.path)
 
  24             previous_url = ticket[cur_index - 1]
 
  26         valid_url = ticket[-1]
 
  28     if request.method == 'POST' and valid:
 
  29         form = QuestionForm(question, request.POST)
 
  32             answer = form.cleaned_data['answer']
 
  33             where_to = answer.where_to()
 
  35             del ticket[cur_index + 1:]
 
  37                 del ticket[ticket.index(where_to) + 1:]
 
  39                 ticket.append(where_to)
 
  41             request.session['ticket'] = ticket
 
  43             return redirect(where_to)
 
  45         form = QuestionForm(question)
 
  47     return render(request, "quiz/question_detail.html", locals())
 
  50 def result(request, slug=None):
 
  51     ticket = request.session.get('ticket', [])
 
  52     valid = request.path in ticket
 
  55         cur_index = ticket.index(request.path)
 
  57             previous_url = ticket[cur_index - 1]
 
  59         valid_url = ticket[-1]
 
  61     result = get_object_or_404(Quiz.current().result_set, slug=slug)
 
  62     return render(request, "quiz/result_detail.html", locals())