comparison gpp/polls/models.py @ 440:ac9217eef610

Added total vote information to the poll templates. Cache the total votes in the model instance.
author Brian Neal <bgneal@gmail.com>
date Sat, 21 May 2011 20:35:02 +0000
parents 1f139de929c4
children
comparison
equal deleted inserted replaced
439:1f139de929c4 440:ac9217eef610
66 for choice in choices: 66 for choice in choices:
67 choice['pct'] = float(choice['votes']) / total_votes * 100.0 67 choice['pct'] = float(choice['votes']) / total_votes * 100.0
68 68
69 return (total_votes, choices) 69 return (total_votes, choices)
70 70
71 def total_votes(self): 71 def _total_votes(self):
72 """ 72 """
73 Returns the number of votes cast in this poll to date. 73 Returns the number of votes cast in this poll to date.
74 74
75 """ 75 """
76 return sum(choice.votes for choice in self.choice_set.all()) 76 if not hasattr(self, '_total_votes_cache'):
77 self._total_votes_cache = sum(choice.votes for choice in
78 self.choice_set.all())
79 return self._total_votes_cache
80 total_votes = property(_total_votes)
77 81
78 def is_open(self): 82 def is_open(self):
79 now = datetime.datetime.now() 83 now = datetime.datetime.now()
80 return self.start_date <= now and (not self.end_date or now <= self.end_date) 84 return self.start_date <= now and (not self.end_date or now <= self.end_date)
81 85