diff options
Diffstat (limited to 'polls/models.py')
-rw-r--r-- | polls/models.py | 40 |
1 files changed, 39 insertions, 1 deletions
diff --git a/polls/models.py b/polls/models.py index 7128e0d..dac3d39 100644 --- a/polls/models.py +++ b/polls/models.py @@ -72,6 +72,13 @@ class Voter(models.Model): ordering = ['creation_date'] def __unicode__(self): return _("Vote from %(user)s") % {'user':self.user.name} + def getVotes(self, choice_ids): + '''Get votes for a subset of choices + ''' + query = Vote.objects.filter(voter=self) + query = query.extra(where=['choice_id IN (%s)' \ + % ",".join([str(choice_id) for choice_id in choice_ids])]) + return list(query.order_by('choice')) class Choice(models.Model): poll = models.ForeignKey(Poll) @@ -84,10 +91,41 @@ class Choice(models.Model): class Meta: ordering = ['order'] + def getSum(self): + '''Get the sum of votes for this choice''' + sum = 0 + for vote in Vote.objects.filter(choice=self): + sum += vote.value + return sum + + def changeOrder(self, idx=1): + ''' + Change a choice in the list + ''' + if (self.order + idx) < 0: + return + choices = Choice.objects.filter(poll=self.poll) + if self.order + idx > len(choices): + return + new_order = self.order + idx + for choice in choices: + if choice == self: + continue + if idx < 0 and choice.order < self.order \ + and choice.order >= new_order: + choice.order += 1 + choice.save() + if idx > 0 and choice.order > self.order \ + and choice.order <= new_order: + choice.order -= 1 + choice.save() + self.order = new_order + self.save() + class Vote(models.Model): voter = models.ForeignKey(Voter) choice = models.ForeignKey(Choice) VOTE = ((1, (_('Yes'), _('Yes'))), (0, (_('No'), _('Maybe')), ), (-1, (_('No'), _('No'))),) - value = models.IntegerField(choices=VOTE, blank=True, null=True) + value = models.IntegerField(choices=VOTE, blank=True, null=True)
\ No newline at end of file |