Mercurial > public > sg101
comparison 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 |
comparison
equal
deleted
inserted
replaced
619:00c14431e911 | 620:40ae28f33b3d |
---|---|
74 if limit: | 74 if limit: |
75 pct = min(pct, 100) | 75 pct = min(pct, 100) |
76 | 76 |
77 return pct | 77 return pct |
78 | 78 |
79 def top_donors(self, n=10): | |
80 """Returns a list of the top n donors as user objects that have a | |
81 total_donations field annotation. | |
82 | |
83 The data is taken from non anonymous donations from logged in users. | |
84 | |
85 """ | |
86 qs = User.objects.filter(donation__isnull=False, | |
87 donation__is_anonymous=False) \ | |
88 .distinct() \ | |
89 .annotate(total_donations=Sum('donation__mc_gross')) \ | |
90 .order_by('-total_donations')[:n] | |
91 | |
92 return qs | |
93 | |
79 | 94 |
80 class Donation(models.Model): | 95 class Donation(models.Model): |
81 """Model to represent a donation to the website.""" | 96 """Model to represent a donation to the website.""" |
82 | 97 |
83 user = models.ForeignKey(User, null=True, blank=True) | 98 user = models.ForeignKey(User, null=True, blank=True) |