bgneal@33: """ bgneal@33: Models for the donations application. bgneal@33: """ bgneal@34: import datetime bgneal@34: import decimal bgneal@34: bgneal@33: from django.db import models bgneal@33: from django.contrib import auth bgneal@35: from django.conf import settings bgneal@33: bgneal@34: bgneal@34: class DonationManager(models.Manager): bgneal@34: def monthly_stats(self, year=None, month=None): bgneal@34: """ bgneal@35: Returns a tuple of items for the given month in the given bgneal@34: year. If year is None, the current year is used. If month is None, bgneal@34: the current month is used. bgneal@35: The returned tuple has the following items, in order: bgneal@35: (gross, net, donations) bgneal@35: where: bgneal@34: 'gross': total gross donations bgneal@34: 'net': total net donations bgneal@35: 'donations': list of donation objects bgneal@34: """ bgneal@34: today = datetime.date.today() bgneal@34: if year is None: bgneal@34: year = today.year bgneal@34: if month is None: bgneal@34: month = today.month bgneal@34: bgneal@34: qs = self.filter(payment_date__year=year, bgneal@34: payment_date__month=month, bgneal@65: test_ipn=settings.DONATIONS_DEBUG).select_related('user') bgneal@34: bgneal@35: gross = decimal.Decimal() bgneal@35: net = decimal.Decimal() bgneal@35: donations = [] bgneal@34: for donation in qs: bgneal@35: gross += donation.mc_gross bgneal@35: net += donation.mc_gross - donation.mc_fee bgneal@35: donations.append(donation) bgneal@34: bgneal@35: return gross, net, donations bgneal@34: bgneal@34: bgneal@33: class Donation(models.Model): bgneal@33: """Model to represent a donation to the website.""" bgneal@33: bgneal@33: user = models.ForeignKey(auth.models.User, null=True, blank=True) bgneal@33: is_anonymous = models.BooleanField() bgneal@33: test_ipn = models.BooleanField(default=False, verbose_name="Test IPN") bgneal@33: txn_id = models.CharField(max_length=20, verbose_name="Txn ID") bgneal@33: txn_type = models.CharField(max_length=64) bgneal@33: first_name = models.CharField(max_length=64, blank=True) bgneal@33: last_name = models.CharField(max_length=64, blank=True) bgneal@33: payer_email = models.EmailField(max_length=127, blank=True) bgneal@33: payer_id = models.CharField(max_length=13, blank=True, verbose_name="Payer ID") bgneal@33: mc_fee = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Fee") bgneal@33: mc_gross = models.DecimalField(max_digits=8, decimal_places=2, verbose_name="Gross") bgneal@33: memo = models.TextField(blank=True) bgneal@33: payer_status = models.CharField(max_length=10, blank=True) bgneal@33: payment_date = models.DateTimeField() bgneal@33: bgneal@34: objects = DonationManager() bgneal@34: bgneal@33: class Meta: bgneal@33: ordering = ('-payment_date', ) bgneal@33: bgneal@33: def __unicode__(self): bgneal@33: if self.user: bgneal@33: return u'%s from %s' % (self.mc_gross, self.user.username) bgneal@33: return u'%s from %s %s' % (self.mc_gross, self.first_name, self.last_name) bgneal@33: bgneal@34: def donor(self): bgneal@34: """Returns the donor name for the donation.""" bgneal@34: if self.is_anonymous: bgneal@35: return settings.DONATIONS_ANON_NAME bgneal@34: if self.user is not None: bgneal@34: return self.user.username bgneal@34: if self.first_name or self.last_name: bgneal@34: name = u'%s %s' % (self.first_name, self.last_name) bgneal@34: return name.strip() bgneal@35: return settings.DONATIONS_ANON_NAME bgneal@34: