Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make date fields be datetime objects #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
make date fields be datetime objects in the eastern timezone
  • Loading branch information
Evan Sonderegger committed Jun 15, 2018
commit f4d20ede6d1591399ec277f37f43249526508f6e
7 changes: 6 additions & 1 deletion pyopenfec/aggregates.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,13 @@ def __init__(self, **kwargs):
self.transfers_to_affiliated_committee = None
self.transfers_to_other_authorized_committee = None

date_fields = {
'coverage_start_date': '%Y-%m-%dT%H:%M:%S+00:00',
'coverage_end_date': '%Y-%m-%dT%H:%M:%S+00:00',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

def __unicode__(self):
return unicode("{cid} totals ({c} cycle, {csd}-{ced})".format(
Expand Down
18 changes: 16 additions & 2 deletions pyopenfec/candidate.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ def __init__(self, **kwargs):
self._history = None
self._committees = None

date_fields = {
'first_file_date': '%Y-%m-%d',
'last_f2_date': '%Y-%m-%d',
'last_file_date': '%Y-%m-%d',
'load_date': '%Y-%m-%dT%H:%M:%S',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

def __unicode__(self):
return unicode("{name} {id}".format(name=self.name,
Expand Down Expand Up @@ -88,8 +95,15 @@ def __init__(self, **kwargs):
self.state = None
self.two_year_period = None

date_fields = {
'first_file_date': '%Y-%m-%d',
'last_f2_date': '%Y-%m-%d',
'last_file_date': '%Y-%m-%d',
'load_date': '%Y-%m-%dT%H:%M:%S',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

def __unicode__(self):
return unicode("{name} [{cand_id}] ({period})".format(
Expand Down
8 changes: 7 additions & 1 deletion pyopenfec/committee.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,14 @@ def __init__(self, **kwargs):
self._history = None
self._totals = None

date_fields = {
'first_file_date': '%Y-%m-%d',
'last_f1_date': '%Y-%m-%d',
'last_file_date': '%Y-%m-%d',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

def __unicode__(self):
return unicode("{name} {id}".format(name=self.name,
Expand Down
9 changes: 8 additions & 1 deletion pyopenfec/filing.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,15 @@ def __init__(self, **kwargs):
self.treasurer_name = None
self.update_date = None

date_fields = {
'coverage_end_date': '%Y-%m-%dT%H:%M:%S',
'coverage_start_date': '%Y-%m-%dT%H:%M:%S',
'receipt_date': '%Y-%m-%dT%H:%M:%S',
'update_date': '%Y-%m-%dT%H:%M:%S',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

def __unicode__(self):
return unicode("{cid}'s #{fn} Form {ft} ({rtf})".format(fn=self.file_number,
Expand Down
8 changes: 7 additions & 1 deletion pyopenfec/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,14 @@ def __init__(self, **kwargs):
self.transfers_to_other_authorized_committee_period = None
self.transfers_to_other_authorized_committee_ytd = None

date_fields = {
'coverage_end_date': '%Y-%m-%dT%H:%M:%S+00:00',
'coverage_start_date': '%Y-%m-%dT%H:%M:%S+00:00',
'receipt_date': '%Y-%m-%dT%H:%M:%S',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

def __unicode__(self):
return unicode("{cid} {rtf} ({c} cycle, {csd}-{ced})".format(
Expand Down
15 changes: 13 additions & 2 deletions pyopenfec/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,14 @@ def __init__(self, **kwargs):
self.transaction_id = None
self.update_date = None

date_fields = {
'contribution_receipt_date': '%Y-%m-%dT%H:%M:%S',
'load_date': '%Y-%m-%dT%H:%M:%S.%f+00:00',
'timestamp': '%Y-%m-%dT%H:%M:%S.%f+00:00',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

@classmethod
def fetch(cls, **kwargs):
Expand Down Expand Up @@ -133,8 +139,13 @@ def __init__(self, **kwargs):
self.transaction_id = None
self.update_date = None

date_fields = {
'disbursement_date': '%Y-%m-%dT%H:%M:%S',
'load_date': '%Y-%m-%dT%H:%M:%S.%f+00:00',
}

for k, v in kwargs.items():
setattr(self, k, v)
utils.set_instance_attr(self, k, v, date_fields)

@classmethod
def fetch(cls, **kwargs):
Expand Down
12 changes: 12 additions & 0 deletions pyopenfec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import os
import time
import logging
from datetime import datetime

import requests
import six
from pytz import timezone

if six.PY2:
from exceptions import Exception, NotImplementedError, TypeError
Expand All @@ -14,6 +16,7 @@
BASE_URL = 'https://api.open.fec.gov'
VERSION = '/v1'

eastern = timezone('US/Eastern')

class PyOpenFecException(Exception):
"""
Expand Down Expand Up @@ -182,3 +185,12 @@ def inner(*args, **kwargs):
except TypeError:
return []
return inner


def set_instance_attr(instance, k, v, date_fields):
if k in date_fields and v is not None:
parsed_date = datetime.strptime(v, date_fields[k])
tz_aware = eastern.localize(parsed_date)
setattr(instance, k, tz_aware)
return
setattr(instance, k, v)
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nose==1.3.7
requests==2.7.0
six==1.9.0
pytz==2018.4
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
from setuptools import setup, find_packages

setup(name="pyopenfec",
version="0.2.0",
version="0.2.1",
description="OpenFEC API client",
license="MIT",
install_requires=["requests", "six"],
install_requires=["pytz", "requests", "six"],
author="Jeremy Bowers",
author_email="jeremyjbowers@gmail.com",
url="http://github.com/jeremyjbowers/pyopenfec",
Expand Down
86 changes: 86 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import unittest
from pyopenfec import Candidate, Committee
from datetime import datetime


class CandidateTest(unittest.TestCase):
def setUp(self):
self.candidate = None
candidates = Candidate.fetch(candidate_id='H8CA05035')
for candidate in candidates:
self.candidate = candidate

def test_candidate_dates(self):
self.assertIsInstance(self.candidate.first_file_date, datetime)
self.assertIsInstance(self.candidate.last_f2_date, datetime)
self.assertIsInstance(self.candidate.last_file_date, datetime)
self.assertIsInstance(self.candidate.load_date, datetime)

def test_history_dates(self):
history = self.candidate.history[2018]
self.assertIsInstance(history.first_file_date, datetime)
self.assertIsInstance(history.last_f2_date, datetime)
self.assertIsInstance(history.last_file_date, datetime)
self.assertIsInstance(history.load_date, datetime)


class CommitteeTest(unittest.TestCase):
def setUp(self):
self.committee = None
committees = Committee.fetch(designation='P', candidate_id='H8CA05035')
for committee in committees:
self.committee = committee

def test_committee_dates(self):
self.assertIsInstance(self.committee.first_file_date, datetime)
self.assertIsInstance(self.committee.last_f1_date, datetime)
self.assertIsInstance(self.committee.last_file_date, datetime)

def test_committee_totals_dates(self):
totals = self.committee.totals[2018]
self.assertIsInstance(totals.coverage_end_date, datetime)
self.assertIsInstance(totals.coverage_start_date, datetime)

def test_filing_dates(self):
example_filing = None
for filing in self.committee.select_filings(file_number=1186352):
example_filing = filing
self.assertIsInstance(example_filing.coverage_end_date, datetime)
self.assertIsInstance(example_filing.coverage_start_date, datetime)
self.assertIsInstance(example_filing.receipt_date, datetime)
self.assertIsInstance(example_filing.update_date, datetime)

def test_report_dates(self):
example_report = None
for report in self.committee.select_reports(file_number=1234452):
example_report = report
self.assertIsInstance(example_report.coverage_end_date, datetime)
self.assertIsInstance(example_report.coverage_start_date, datetime)
self.assertIsInstance(example_report.receipt_date, datetime)

def test_schedule_a_dates(self):
example = None
donations = self.committee.select_receipts(
contributor_name='SANDBERG, SHERYL',
two_year_transaction_period=2018,
)
for donation in donations:
example = donation
self.assertIsInstance(example.contribution_receipt_date, datetime)
self.assertIsInstance(example.load_date, datetime)
self.assertIsInstance(example.timestamp, datetime)

def test_schedule_b_dates(self):
example = None
donations = self.committee.select_disbursements(
recipient_name='GOAT HILL PIZZA',
two_year_transaction_period=2018,
)
for donation in donations:
example = donation
self.assertIsInstance(example.disbursement_date, datetime)
self.assertIsInstance(example.load_date, datetime)


if __name__ == '__main__':
unittest.main()