# HG changeset patch # User Brian Neal # Date 1244082175 0 # Node ID d5d7e510ecd74d913c3b2da407d1ca255c7d58cc # Parent c018872385ea5d7c72003a09471235ead1d33cd3 Donations: created a DonationManager that can retrieve monthly stats. diff -r c018872385ea -r d5d7e510ecd7 gpp/donations/models.py --- a/gpp/donations/models.py Wed Jun 03 00:59:17 2009 +0000 +++ b/gpp/donations/models.py Thu Jun 04 02:22:55 2009 +0000 @@ -1,9 +1,48 @@ """ Models for the donations application. """ +import datetime +import decimal + from django.db import models from django.contrib import auth +ANONYMOUS = u'Anonymous' + +class DonationManager(models.Manager): + def monthly_stats(self, year=None, month=None): + """ + Returns a dictionary of statistics for the given month in the given + year. If year is None, the current year is used. If month is None, + the current month is used. + The returned dict has the following keys: + 'gross': total gross donations + 'net': total net donations + 'donors': list of donor names + """ + today = datetime.date.today() + if year is None: + year = today.year + if month is None: + month = today.month + + qs = self.filter(payment_date__year=year, + payment_date__month=month, + test_ipn=False) + + stats = { + 'gross': decimal.Decimal(), + 'net': decimal.Decimal(), + 'donors': [], + } + for donation in qs: + stats['gross'] += donation.mc_gross + stats['net'] += donation.mc_gross - donation.mc_fee + stats['donors'].append(donation.donor()) + + return stats + + class Donation(models.Model): """Model to represent a donation to the website.""" @@ -22,6 +61,8 @@ payer_status = models.CharField(max_length=10, blank=True) payment_date = models.DateTimeField() + objects = DonationManager() + class Meta: ordering = ('-payment_date', ) @@ -34,3 +75,15 @@ if self.user is None: self.is_anonymous = True super(Donation, self).save(*args, **kwargs) + + def donor(self): + """Returns the donor name for the donation.""" + if self.is_anonymous: + return ANONYMOUS + if self.user is not None: + return self.user.username + if self.first_name or self.last_name: + name = u'%s %s' % (self.first_name, self.last_name) + return name.strip() + return ANONYMOUS +