Mercurial > public > sg101
comparison donations/models.py @ 619:00c14431e911
Created a donations side block for BB issue 21.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 26 Sep 2012 19:33:26 -0500 |
parents | ee87ea74d46b |
children | 40ae28f33b3d |
comparison
equal
deleted
inserted
replaced
618:8c901a03b2e3 | 619:00c14431e911 |
---|---|
5 import decimal | 5 import decimal |
6 | 6 |
7 from django.db import models | 7 from django.db import models |
8 from django.contrib.auth.models import User | 8 from django.contrib.auth.models import User |
9 from django.conf import settings | 9 from django.conf import settings |
10 from django.db.models import Sum | |
10 | 11 |
11 | 12 |
12 class DonationManager(models.Manager): | 13 class DonationManager(models.Manager): |
14 """Manager for the Donations model.""" | |
15 | |
13 def monthly_stats(self, year=None, month=None): | 16 def monthly_stats(self, year=None, month=None): |
14 """ | 17 """ |
15 Returns a tuple of items for the given month in the given | 18 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, | 19 year. If year is None, the current year is used. If month is None, |
17 the current month is used. | 20 the current month is used. |
40 gross += donation.mc_gross | 43 gross += donation.mc_gross |
41 net += donation.mc_gross - donation.mc_fee | 44 net += donation.mc_gross - donation.mc_fee |
42 donations.append(donation) | 45 donations.append(donation) |
43 | 46 |
44 return gross, net, donations | 47 return gross, net, donations |
48 | |
49 def monthly_goal_pct(self, year=None, month=None, limit=True): | |
50 """Returns progress towards the given monthly goal as an integer | |
51 percent. | |
52 | |
53 If year is None, the current year is used. | |
54 If month is None, the current month is used. | |
55 If limit is True, the return value is limited to 100. | |
56 | |
57 """ | |
58 today = datetime.datetime.today() | |
59 if year is None: | |
60 year = today.year | |
61 if month is None: | |
62 month = today.month | |
63 | |
64 r = self.filter(payment_date__year=year, payment_date__month=month).aggregate( | |
65 Sum('mc_gross'), Sum('mc_fee')) | |
66 | |
67 gross, fee = r['mc_gross__sum'], r['mc_fee__sum'] | |
68 | |
69 if gross is not None and fee is not None: | |
70 pct = int((gross - fee) / settings.DONATIONS_GOAL * 100) | |
71 else: | |
72 pct = 0 | |
73 | |
74 if limit: | |
75 pct = min(pct, 100) | |
76 | |
77 return pct | |
45 | 78 |
46 | 79 |
47 class Donation(models.Model): | 80 class Donation(models.Model): |
48 """Model to represent a donation to the website.""" | 81 """Model to represent a donation to the website.""" |
49 | 82 |