comparison gpp/donations/models.py @ 34:d5d7e510ecd7

Donations: created a DonationManager that can retrieve monthly stats.
author Brian Neal <bgneal@gmail.com>
date Thu, 04 Jun 2009 02:22:55 +0000
parents c018872385ea
children f77a1cdd7a46
comparison
equal deleted inserted replaced
33:c018872385ea 34:d5d7e510ecd7
1 """ 1 """
2 Models for the donations application. 2 Models for the donations application.
3 """ 3 """
4 import datetime
5 import decimal
6
4 from django.db import models 7 from django.db import models
5 from django.contrib import auth 8 from django.contrib import auth
9
10 ANONYMOUS = u'Anonymous'
11
12 class DonationManager(models.Manager):
13 def monthly_stats(self, year=None, month=None):
14 """
15 Returns a dictionary of statistics for the given month in the given
16 year. If year is None, the current year is used. If month is None,
17 the current month is used.
18 The returned dict has the following keys:
19 'gross': total gross donations
20 'net': total net donations
21 'donors': list of donor names
22 """
23 today = datetime.date.today()
24 if year is None:
25 year = today.year
26 if month is None:
27 month = today.month
28
29 qs = self.filter(payment_date__year=year,
30 payment_date__month=month,
31 test_ipn=False)
32
33 stats = {
34 'gross': decimal.Decimal(),
35 'net': decimal.Decimal(),
36 'donors': [],
37 }
38 for donation in qs:
39 stats['gross'] += donation.mc_gross
40 stats['net'] += donation.mc_gross - donation.mc_fee
41 stats['donors'].append(donation.donor())
42
43 return stats
44
6 45
7 class Donation(models.Model): 46 class Donation(models.Model):
8 """Model to represent a donation to the website.""" 47 """Model to represent a donation to the website."""
9 48
10 user = models.ForeignKey(auth.models.User, null=True, blank=True) 49 user = models.ForeignKey(auth.models.User, null=True, blank=True)
20 mc_gross = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Gross") 59 mc_gross = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Gross")
21 memo = models.TextField(blank=True) 60 memo = models.TextField(blank=True)
22 payer_status = models.CharField(max_length=10, blank=True) 61 payer_status = models.CharField(max_length=10, blank=True)
23 payment_date = models.DateTimeField() 62 payment_date = models.DateTimeField()
24 63
64 objects = DonationManager()
65
25 class Meta: 66 class Meta:
26 ordering = ('-payment_date', ) 67 ordering = ('-payment_date', )
27 68
28 def __unicode__(self): 69 def __unicode__(self):
29 if self.user: 70 if self.user:
32 73
33 def save(self, *args, **kwargs): 74 def save(self, *args, **kwargs):
34 if self.user is None: 75 if self.user is None:
35 self.is_anonymous = True 76 self.is_anonymous = True
36 super(Donation, self).save(*args, **kwargs) 77 super(Donation, self).save(*args, **kwargs)
78
79 def donor(self):
80 """Returns the donor name for the donation."""
81 if self.is_anonymous:
82 return ANONYMOUS
83 if self.user is not None:
84 return self.user.username
85 if self.first_name or self.last_name:
86 name = u'%s %s' % (self.first_name, self.last_name)
87 return name.strip()
88 return ANONYMOUS
89