diff donations/models.py @ 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 e14f54f16dbc
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."""