Mercurial > public > sg101
changeset 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 | 33d0c55e57a9 |
files | gpp/polls/models.py gpp/templates/polls/index.html gpp/templates/polls/poll.html |
diffstat | 3 files changed, 10 insertions(+), 3 deletions(-) [+] |
line wrap: on
line diff
--- a/gpp/polls/models.py Sat May 21 19:55:48 2011 +0000 +++ b/gpp/polls/models.py Sat May 21 20:35:02 2011 +0000 @@ -68,12 +68,16 @@ return (total_votes, choices) - def total_votes(self): + def _total_votes(self): """ Returns the number of votes cast in this poll to date. """ - return sum(choice.votes for choice in self.choice_set.all()) + if not hasattr(self, '_total_votes_cache'): + self._total_votes_cache = sum(choice.votes for choice in + self.choice_set.all()) + return self._total_votes_cache + total_votes = property(_total_votes) def is_open(self): now = datetime.datetime.now()
--- a/gpp/templates/polls/index.html Sat May 21 19:55:48 2011 +0000 +++ b/gpp/templates/polls/index.html Sat May 21 20:35:02 2011 +0000 @@ -8,6 +8,7 @@ <ul> {% for poll in current_polls %} <li><a href="{{ poll.get_absolute_url }}">{{ poll.question }}</a> • + {{ poll.total_votes }} vote{{ poll.total_votes|pluralize }} • {% get_comment_count for poll as comment_count %} {{ comment_count }} comment{{ comment_count|pluralize }} • {{ poll.start_date|date:"M d, Y" }} @@ -25,6 +26,7 @@ <ul> {% for poll in old_polls %} <li><a href="{{ poll.get_absolute_url }}">{{ poll.question }}</a> • + {{ poll.total_votes }} vote{{ poll.total_votes|pluralize }} • {% get_comment_count for poll as comment_count %} {{ comment_count }} comment{{ comment_count|pluralize }} • {{ poll.start_date|date:"M d, Y" }} -
--- a/gpp/templates/polls/poll.html Sat May 21 19:55:48 2011 +0000 +++ b/gpp/templates/polls/poll.html Sat May 21 20:35:02 2011 +0000 @@ -15,7 +15,8 @@ {% endif %} {% get_comment_count for poll as comment_count %} <p> -This poll has <a href="{% url 'polls.views.poll_results' poll.id %}">{{ comment_count }} comment{{ comment_count|pluralize }}</a>. +This poll has {{ poll.total_votes }} vote{{ poll.total_votes|pluralize }} and +<a href="{% url 'polls.views.poll_results' poll.id %}">{{ comment_count }} comment{{ comment_count|pluralize }}</a>. {% if poll.is_open %} Voting for this poll started on {{ poll.start_date|date:"F d, Y" }}. {% if poll.end_date %}