# HG changeset patch # User Brian Neal # Date 1348937987 18000 # Node ID 40ae28f33b3d10bc1a62c1dc4b72e54683fba0e9 # Parent 00c14431e911541a0bb0e87fbbb44b9542f04134 For issue 21, add a top donors template tag & display on donations page. diff -r 00c14431e911 -r 40ae28f33b3d donations/models.py --- 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.""" diff -r 00c14431e911 -r 40ae28f33b3d donations/templatetags/donations_tags.py --- 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) + } diff -r 00c14431e911 -r 40ae28f33b3d sg101/templates/donations/index.html --- 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 %}

Donations

@@ -48,7 +50,7 @@ {% endif %} -
+
Make A Donation @@ -101,4 +103,11 @@
+ +
+

Hall of Fame Donors

+{% cache 3600 top_donors_tag %} + {% top_donors 10 %} +{% endcache %} +
{% endblock %} diff -r 00c14431e911 -r 40ae28f33b3d sg101/templates/donations/top_donors_tag.html --- /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 %} +
    + {% for donor in top_donors %} +
  1. {% profile_link donor.username %}
  2. + {% endfor %} +
+{% else %} +

No donation data available at this time.

+{% endif %}