annotate donations/management/commands/top_donors.py @ 943:cf9918328c64

Haystack tweaks for Django 1.7.7. I had to upgrade to Haystack 2.3.1 to get it to work with Django 1.7.7. I also had to update the Xapian backend. But I ran into problems. On my laptop anyway (Ubuntu 14.0.4), xapian gets mad when search terms are greater than 245 chars (or something) when indexing. So I created a custom field that would simply omit terms greater than 64 chars and used this field everywhere I previously used a CharField. Secondly, the custom search form was broken now. Something changed in the Xapian backend and exact searches stopped working. Fortunately the auto_query (which I was using originally and broke during an upgrade) started working again. So I cut the search form back over to doing an auto_query. I kept the form the same (3 fields) because I didn't want to change the form and I think it's better that way.
author Brian Neal <bgneal@gmail.com>
date Wed, 13 May 2015 20:25:07 -0500
parents 1ddd72f48d73
children
rev   line source
bgneal@761 1 """top_donors.py
bgneal@761 2
bgneal@761 3 A management command to display top donor data.
bgneal@761 4
bgneal@761 5 """
bgneal@761 6 from optparse import make_option
bgneal@761 7
bgneal@761 8 from django.core.management.base import BaseCommand
bgneal@761 9
bgneal@761 10 from donations.models import Donation
bgneal@761 11
bgneal@761 12
bgneal@761 13 ROW_FMT = '{n:<5} {name:<32} ${amount}\n'
bgneal@761 14
bgneal@761 15
bgneal@761 16 class Command(BaseCommand):
bgneal@761 17 help = "Display the top N donors and stats"
bgneal@761 18 args = "<n>"
bgneal@761 19 option_list = BaseCommand.option_list + (
bgneal@761 20 make_option('-n', '--number',
bgneal@761 21 action='store',
bgneal@761 22 type='int',
bgneal@761 23 default=10,
bgneal@761 24 help='number of donors to display'),
bgneal@761 25 )
bgneal@761 26
bgneal@761 27 def handle(self, *args, **kwargs):
bgneal@761 28 """Display the top n donors and their amounts."""
bgneal@761 29
bgneal@761 30 n = kwargs['number']
bgneal@761 31
bgneal@761 32 donors = Donation.objects.top_donors(n)
bgneal@761 33 for i, donor in enumerate(donors, start=1):
bgneal@761 34 self.stdout.write(ROW_FMT.format(n=i, name=donor.username,
bgneal@761 35 amount=donor.total_donations))