annotate gpp/donations/models.py @ 557:7247a406f92b

Merging change to requirements.txt made on production server.
author Brian Neal <bgneal@gmail.com>
date Sun, 29 Jan 2012 14:45:09 -0600
parents 2177a71b680c
children
rev   line source
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@259 8 from django.contrib.auth.models import User
bgneal@35 9 from django.conf import settings
bgneal@33 10
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@35 15 Returns a tuple of items 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@35 18 The returned tuple has the following items, in order:
bgneal@35 19 (gross, net, donations)
bgneal@35 20 where:
bgneal@34 21 'gross': total gross donations
bgneal@34 22 'net': total net donations
bgneal@35 23 'donations': list of donation objects
bgneal@34 24 """
bgneal@34 25 today = datetime.date.today()
bgneal@34 26 if year is None:
bgneal@34 27 year = today.year
bgneal@34 28 if month is None:
bgneal@34 29 month = today.month
bgneal@34 30
bgneal@34 31 qs = self.filter(payment_date__year=year,
bgneal@34 32 payment_date__month=month,
bgneal@350 33 test_ipn=settings.DONATIONS_DEBUG).order_by(
bgneal@350 34 'payment_date').select_related('user')
bgneal@34 35
bgneal@35 36 gross = decimal.Decimal()
bgneal@35 37 net = decimal.Decimal()
bgneal@35 38 donations = []
bgneal@34 39 for donation in qs:
bgneal@35 40 gross += donation.mc_gross
bgneal@35 41 net += donation.mc_gross - donation.mc_fee
bgneal@35 42 donations.append(donation)
bgneal@34 43
bgneal@35 44 return gross, net, donations
bgneal@34 45
bgneal@34 46
bgneal@33 47 class Donation(models.Model):
bgneal@33 48 """Model to represent a donation to the website."""
bgneal@33 49
bgneal@259 50 user = models.ForeignKey(User, null=True, blank=True)
bgneal@33 51 is_anonymous = models.BooleanField()
bgneal@33 52 test_ipn = models.BooleanField(default=False, verbose_name="Test IPN")
bgneal@33 53 txn_id = models.CharField(max_length=20, verbose_name="Txn ID")
bgneal@33 54 txn_type = models.CharField(max_length=64)
bgneal@33 55 first_name = models.CharField(max_length=64, blank=True)
bgneal@33 56 last_name = models.CharField(max_length=64, blank=True)
bgneal@33 57 payer_email = models.EmailField(max_length=127, blank=True)
bgneal@33 58 payer_id = models.CharField(max_length=13, blank=True, verbose_name="Payer ID")
bgneal@33 59 mc_fee = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Fee")
bgneal@33 60 mc_gross = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Gross")
bgneal@33 61 memo = models.TextField(blank=True)
bgneal@33 62 payer_status = models.CharField(max_length=10, blank=True)
bgneal@33 63 payment_date = models.DateTimeField()
bgneal@33 64
bgneal@34 65 objects = DonationManager()
bgneal@34 66
bgneal@33 67 class Meta:
bgneal@33 68 ordering = ('-payment_date', )
bgneal@33 69
bgneal@33 70 def __unicode__(self):
bgneal@33 71 if self.user:
bgneal@33 72 return u'%s from %s' % (self.mc_gross, self.user.username)
bgneal@33 73 return u'%s from %s %s' % (self.mc_gross, self.first_name, self.last_name)
bgneal@33 74
bgneal@34 75 def donor(self):
bgneal@34 76 """Returns the donor name for the donation."""
bgneal@34 77 if self.is_anonymous:
bgneal@35 78 return settings.DONATIONS_ANON_NAME
bgneal@34 79 if self.user is not None:
bgneal@34 80 return self.user.username
bgneal@34 81 if self.first_name or self.last_name:
bgneal@34 82 name = u'%s %s' % (self.first_name, self.last_name)
bgneal@34 83 return name.strip()
bgneal@35 84 return settings.DONATIONS_ANON_NAME
bgneal@34 85