view donations/management/commands/donations_report.py @ 693:ad69236e8501

For issue #52, update many 3rd party Javascript libraries. Updated to jquery 1.10.2, jquery ui 1.10.3. This broke a lot of stuff. - Found a newer version of the jquery cycle all plugin (3.0.3). - Updated JPlayer to 2.4.0. - Updated to MarkItUp 1.1.14. This also required me to add multiline attributes set to true on various buttons in the markdown set. - As per a stackoverflow post, added some code to get multiline titles in a jQuery UI dialog. They removed that functionality but allow you to put it back. Tweaked the MarkItUp preview CSS to show blockquotes in italic. Did not update TinyMCE at this time. I'm not using the JQuery version and this version appears to work ok for now. What I should do is make a repo for MarkItUp and do a vendor branch thing so I don't have to futz around diffing directories to figure out if I'll lose changes when I update.
author Brian Neal <bgneal@gmail.com>
date Wed, 04 Sep 2013 19:55:20 -0500
parents 161b56849114
children
line wrap: on
line source
"""donations_report.py

A management command to display donation statistics for a given year.

"""
import datetime
import decimal

from django.core.management.base import LabelCommand, CommandError
from django.db.models import Sum

from donations.models import Donation

ROW_FMT = '%10s %7s %7s'


class Command(LabelCommand):
    help = "Display donation statistics for a given year"
    args = "<year>"

    def handle_label(self, year, **kwargs):
        """Display donation statistics for a given year"""

        try:
            year = int(year)
        except ValueError:
            raise CommandError("invalid year")

        today = datetime.date.today()
        if year < 2011 or year > today.year:
            raise CommandError("invalid year")

        max_month = 12 if year != today.year else today.month

        self.stdout.write(ROW_FMT % ('Month', 'Gross', 'Net'))
        sep = ROW_FMT % ('-' * 10, '-' * 7, '-' * 7)
        self.stdout.write(sep)

        total_gross = decimal.Decimal()
        total_net = decimal.Decimal()

        for month in range(1, max_month + 1):
            r = Donation.objects.filter(
                    payment_date__year=year,
                    payment_date__month=month).aggregate(
                            Sum('mc_gross'), Sum('mc_fee'))

            gross, fee = r['mc_gross__sum'], r['mc_fee__sum']

            gross = gross if gross else decimal.Decimal()
            fee = fee if fee else decimal.Decimal()

            net = gross - fee

            d = datetime.date(year=year, month=month, day=1)

            self.stdout.write(ROW_FMT % (d.strftime('%b %Y'), gross, net))

            total_gross += gross
            total_net += net

        self.stdout.write(sep)
        self.stdout.write(ROW_FMT % ('Total:', total_gross, total_net))