1 from django.db import models
2 from django.contrib.sites.models import Site
3 from django.utils.translation import ugettext_lazy as _
5 from django.conf import settings
9 description = models.TextField()
12 verbose_name = _('quiz')
13 verbose_name_plural = _('quizzes')
17 return cls.objects.get(id=settings.SITE_ID)
20 return self.question_set.all()[0]
23 def get_absolute_url(self):
28 return Result.objects.get(quiz=self).get_absolute_url()
29 except Result.DoesNotExist:
30 # just go to the beginning
31 return self.get_absolute_url()
34 class Result(models.Model):
35 quiz = models.ForeignKey(Quiz)
36 slug = models.SlugField(db_index=True)
37 title = models.CharField(max_length=255)
38 text = models.TextField()
41 verbose_name = _('result')
42 verbose_name_plural = _('results')
44 def __unicode__(self):
48 def get_absolute_url(self):
49 return ('quiz_result', [self.slug])
52 class Question(models.Model):
53 quiz = models.ForeignKey(Quiz)
54 slug = models.SlugField(db_index=True)
55 ordering = models.SmallIntegerField()
56 title = models.CharField(max_length=255)
57 text = models.TextField(null=True, blank=True)
58 description = models.TextField(null=True, blank=True)
61 verbose_name = _('question')
62 verbose_name_plural = _('questions')
63 ordering = ['quiz', 'ordering']
64 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
66 def __unicode__(self):
70 def get_absolute_url(self):
71 return ('quiz', [self.slug])
74 later = self.quiz.question_set.filter(ordering__gt=self.ordering)
76 return later[0].get_absolute_url()
78 return self.quiz.where_to()
82 class Answer(models.Model):
83 title = models.CharField(max_length=255)
84 question = models.ForeignKey(Question)
85 go_to = models.ForeignKey(Question, null=True, blank=True,
86 related_name='go_tos')
87 result = models.ForeignKey(Result, null=True, blank=True)
88 ordering = models.SmallIntegerField()
91 verbose_name = _('answer')
92 verbose_name_plural = _('answers')
93 ordering = ['question', 'ordering']
95 def __unicode__(self):
99 # follow explicit redirects
101 return self.result.get_absolute_url()
103 return self.go_to.get_absolute_url()
105 # or just get the next question
106 return self.question.where_to()