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')
43 ordering = ['quiz', 'title']
45 def __unicode__(self):
49 def get_absolute_url(self):
50 return ('quiz_result', [self.slug])
53 class Question(models.Model):
54 quiz = models.ForeignKey(Quiz)
55 slug = models.SlugField(db_index=True)
56 ordering = models.SmallIntegerField()
57 title = models.CharField(max_length=255)
58 text = models.TextField(null=True, blank=True)
59 description = models.TextField(null=True, blank=True)
62 verbose_name = _('question')
63 verbose_name_plural = _('questions')
64 ordering = ['quiz', 'ordering']
65 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
67 def __unicode__(self):
71 def get_absolute_url(self):
72 return ('quiz', [self.slug])
75 later = self.quiz.question_set.filter(ordering__gt=self.ordering)
77 return later[0].get_absolute_url()
79 return self.quiz.where_to()
83 class Answer(models.Model):
84 title = models.CharField(max_length=255)
85 question = models.ForeignKey(Question)
86 go_to = models.ForeignKey(Question, null=True, blank=True,
87 related_name='go_tos')
88 result = models.ForeignKey(Result, null=True, blank=True)
89 ordering = models.SmallIntegerField()
92 verbose_name = _('answer')
93 verbose_name_plural = _('answers')
94 ordering = ['question', 'ordering']
96 def __unicode__(self):
100 # follow explicit redirects
102 return self.result.get_absolute_url()
104 return self.go_to.get_absolute_url()
106 # or just get the next question
107 return self.question.where_to()