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()
16 verbose_name = _('quiz')
17 verbose_name_plural = _('quizzes')
21 return cls.objects.get(id=settings.SITE_ID)
24 return self.question_set.all()[0]
27 def get_absolute_url(self):
32 return Result.objects.get(quiz=self).get_absolute_url()
33 except Result.DoesNotExist:
34 # just go to the beginning
35 return self.get_absolute_url()
38 class Result(models.Model):
39 quiz = models.ForeignKey(Quiz)
40 slug = models.SlugField(db_index=True)
41 title = models.CharField(max_length=255)
42 text = models.TextField()
45 verbose_name = _('result')
46 verbose_name_plural = _('results')
47 ordering = ['quiz', 'title']
49 def __unicode__(self):
53 def get_absolute_url(self):
54 return ('quiz_result', [self.slug])
57 class Question(models.Model):
58 quiz = models.ForeignKey(Quiz)
59 slug = models.SlugField(db_index=True)
60 ordering = models.SmallIntegerField()
61 title = models.CharField(max_length=255)
62 text = models.TextField(null=True, blank=True)
63 description = models.TextField(null=True, blank=True)
66 verbose_name = _('question')
67 verbose_name_plural = _('questions')
68 ordering = ['quiz', 'ordering']
69 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
71 def __unicode__(self):
75 def get_absolute_url(self):
76 return ('quiz', [self.slug])
79 later = self.quiz.question_set.filter(ordering__gt=self.ordering)
81 return later[0].get_absolute_url()
83 return self.quiz.where_to()
87 class Answer(models.Model):
88 title = models.CharField(max_length=255)
89 question = models.ForeignKey(Question)
90 go_to = models.ForeignKey(Question, null=True, blank=True,
91 related_name='go_tos')
92 result = models.ForeignKey(Result, null=True, blank=True)
93 ordering = models.SmallIntegerField()
96 verbose_name = _('answer')
97 verbose_name_plural = _('answers')
98 ordering = ['question', 'ordering']
100 def __unicode__(self):
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()