From 1764cd62d691f122f12fdba3e367e58620d8dd6c Mon Sep 17 00:00:00 2001 From: eric Date: Sat, 7 Jul 2012 16:38:23 -0400 Subject: [PATCH] small optimization of supporter count --- core/models.py | 5 +++++ frontend/templates/manage_campaign.html | 2 +- frontend/templates/pledge.html | 2 +- frontend/templates/work.html | 4 ++-- payment/tests.py | 1 + 5 files changed, 10 insertions(+), 4 deletions(-) diff --git a/core/models.py b/core/models.py index d119c30d..cb6e7c26 100755 --- a/core/models.py +++ b/core/models.py @@ -316,6 +316,11 @@ class Campaign(models.Model): """nb: returns (distinct) supporter IDs, not supporter objects""" translist = self.transactions().filter(status=TRANSACTION_STATUS_ACTIVE).values_list('user', flat=True).distinct() return translist + + @property + def supporters_count(self): + # avoid transmitting the whole list if you don't need to; let the db do the count. + return self.transactions().filter(status=TRANSACTION_STATUS_ACTIVE).values_list('user', flat=True).distinct().count() def effective_premiums(self): """returns the available premiums for the Campaign including any default premiums""" diff --git a/frontend/templates/manage_campaign.html b/frontend/templates/manage_campaign.html index 46c8d6b0..cb666446 100644 --- a/frontend/templates/manage_campaign.html +++ b/frontend/templates/manage_campaign.html @@ -79,7 +79,7 @@ Please fix the following before launching your campaign:
- {{ work.last_campaign.supporters.count }} Ungluers have pledged ${{ work.last_campaign.current_total|intcomma }} + {{ work.last_campaign.supporters_count }} Ungluers have pledged ${{ work.last_campaign.current_total|intcomma }}
book list status diff --git a/frontend/templates/pledge.html b/frontend/templates/pledge.html index 27a01456..adf94005 100644 --- a/frontend/templates/pledge.html +++ b/frontend/templates/pledge.html @@ -37,7 +37,7 @@
- {{ work.last_campaign.supporters.count }} Ungluers have pledged ${{ work.last_campaign.current_total|intcomma }} + {{ work.last_campaign.supporters_count }} Ungluers have pledged ${{ work.last_campaign.current_total|intcomma }}
book list status diff --git a/frontend/templates/work.html b/frontend/templates/work.html index 18b249e9..57008fc8 100644 --- a/frontend/templates/work.html +++ b/frontend/templates/work.html @@ -181,10 +181,10 @@ $j(document).ready(function(){ {% if not work.first_ebook %}
{% if status == 'ACTIVE' %} - {% if work.last_campaign.supporters.count == 1 %} + {% if work.last_campaign.supporters_count == 1 %} One Ungluer has {% else %} - {{ work.last_campaign.supporters.count }} Ungluers have + {{ work.last_campaign.supporters_count }} Ungluers have {% endif %} pledged ${{ work.last_campaign.current_total|floatformat:0|intcomma }}
toward a ${{ work.last_campaign.target|floatformat:0|intcomma }} goal {% else %} diff --git a/payment/tests.py b/payment/tests.py index 0b5f1a98..06ce262c 100644 --- a/payment/tests.py +++ b/payment/tests.py @@ -331,6 +331,7 @@ class TransactionTest(TestCase): results = p.query_campaign(c,campaign_total=True, summary=False) self.assertEqual(results[0].amount, D('12.34')) self.assertEqual(c.left,c.target-D('12.34')) + self.assertEqual(c.supporters_count, 1) class BasicGuiTest(TestCase): def setUp(self):