python models: Add Date.strptime constructor.

This commit is contained in:
Brett Smith 2017-02-17 11:41:39 -05:00
parent 1508660842
commit e2eab732ac
2 changed files with 8 additions and 3 deletions

View file

@ -4,6 +4,7 @@ import argparse
import collections import collections
import csv import csv
import datetime import datetime
import functools
import os import os
import sys import sys
@ -15,14 +16,12 @@ from supporters.models import Date, Payment, Supporter
MONTH_FMT = '%Y-%m' MONTH_FMT = '%Y-%m'
def month_date(arg_s):
return Date.from_pydate(datetime.datetime.strptime(arg_s, MONTH_FMT))
def parse_arguments(arglist): def parse_arguments(arglist):
parser = argparse.ArgumentParser( parser = argparse.ArgumentParser(
prog='status_report', prog='status_report',
description="Print a CSV report counting Supporters over time", description="Print a CSV report counting Supporters over time",
) )
month_date = functools.partial(Date.strptime, fmt=MONTH_FMT)
parser.add_argument( parser.add_argument(
'--start-month', type=month_date, metavar='YYYY-MM', '--start-month', type=month_date, metavar='YYYY-MM',
default=Payment.objects.order_by('date').first().date, default=Payment.objects.order_by('date').first().date,

View file

@ -2,6 +2,7 @@
import datetime import datetime
import operator import operator
import time
from django.db import models from django.db import models
@ -25,6 +26,11 @@ class Date(datetime.date):
def from_pydate(cls, date): def from_pydate(cls, date):
return cls(date.year, date.month, date.day) 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): def adjust_month(self, delta, day=None):
if day is None: if day is None:
day = self.day day = self.day