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.db import models
6 from django.contrib.sites.models import Site
7 from django.utils.encoding import python_2_unicode_compatible
8 from django.utils.translation import ugettext_lazy as _
10 from django.conf import settings
14 description = models.TextField()
15 footer = models.TextField(null=True, blank=True)
18 verbose_name = _('quiz')
19 verbose_name_plural = _('quizzes')
23 return cls.objects.get(id=settings.SITE_ID)
26 return self.question_set.all()[0]
29 def get_absolute_url(self):
34 return Result.objects.get(quiz=self).get_absolute_url()
35 except Result.DoesNotExist:
36 # just go to the beginning
37 return self.get_absolute_url()
40 @python_2_unicode_compatible
41 class Result(models.Model):
42 quiz = models.ForeignKey(Quiz)
43 slug = models.SlugField(db_index=True)
44 title = models.CharField(max_length=255)
45 text = models.TextField()
48 verbose_name = _('result')
49 verbose_name_plural = _('results')
50 ordering = ['quiz', 'title']
56 def get_absolute_url(self):
57 return ('quiz_result', [self.slug])
60 @python_2_unicode_compatible
61 class Question(models.Model):
62 quiz = models.ForeignKey(Quiz)
63 slug = models.SlugField(db_index=True)
64 ordering = models.SmallIntegerField()
65 title = models.CharField(max_length=255)
66 text = models.TextField(null=True, blank=True)
67 description = models.TextField(null=True, blank=True)
70 verbose_name = _('question')
71 verbose_name_plural = _('questions')
72 ordering = ['quiz', 'ordering']
73 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
79 def get_absolute_url(self):
80 return ('quiz', [self.slug])
83 later = self.quiz.question_set.filter(ordering__gt=self.ordering)
85 return later[0].get_absolute_url()
87 return self.quiz.where_to()
90 @python_2_unicode_compatible
91 class Answer(models.Model):
92 title = models.CharField(max_length=255)
93 question = models.ForeignKey(Question)
94 go_to = models.ForeignKey(Question, null=True, blank=True,
95 related_name='go_tos')
96 result = models.ForeignKey(Result, null=True, blank=True)
97 ordering = models.SmallIntegerField()
100 verbose_name = _('answer')
101 verbose_name_plural = _('answers')
102 ordering = ['question', 'ordering']
108 # follow explicit redirects
110 return self.result.get_absolute_url()
112 return self.go_to.get_absolute_url()
114 # or just get the next question
115 return self.question.where_to()