bgneal@761: """top_donors.py bgneal@761: bgneal@761: A management command to display top donor data. bgneal@761: bgneal@761: """ bgneal@761: from optparse import make_option bgneal@761: bgneal@761: from django.core.management.base import BaseCommand bgneal@761: bgneal@761: from donations.models import Donation bgneal@761: bgneal@761: bgneal@761: ROW_FMT = '{n:<5} {name:<32} ${amount}\n' bgneal@761: bgneal@761: bgneal@761: class Command(BaseCommand): bgneal@761: help = "Display the top N donors and stats" bgneal@761: args = "" bgneal@761: option_list = BaseCommand.option_list + ( bgneal@761: make_option('-n', '--number', bgneal@761: action='store', bgneal@761: type='int', bgneal@761: default=10, bgneal@761: help='number of donors to display'), bgneal@761: ) bgneal@761: bgneal@761: def handle(self, *args, **kwargs): bgneal@761: """Display the top n donors and their amounts.""" bgneal@761: bgneal@761: n = kwargs['number'] bgneal@761: bgneal@761: donors = Donation.objects.top_donors(n) bgneal@761: for i, donor in enumerate(donors, start=1): bgneal@761: self.stdout.write(ROW_FMT.format(n=i, name=donor.username, bgneal@761: amount=donor.total_donations))