2017-01-24 22:51:59 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import argparse
|
|
|
|
import collections
|
|
|
|
import csv
|
|
|
|
import datetime
|
2017-02-17 16:41:39 +00:00
|
|
|
import functools
|
2017-01-24 22:51:59 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
import django
|
|
|
|
|
|
|
|
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'supporters.settings')
|
|
|
|
django.setup()
|
|
|
|
from supporters.models import Date, Payment, Supporter
|
|
|
|
|
|
|
|
MONTH_FMT = '%Y-%m'
|
|
|
|
|
|
|
|
def parse_arguments(arglist):
|
2017-01-25 16:10:27 +00:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
prog='status_report',
|
|
|
|
description="Print a CSV report counting Supporters over time",
|
|
|
|
)
|
2017-02-17 16:41:39 +00:00
|
|
|
month_date = functools.partial(Date.strptime, fmt=MONTH_FMT)
|
2017-01-24 22:51:59 +00:00
|
|
|
parser.add_argument(
|
2017-01-25 16:10:27 +00:00
|
|
|
'--start-month', type=month_date, metavar='YYYY-MM',
|
2017-01-24 22:51:59 +00:00
|
|
|
default=Payment.objects.order_by('date').first().date,
|
2017-01-25 16:10:27 +00:00
|
|
|
help="First month in report")
|
2017-01-24 22:51:59 +00:00
|
|
|
parser.add_argument(
|
2017-01-25 16:10:27 +00:00
|
|
|
'--end-month', type=month_date, metavar='YYYY-MM',
|
|
|
|
default=Date.today(),
|
|
|
|
help="Last month in report")
|
2017-01-24 22:51:59 +00:00
|
|
|
args = parser.parse_args(arglist)
|
|
|
|
if args.end_month < args.start_month:
|
|
|
|
parser.error("End month predates start month")
|
|
|
|
return args
|
|
|
|
|
|
|
|
def count_tuple(counter):
|
|
|
|
return (
|
|
|
|
counter[Supporter.STATUS_NEW],
|
|
|
|
counter[Supporter.STATUS_NEW] + counter[Supporter.STATUS_ACTIVE],
|
|
|
|
counter[Supporter.STATUS_LAPSED],
|
|
|
|
counter[Supporter.STATUS_LOST],
|
|
|
|
)
|
|
|
|
|
|
|
|
def report_month(month):
|
|
|
|
annuals = collections.Counter(Supporter(name).status(month)
|
|
|
|
for name in Supporter.iter_entities(['Annual']))
|
|
|
|
monthlies = collections.Counter(Supporter(name).status(month)
|
|
|
|
for name in Supporter.iter_entities(['Monthly']))
|
|
|
|
return ((month.strftime(MONTH_FMT),)
|
|
|
|
+ count_tuple(annuals)
|
|
|
|
+ count_tuple(monthlies)
|
|
|
|
+ count_tuple(annuals + monthlies))
|
|
|
|
|
|
|
|
def main(arglist):
|
|
|
|
args = parse_arguments(arglist)
|
|
|
|
out_csv = csv.writer(sys.stdout)
|
|
|
|
out_csv.writerow((
|
|
|
|
'Month',
|
|
|
|
'Annual New', 'Annual Active', 'Annual Lapsed', 'Annual Lost',
|
|
|
|
'Monthly New', 'Monthly Active', 'Monthly Lapsed', 'Monthly Lost',
|
|
|
|
'Total New', 'Total Active', 'Total Lapsed', 'Total Lost',
|
|
|
|
))
|
|
|
|
month = Date.from_pydate(args.start_month)
|
|
|
|
while month <= args.end_month:
|
|
|
|
out_csv.writerow(report_month(month))
|
|
|
|
month = month.round_month_up()
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main(None)
|