Mercurial > public > sg101
comparison gpp/donations/models.py @ 35:f77a1cdd7a46
Donations: first cut at a donations view and a form built for paypal.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Sun, 07 Jun 2009 00:22:50 +0000 |
parents | d5d7e510ecd7 |
children | 9fabeabd89d4 |
comparison
equal
deleted
inserted
replaced
34:d5d7e510ecd7 | 35:f77a1cdd7a46 |
---|---|
4 import datetime | 4 import datetime |
5 import decimal | 5 import decimal |
6 | 6 |
7 from django.db import models | 7 from django.db import models |
8 from django.contrib import auth | 8 from django.contrib import auth |
9 from django.conf import settings | |
9 | 10 |
10 ANONYMOUS = u'Anonymous' | |
11 | 11 |
12 class DonationManager(models.Manager): | 12 class DonationManager(models.Manager): |
13 def monthly_stats(self, year=None, month=None): | 13 def monthly_stats(self, year=None, month=None): |
14 """ | 14 """ |
15 Returns a dictionary of statistics for the given month in the given | 15 Returns a tuple of items for the given month in the given |
16 year. If year is None, the current year is used. If month is None, | 16 year. If year is None, the current year is used. If month is None, |
17 the current month is used. | 17 the current month is used. |
18 The returned dict has the following keys: | 18 The returned tuple has the following items, in order: |
19 (gross, net, donations) | |
20 where: | |
19 'gross': total gross donations | 21 'gross': total gross donations |
20 'net': total net donations | 22 'net': total net donations |
21 'donors': list of donor names | 23 'donations': list of donation objects |
22 """ | 24 """ |
23 today = datetime.date.today() | 25 today = datetime.date.today() |
24 if year is None: | 26 if year is None: |
25 year = today.year | 27 year = today.year |
26 if month is None: | 28 if month is None: |
27 month = today.month | 29 month = today.month |
28 | 30 |
29 qs = self.filter(payment_date__year=year, | 31 qs = self.filter(payment_date__year=year, |
30 payment_date__month=month, | 32 payment_date__month=month, |
31 test_ipn=False) | 33 test_ipn=False).select_related('user') |
32 | 34 |
33 stats = { | 35 gross = decimal.Decimal() |
34 'gross': decimal.Decimal(), | 36 net = decimal.Decimal() |
35 'net': decimal.Decimal(), | 37 donations = [] |
36 'donors': [], | |
37 } | |
38 for donation in qs: | 38 for donation in qs: |
39 stats['gross'] += donation.mc_gross | 39 gross += donation.mc_gross |
40 stats['net'] += donation.mc_gross - donation.mc_fee | 40 net += donation.mc_gross - donation.mc_fee |
41 stats['donors'].append(donation.donor()) | 41 donations.append(donation) |
42 | 42 |
43 return stats | 43 return gross, net, donations |
44 | 44 |
45 | 45 |
46 class Donation(models.Model): | 46 class Donation(models.Model): |
47 """Model to represent a donation to the website.""" | 47 """Model to represent a donation to the website.""" |
48 | 48 |
69 def __unicode__(self): | 69 def __unicode__(self): |
70 if self.user: | 70 if self.user: |
71 return u'%s from %s' % (self.mc_gross, self.user.username) | 71 return u'%s from %s' % (self.mc_gross, self.user.username) |
72 return u'%s from %s %s' % (self.mc_gross, self.first_name, self.last_name) | 72 return u'%s from %s %s' % (self.mc_gross, self.first_name, self.last_name) |
73 | 73 |
74 def save(self, *args, **kwargs): | |
75 if self.user is None: | |
76 self.is_anonymous = True | |
77 super(Donation, self).save(*args, **kwargs) | |
78 | |
79 def donor(self): | 74 def donor(self): |
80 """Returns the donor name for the donation.""" | 75 """Returns the donor name for the donation.""" |
81 if self.is_anonymous: | 76 if self.is_anonymous: |
82 return ANONYMOUS | 77 return settings.DONATIONS_ANON_NAME |
83 if self.user is not None: | 78 if self.user is not None: |
84 return self.user.username | 79 return self.user.username |
85 if self.first_name or self.last_name: | 80 if self.first_name or self.last_name: |
86 name = u'%s %s' % (self.first_name, self.last_name) | 81 name = u'%s %s' % (self.first_name, self.last_name) |
87 return name.strip() | 82 return name.strip() |
88 return ANONYMOUS | 83 return settings.DONATIONS_ANON_NAME |
89 | 84 |