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.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')
22 return cls.objects.get(id=settings.SITE_ID)
25 return self.question_set.all()[0]
28 def get_absolute_url(self):
33 return Result.objects.get(quiz=self).get_absolute_url()
34 except Result.DoesNotExist:
35 # just go to the beginning
36 return self.get_absolute_url()
39 class Result(models.Model):
40 quiz = models.ForeignKey(Quiz)
41 slug = models.SlugField(db_index=True)
42 title = models.CharField(max_length=255)
43 text = models.TextField()
46 verbose_name = _('result')
47 verbose_name_plural = _('results')
48 ordering = ['quiz', 'title']
50 def __unicode__(self):
54 def get_absolute_url(self):
55 return ('quiz_result', [self.slug])
58 class Question(models.Model):
59 quiz = models.ForeignKey(Quiz)
60 slug = models.SlugField(db_index=True)
61 ordering = models.SmallIntegerField()
62 title = models.CharField(max_length=255)
63 text = models.TextField(null=True, blank=True)
64 description = models.TextField(null=True, blank=True)
67 verbose_name = _('question')
68 verbose_name_plural = _('questions')
69 ordering = ['quiz', 'ordering']
70 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
72 def __unicode__(self):
76 def get_absolute_url(self):
77 return ('quiz', [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()
88 class Answer(models.Model):
89 title = models.CharField(max_length=255)
90 question = models.ForeignKey(Question)
91 go_to = models.ForeignKey(Question, null=True, blank=True,
92 related_name='go_tos')
93 result = models.ForeignKey(Result, null=True, blank=True)
94 ordering = models.SmallIntegerField()
97 verbose_name = _('answer')
98 verbose_name_plural = _('answers')
99 ordering = ['question', 'ordering']
101 def __unicode__(self):
105 # follow explicit redirects
107 return self.result.get_absolute_url()
109 return self.go_to.get_absolute_url()
111 # or just get the next question
112 return self.question.where_to()