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
11 verbose_name = _('quiz')
12 verbose_name_plural = _('quizzes')
16 return cls.objects.get(id=settings.SITE_ID)
19 return self.question_set.all()[0]
22 def get_absolute_url(self):
27 return Result.objects.get(quiz=self).get_absolute_url()
28 except Result.DoesNotExist:
29 # just go to the beginning
30 return self.get_absolute_url()
33 class Result(models.Model):
34 quiz = models.ForeignKey(Quiz)
35 slug = models.SlugField(db_index=True)
36 title = models.CharField(max_length=255)
37 text = models.TextField()
40 verbose_name = _('result')
41 verbose_name_plural = _('results')
43 def __unicode__(self):
47 def get_absolute_url(self):
48 return ('quiz_result', [self.slug])
51 class Question(models.Model):
52 quiz = models.ForeignKey(Quiz)
53 slug = models.SlugField(db_index=True)
54 ordering = models.SmallIntegerField()
55 title = models.CharField(max_length=255)
56 text = models.TextField(null=True, blank=True)
57 description = models.TextField(null=True, blank=True)
60 verbose_name = _('question')
61 verbose_name_plural = _('questions')
62 ordering = ['quiz', 'ordering']
63 unique_together = [['quiz', 'slug'], ['quiz', 'ordering']]
65 def __unicode__(self):
69 def get_absolute_url(self):
70 return ('quiz', [self.slug])
73 later = self.quiz.question_set.filter(ordering__gt=self.ordering)
75 return later[0].get_absolute_url()
77 return self.quiz.where_to()
81 class Answer(models.Model):
82 title = models.CharField(max_length=255)
83 question = models.ForeignKey(Question)
84 go_to = models.ForeignKey(Question, null=True, blank=True,
85 related_name='go_tos')
86 result = models.ForeignKey(Result, null=True, blank=True)
87 ordering = models.SmallIntegerField()
90 verbose_name = _('answer')
91 verbose_name_plural = _('answers')
92 ordering = ['question', 'ordering']
94 def __unicode__(self):
98 # follow explicit redirects
100 return self.result.get_absolute_url()
102 return self.go_to.get_absolute_url()
104 # or just get the next question
105 return self.question.where_to()