bgneal@33
|
1 """
|
bgneal@33
|
2 Models for the donations application.
|
bgneal@33
|
3 """
|
bgneal@34
|
4 import datetime
|
bgneal@34
|
5 import decimal
|
bgneal@34
|
6
|
bgneal@33
|
7 from django.db import models
|
bgneal@33
|
8 from django.contrib import auth
|
bgneal@33
|
9
|
bgneal@34
|
10 ANONYMOUS = u'Anonymous'
|
bgneal@34
|
11
|
bgneal@34
|
12 class DonationManager(models.Manager):
|
bgneal@34
|
13 def monthly_stats(self, year=None, month=None):
|
bgneal@34
|
14 """
|
bgneal@34
|
15 Returns a dictionary of statistics for the given month in the given
|
bgneal@34
|
16 year. If year is None, the current year is used. If month is None,
|
bgneal@34
|
17 the current month is used.
|
bgneal@34
|
18 The returned dict has the following keys:
|
bgneal@34
|
19 'gross': total gross donations
|
bgneal@34
|
20 'net': total net donations
|
bgneal@34
|
21 'donors': list of donor names
|
bgneal@34
|
22 """
|
bgneal@34
|
23 today = datetime.date.today()
|
bgneal@34
|
24 if year is None:
|
bgneal@34
|
25 year = today.year
|
bgneal@34
|
26 if month is None:
|
bgneal@34
|
27 month = today.month
|
bgneal@34
|
28
|
bgneal@34
|
29 qs = self.filter(payment_date__year=year,
|
bgneal@34
|
30 payment_date__month=month,
|
bgneal@34
|
31 test_ipn=False)
|
bgneal@34
|
32
|
bgneal@34
|
33 stats = {
|
bgneal@34
|
34 'gross': decimal.Decimal(),
|
bgneal@34
|
35 'net': decimal.Decimal(),
|
bgneal@34
|
36 'donors': [],
|
bgneal@34
|
37 }
|
bgneal@34
|
38 for donation in qs:
|
bgneal@34
|
39 stats['gross'] += donation.mc_gross
|
bgneal@34
|
40 stats['net'] += donation.mc_gross - donation.mc_fee
|
bgneal@34
|
41 stats['donors'].append(donation.donor())
|
bgneal@34
|
42
|
bgneal@34
|
43 return stats
|
bgneal@34
|
44
|
bgneal@34
|
45
|
bgneal@33
|
46 class Donation(models.Model):
|
bgneal@33
|
47 """Model to represent a donation to the website."""
|
bgneal@33
|
48
|
bgneal@33
|
49 user = models.ForeignKey(auth.models.User, null=True, blank=True)
|
bgneal@33
|
50 is_anonymous = models.BooleanField()
|
bgneal@33
|
51 test_ipn = models.BooleanField(default=False, verbose_name="Test IPN")
|
bgneal@33
|
52 txn_id = models.CharField(max_length=20, verbose_name="Txn ID")
|
bgneal@33
|
53 txn_type = models.CharField(max_length=64)
|
bgneal@33
|
54 first_name = models.CharField(max_length=64, blank=True)
|
bgneal@33
|
55 last_name = models.CharField(max_length=64, blank=True)
|
bgneal@33
|
56 payer_email = models.EmailField(max_length=127, blank=True)
|
bgneal@33
|
57 payer_id = models.CharField(max_length=13, blank=True, verbose_name="Payer ID")
|
bgneal@33
|
58 mc_fee = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Fee")
|
bgneal@33
|
59 mc_gross = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Gross")
|
bgneal@33
|
60 memo = models.TextField(blank=True)
|
bgneal@33
|
61 payer_status = models.CharField(max_length=10, blank=True)
|
bgneal@33
|
62 payment_date = models.DateTimeField()
|
bgneal@33
|
63
|
bgneal@34
|
64 objects = DonationManager()
|
bgneal@34
|
65
|
bgneal@33
|
66 class Meta:
|
bgneal@33
|
67 ordering = ('-payment_date', )
|
bgneal@33
|
68
|
bgneal@33
|
69 def __unicode__(self):
|
bgneal@33
|
70 if self.user:
|
bgneal@33
|
71 return u'%s from %s' % (self.mc_gross, self.user.username)
|
bgneal@33
|
72 return u'%s from %s %s' % (self.mc_gross, self.first_name, self.last_name)
|
bgneal@33
|
73
|
bgneal@33
|
74 def save(self, *args, **kwargs):
|
bgneal@33
|
75 if self.user is None:
|
bgneal@33
|
76 self.is_anonymous = True
|
bgneal@33
|
77 super(Donation, self).save(*args, **kwargs)
|
bgneal@34
|
78
|
bgneal@34
|
79 def donor(self):
|
bgneal@34
|
80 """Returns the donor name for the donation."""
|
bgneal@34
|
81 if self.is_anonymous:
|
bgneal@34
|
82 return ANONYMOUS
|
bgneal@34
|
83 if self.user is not None:
|
bgneal@34
|
84 return self.user.username
|
bgneal@34
|
85 if self.first_name or self.last_name:
|
bgneal@34
|
86 name = u'%s %s' % (self.first_name, self.last_name)
|
bgneal@34
|
87 return name.strip()
|
bgneal@34
|
88 return ANONYMOUS
|
bgneal@34
|
89
|