changeset 620:40ae28f33b3d

For issue 21, add a top donors template tag & display on donations page.
author Brian Neal <bgneal@gmail.com>
date Sat, 29 Sep 2012 11:59:47 -0500
parents 00c14431e911
children 08d83015e15d
files donations/models.py donations/templatetags/donations_tags.py sg101/templates/donations/index.html sg101/templates/donations/top_donors_tag.html
diffstat 4 files changed, 42 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/donations/models.py	Wed Sep 26 19:33:26 2012 -0500
+++ b/donations/models.py	Sat Sep 29 11:59:47 2012 -0500
@@ -76,6 +76,21 @@
 
         return pct
 
+    def top_donors(self, n=10):
+        """Returns a list of the top n donors as user objects that have a
+        total_donations field annotation.
+
+        The data is taken from non anonymous donations from logged in users.
+
+        """
+        qs = User.objects.filter(donation__isnull=False,
+                                 donation__is_anonymous=False) \
+                .distinct() \
+                .annotate(total_donations=Sum('donation__mc_gross')) \
+                .order_by('-total_donations')[:n]
+
+        return qs
+
 
 class Donation(models.Model):
     """Model to represent a donation to the website."""
--- a/donations/templatetags/donations_tags.py	Wed Sep 26 19:33:26 2012 -0500
+++ b/donations/templatetags/donations_tags.py	Sat Sep 29 11:59:47 2012 -0500
@@ -15,3 +15,10 @@
     return {
         'pct': Donation.objects.monthly_goal_pct()
     }
+
+
+@register.inclusion_tag('donations/top_donors_tag.html')
+def top_donors(n=10):
+    return {
+        'top_donors': Donation.objects.top_donors(n)
+    }
--- a/sg101/templates/donations/index.html	Wed Sep 26 19:33:26 2012 -0500
+++ b/sg101/templates/donations/index.html	Sat Sep 29 11:59:47 2012 -0500
@@ -1,6 +1,8 @@
 {% extends 'base.html' %}
 {% load url from future %}
 {% load bio_tags %}
+{% load cache %}
+{% load donations_tags %}
 {% block title %}Donations{% endblock %}
 {% block content %}
 <h2>Donations</h2>
@@ -48,7 +50,7 @@
 {% endif %}
 </div>
 
-<div class="span-19 last">
+<div class="span-9 append-1">
 <form action="{{ form_action }}" method="post">
 <fieldset>
    <legend>Make A Donation</legend>
@@ -101,4 +103,11 @@
 </fieldset>
 </form>
 </div>
+
+<div class="span-9 last">
+<h3>Hall of Fame Donors</h3>
+{% cache 3600 top_donors_tag %}
+   {% top_donors 10 %}
+{% endcache %}
+</div>
 {% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sg101/templates/donations/top_donors_tag.html	Sat Sep 29 11:59:47 2012 -0500
@@ -0,0 +1,10 @@
+{% load bio_tags %}
+{% if top_donors %}
+<ol>
+   {% for donor in top_donors %}
+   <li>{% profile_link donor.username %}</li>
+   {% endfor %}
+</ol>
+{% else %}
+<p>No donation data available at this time.</p>
+{% endif %}