comparison donations/models.py @ 581:ee87ea74d46b

For Django 1.4, rearranged project structure for new manage.py.
author Brian Neal <bgneal@gmail.com>
date Sat, 05 May 2012 17:10:48 -0500
parents gpp/donations/models.py@2177a71b680c
children 00c14431e911
comparison
equal deleted inserted replaced
580:c525f3e0b5d0 581:ee87ea74d46b
1 """
2 Models for the donations application.
3 """
4 import datetime
5 import decimal
6
7 from django.db import models
8 from django.contrib.auth.models import User
9 from django.conf import settings
10
11
12 class DonationManager(models.Manager):
13 def monthly_stats(self, year=None, month=None):
14 """
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,
17 the current month is used.
18 The returned tuple has the following items, in order:
19 (gross, net, donations)
20 where:
21 'gross': total gross donations
22 'net': total net donations
23 'donations': list of donation objects
24 """
25 today = datetime.date.today()
26 if year is None:
27 year = today.year
28 if month is None:
29 month = today.month
30
31 qs = self.filter(payment_date__year=year,
32 payment_date__month=month,
33 test_ipn=settings.DONATIONS_DEBUG).order_by(
34 'payment_date').select_related('user')
35
36 gross = decimal.Decimal()
37 net = decimal.Decimal()
38 donations = []
39 for donation in qs:
40 gross += donation.mc_gross
41 net += donation.mc_gross - donation.mc_fee
42 donations.append(donation)
43
44 return gross, net, donations
45
46
47 class Donation(models.Model):
48 """Model to represent a donation to the website."""
49
50 user = models.ForeignKey(User, null=True, blank=True)
51 is_anonymous = models.BooleanField()
52 test_ipn = models.BooleanField(default=False, verbose_name="Test IPN")
53 txn_id = models.CharField(max_length=20, verbose_name="Txn ID")
54 txn_type = models.CharField(max_length=64)
55 first_name = models.CharField(max_length=64, blank=True)
56 last_name = models.CharField(max_length=64, blank=True)
57 payer_email = models.EmailField(max_length=127, blank=True)
58 payer_id = models.CharField(max_length=13, blank=True, verbose_name="Payer ID")
59 mc_fee = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Fee")
60 mc_gross = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Gross")
61 memo = models.TextField(blank=True)
62 payer_status = models.CharField(max_length=10, blank=True)
63 payment_date = models.DateTimeField()
64
65 objects = DonationManager()
66
67 class Meta:
68 ordering = ('-payment_date', )
69
70 def __unicode__(self):
71 if self.user:
72 return u'%s from %s' % (self.mc_gross, self.user.username)
73 return u'%s from %s %s' % (self.mc_gross, self.first_name, self.last_name)
74
75 def donor(self):
76 """Returns the donor name for the donation."""
77 if self.is_anonymous:
78 return settings.DONATIONS_ANON_NAME
79 if self.user is not None:
80 return self.user.username
81 if self.first_name or self.last_name:
82 name = u'%s %s' % (self.first_name, self.last_name)
83 return name.strip()
84 return settings.DONATIONS_ANON_NAME
85