diff --git a/python/status_report.py b/python/status_report.py index 44675b6..4ee3fb2 100755 --- a/python/status_report.py +++ b/python/status_report.py @@ -4,6 +4,7 @@ import argparse import collections import csv import datetime +import functools import os import sys @@ -15,14 +16,12 @@ from supporters.models import Date, Payment, Supporter MONTH_FMT = '%Y-%m' -def month_date(arg_s): - return Date.from_pydate(datetime.datetime.strptime(arg_s, MONTH_FMT)) - def parse_arguments(arglist): parser = argparse.ArgumentParser( prog='status_report', description="Print a CSV report counting Supporters over time", ) + month_date = functools.partial(Date.strptime, fmt=MONTH_FMT) parser.add_argument( '--start-month', type=month_date, metavar='YYYY-MM', default=Payment.objects.order_by('date').first().date, diff --git a/python/supporters/models.py b/python/supporters/models.py index 6cc2f3d..cc9e21d 100644 --- a/python/supporters/models.py +++ b/python/supporters/models.py @@ -2,6 +2,7 @@ import datetime import operator +import time from django.db import models @@ -25,6 +26,11 @@ class Date(datetime.date): def from_pydate(cls, date): return cls(date.year, date.month, date.day) + @classmethod + def strptime(cls, s, fmt): + time_tuple = time.strptime(s, fmt) + return cls(*time_tuple[:3]) + def adjust_month(self, delta, day=None): if day is None: day = self.day @@ -106,7 +112,12 @@ class Supporter: return expose_wrapper def _supporter_type(self, payments): - return payments.last().program.rsplit(':', 1)[-1] + try: + program = payments.filter(program__isnull=False).reverse()[0].program + except IndexError: + return None + else: + return program.rsplit(':', 1)[-1] supporter_type = _expose(_supporter_type) def _calculate_lapse_date(self, last_payment_date, supporter_type):