Skip to content

Commit

Permalink
Fix contributions, maybe
Browse files Browse the repository at this point in the history
  • Loading branch information
davepeck committed Nov 23, 2023
1 parent d3b0a65 commit a2c28d0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 7 deletions.
34 changes: 30 additions & 4 deletions fec.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
#!/usr/bin/env python3
# ruff: noqa: E501

import json

import click

from server.data.fec.contributions import ContributionsManager
from server.data.fec.contributions import (
ContributionsManager,
ContributionSummariesManager,
FuzzyIdentifier,
)
from server.data.manager import DataManager
from server.data.names.nicknames import MessyNicknamesManager
from server.data.names.nicknames import MessyNicknamesManager, NicknamesManager


@click.group()
Expand Down Expand Up @@ -59,9 +65,29 @@ def summarize(data: str | None = None):


@contributions.command()
def search():
@click.argument("first_name")
@click.argument("last_name")
@click.argument("zip_code")
@click.option(
"--data",
type=click.Path(exists=True),
help="Path to data dir.",
required=False,
default=None,
)
def search(first_name: str, last_name: str, zip_code: str, data: str | None = None):
"""Search summarized FEC contributions data."""
pass
data_manager = DataManager(data) if data is not None else DataManager.default()
nicknames_manager = NicknamesManager.from_data_manager(data_manager)
fuzzy_id = FuzzyIdentifier(
last_name, first_name, zip_code, get_nickname_index=nicknames_manager
).fuzzy_id
summaries_manager = ContributionSummariesManager.from_data_manager(data_manager)
summary = summaries_manager.get_summary(fuzzy_id)
if summary is None:
print("No matching summary.")
else:
print(json.dumps(summary.to_data(), indent=2))


if __name__ == "__main__":
Expand Down
9 changes: 6 additions & 3 deletions server/data/fec/contributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def _first_nickname(self) -> str | None:

def _make_fuzzy_id(self) -> str:
"""Make the fuzzy ID."""
return f"{self.last_name}-{self._first_nickname}-{self.zip_code}".upper()
return f"{self.last_name}-{self._first_nickname}-{self.zip_code[:5]}".upper()

def _make_fuzzy_id_if_needed(self) -> None:
if self._fuzzy_id is None:
Expand Down Expand Up @@ -329,7 +329,7 @@ def from_data(cls, value: t.Any) -> "ContributionSummary":
zip_code=v.get_str(data, "zip_code"),
total=v.get_convert_decimal(data, "total"),
by_party={
party: v.validate_convert_decimal(amount)
(None if party == "null" else party): v.validate_convert_decimal(amount)
for party, amount in by_party_data.items()
},
by_committee={
Expand All @@ -345,7 +345,10 @@ def to_data(self) -> dict:
"name": self.name,
"zip_code": self.zip_code,
"total": str(self.total),
"by_party": {party: str(amount) for party, amount in self.by_party.items()},
"by_party": {
party if party else "null": str(amount)
for party, amount in self.by_party.items()
},
"by_committee": {
committee: str(amount)
for committee, amount in self.by_committee.items()
Expand Down

0 comments on commit a2c28d0

Please sign in to comment.