annotate gpp/donations/models.py @ 348:d1b11096595b

Fix #168; when nailing a spammer, clear their profile text fields. Guard against topics and forums that don't exist when deleting posts in the signal handler. Make the forum stats template tag only display the latest active users.
author Brian Neal <bgneal@gmail.com>
date Wed, 02 Mar 2011 02:18:28 +0000
parents 75ea1a8be7f2
children 2177a71b680c
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@65 33 test_ipn=settings.DONATIONS_DEBUG).select_related('user')
bgneal@34 34
bgneal@35 35 gross = decimal.Decimal()
bgneal@35 36 net = decimal.Decimal()
bgneal@35 37 donations = []
bgneal@34 38 for donation in qs:
bgneal@35 39 gross += donation.mc_gross
bgneal@35 40 net += donation.mc_gross - donation.mc_fee
bgneal@35 41 donations.append(donation)
bgneal@34 42
bgneal@35 43 return gross, net, donations
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@259 49 user = models.ForeignKey(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@34 74 def donor(self):
bgneal@34 75 """Returns the donor name for the donation."""
bgneal@34 76 if self.is_anonymous:
bgneal@35 77 return settings.DONATIONS_ANON_NAME
bgneal@34 78 if self.user is not None:
bgneal@34 79 return self.user.username
bgneal@34 80 if self.first_name or self.last_name:
bgneal@34 81 name = u'%s %s' % (self.first_name, self.last_name)
bgneal@34 82 return name.strip()
bgneal@35 83 return settings.DONATIONS_ANON_NAME
bgneal@34 84