annotate donations/management/commands/top_donors.py @ 887:9a15f7c27526

Actually save model object upon change. This commit was tested on the comments model. Additional logging added. Added check for Markdown image references. Added TODOs after observing behavior on comments.
author Brian Neal <bgneal@gmail.com>
date Tue, 03 Feb 2015 21:09:44 -0600
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))