root/questionnaire/models.py

Revision 1, 3.4 KB (checked in by alex, 4 years ago)
  • initial djangoquest import
Line 
1from django.db import models
2
3class RegUser(models.Model):
4    email = models.EmailField(u"Emailaddress", unique = True)
5    password = models.CharField(u"Password", max_length=200, blank = True, help_text = u'If you leave this empty, a password will be generated when you send an invitation to the user')
6
7    class Meta:
8        verbose_name = u'User'
9
10    def check_complete(self):
11        questions = Question.objects.all()
12        for question in questions:
13            answer = Answer.objects.filter(question = question, user = self)
14            if len(answer) == 0:
15                return False
16        return True
17
18    def __unicode__(self):
19        return u'%s' % (self.email)
20
21class Category(models.Model):
22    nr = models.IntegerField(u"Category number", help_text = u"This is used for determining the order of the categories")
23    name = models.CharField(u"Category name", max_length=200)
24    text = models.TextField(u"Introduction text", help_text = u"This is displayed above the list of questions you add to this category")
25
26    class Meta:
27        verbose_name_plural = u'Categories'
28       
29    def __unicode__(self):
30        return u'%s' % (self.name)
31
32    def questions(self):
33        return Question.objects.filter(category = self).order_by('nr')
34
35class Question(models.Model):
36    nr = models.IntegerField(u"Question number", help_text = "This is used for determining the order of the questions in a particular category")
37    text = models.CharField(u"Question text", max_length=200)
38    type = models.IntegerField(u"Type of question", choices = ( (1, u'Open'), (2, u'Multiple-choice'), (3, u'Multiple-choice, multiple-answers'), (4, u'Open, textfield') ), help_text = u"Determines the means of answering the question. An open question gives the user a single-line textfield, multiple-choice gives the user a number of choices he/she can choose from. If a question is multiple-choice, enter the choices this user can choose from below'")
39    category = models.ForeignKey(Category, help_text = u"The category that this question is part of. The user sees each category in-order on a single page, with all questions belonging to the category on the page")
40       
41    def __unicode__(self):
42        return u'%s %s %s' % (str(self.category), self.nr, self.text)
43
44    def get_choices(self):
45        choices = Choice.objects.filter(question = self).order_by('nr')
46        if len(choices) == 0:
47            return []
48        return choices
49
50class Choice(models.Model):
51    question = models.ForeignKey(Question)
52    nr = models.IntegerField(u"Choice number", help_text = u"Determines the order of the choices")
53    name = models.CharField(u"Choice text", max_length=200)
54
55    def __unicode__(self):
56        return u'%s %s' % (self.nr, self.name)
57
58class Answer(models.Model):
59    user = models.ForeignKey(RegUser, help_text = u"The user who supplied this answer")
60    question = models.ForeignKey(Question, help_text = u"The question that this an answer to")
61    answer = models.TextField()
62
63    def choice_str(self, secondary = False):
64        choice_string = ""
65        choices = self.question.get_choices()
66        split_answers = self.answer.split()
67
68        for choice in choices:
69            for split_answer in split_answers:
70                if str(split_answer) == str(choice.nr):
71                    choice_string += str(choice.name) + " " 
72        return choice_string
73
74    def __unicode__(self):
75        return u'%s %s %s' % (str(self.user), str(self.question), str(self.answer))
76   
Note: See TracBrowser for help on using the browser.