Exclude the pgAdmin repos from the FTP crawl.
[pgweb.git] / pgweb / survey / models.py
blob80f33c93d39dfb30b282c31937b1ca76c53c8034
1 from django.db import models
4 # internal text/value object
5 class SurveyQuestion(object):
6 def __init__(self, value, text):
7 self.value = value
8 self.text = text
11 class SurveyAnswerValues(object):
12 def __init__(self, option, votes, votespercent):
13 self.option = option
14 self.votes = votes
15 self.votespercent = votespercent
18 class Survey(models.Model):
19 question = models.CharField(max_length=500, null=False, blank=False)
20 opt1 = models.CharField(max_length=500, null=False, blank=False)
21 opt2 = models.CharField(max_length=500, null=False, blank=False)
22 opt3 = models.CharField(max_length=500, null=False, blank=True)
23 opt4 = models.CharField(max_length=500, null=False, blank=True)
24 opt5 = models.CharField(max_length=500, null=False, blank=True)
25 opt6 = models.CharField(max_length=500, null=False, blank=True)
26 opt7 = models.CharField(max_length=500, null=False, blank=True)
27 opt8 = models.CharField(max_length=500, null=False, blank=True)
28 posted = models.DateTimeField(null=False, auto_now_add=True)
29 current = models.BooleanField(null=False, default=False)
31 purge_urls = ('/community/survey', '/community/$')
33 def __str__(self):
34 return self.question
36 @property
37 def questions(self):
38 for i in range(1, 9):
39 v = getattr(self, "opt%s" % i)
40 if not v:
41 break
42 yield SurveyQuestion(i, v)
44 @property
45 def answers(self):
46 if not hasattr(self, "_answers"):
47 self._answers = SurveyAnswer.objects.get_or_create(survey=self)[0]
48 return self._answers
50 @property
51 def completeanswers(self):
52 for a in self._get_complete_answers():
53 yield SurveyAnswerValues(a[0], a[1], self.totalvotes > 0 and (100 * a[1] / self.totalvotes) or 0)
55 @property
56 def totalvotes(self):
57 if not hasattr(self, "_totalvotes"):
58 self._totalvotes = 0
59 for a in self._get_complete_answers():
60 self._totalvotes = self._totalvotes + a[1]
61 return self._totalvotes
63 def _get_complete_answers(self):
64 for i in range(1, 9):
65 q = getattr(self, "opt%s" % i)
66 if not q:
67 break
68 n = getattr(self.answers, "tot%s" % i)
69 yield (q, n)
71 def save(self):
72 # Make sure only one survey at a time can be the current one
73 # (there may be some small race conditions here, but the likelihood
74 # that two admins are editing the surveys at the same time...)
75 if self.current:
76 previous = Survey.objects.filter(current=True)
77 for p in previous:
78 if not p == self:
79 p.current = False
80 p.save() # primary key check avoids recursion
82 # Now that we've made any previously current ones non-current, we are
83 # free to save this one.
84 super(Survey, self).save()
87 class SurveyAnswer(models.Model):
88 survey = models.OneToOneField(Survey, null=False, blank=False, primary_key=True, on_delete=models.CASCADE)
89 tot1 = models.IntegerField(null=False, default=0)
90 tot2 = models.IntegerField(null=False, default=0)
91 tot3 = models.IntegerField(null=False, default=0)
92 tot4 = models.IntegerField(null=False, default=0)
93 tot5 = models.IntegerField(null=False, default=0)
94 tot6 = models.IntegerField(null=False, default=0)
95 tot7 = models.IntegerField(null=False, default=0)
96 tot8 = models.IntegerField(null=False, default=0)
98 purge_urls = ('/community/survey', )
101 class SurveyLock(models.Model):
102 ipaddr = models.GenericIPAddressField(null=False, blank=False)
103 time = models.DateTimeField(null=False, auto_now_add=True)