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.db import models
5 from django.contrib.sites.models import Site
6 from django.urls import reverse
7 from django.utils.translation import ugettext_lazy as _
9 from django.conf import settings
13 description = models.TextField()
14 footer = models.TextField(null=True, blank=True)
17 verbose_name = _('quiz')
18 verbose_name_plural = _('quizzes')
21 def current(cls, request):
23 return cls.objects.get(domain=request.get_host().rstrip('.'))
24 except cls.DoesNotExist:
28 return self.question_set.all()[0]
30 def get_absolute_url(self):
31 return reverse('quiz')
35 return Result.objects.get(quiz=self).get_absolute_url()
36 except Result.DoesNotExist:
37 # just go to the beginning
38 return self.get_absolute_url()
41 class Result(models.Model):
42 quiz = models.ForeignKey(Quiz, models.CASCADE)
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']
55 def get_absolute_url(self):
56 return reverse('quiz_result', args=[self.slug])
59 class Question(models.Model):
60 quiz = models.ForeignKey(Quiz, models.CASCADE)
61 slug = models.SlugField(db_index=True)
62 ordering = models.SmallIntegerField()
63 title = models.CharField(max_length=255)
64 text = models.TextField(null=True, blank=True)
65 description = models.TextField(null=True, blank=True)
68 verbose_name = _('question')
69 verbose_name_plural = _('questions')
70 ordering = ['quiz', 'ordering']
71 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
76 def get_absolute_url(self):
77 return reverse('quiz', args=[self.slug])
80 later = self.quiz.question_set.filter(ordering__gt=self.ordering)
82 return later[0].get_absolute_url()
84 return self.quiz.where_to()
87 class Answer(models.Model):
88 title = models.CharField(max_length=255)
89 question = models.ForeignKey(Question, models.CASCADE)
90 go_to = models.ForeignKey(Question, models.SET_NULL, null=True, blank=True,
91 related_name='go_tos')
92 result = models.ForeignKey(Result, models.SET_NULL, null=True, blank=True)
93 ordering = models.SmallIntegerField()
96 verbose_name = _('answer')
97 verbose_name_plural = _('answers')
98 ordering = ['question', 'ordering']
104 # follow explicit redirects
106 return self.result.get_absolute_url()
108 return self.go_to.get_absolute_url()
110 # or just get the next question
111 return self.question.where_to()