Skip to content

Commit

Permalink
better linkages
Browse files Browse the repository at this point in the history
  • Loading branch information
fgregg committed Jan 27, 2024
1 parent 255e127 commit d518fab
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 73 deletions.
80 changes: 37 additions & 43 deletions camp_fin/management/commands/import_api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ class Command(BaseCommand):
Data will be retrieved from S3 unless a local CSV is specified as --file
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

try:
self._next_entity_id = (
models.Entity.objects.aggregate(max_id=Max("user_id"))["max_id"] + 1
)
except TypeError:
self._next_entity_id = 1

def add_arguments(self, parser):
parser.add_argument(
"--transaction-type",
Expand Down Expand Up @@ -120,7 +110,6 @@ def import_contributions(self, f):

models.LoanTransaction.objects.filter(filing=filing).delete()
models.SpecialEvent.objects.filter(filing=filing).delete()

models.Transaction.objects.filter(filing=filing).exclude(
transaction_type__description="Monetary Expenditure"
).delete()
Expand Down Expand Up @@ -243,7 +232,6 @@ def make_contributor(self, record):
)

entity = models.Entity.objects.create(
user_id=self._next_entity_id,
entity_type=entity_type,
)

Expand All @@ -255,8 +243,6 @@ def make_contributor(self, record):
entity=entity,
)

self._next_entity_id += 1

return contact

def make_pac(self, record, entity_type=None):
Expand Down Expand Up @@ -299,15 +285,7 @@ def make_filing(self, record):
# Create PAC associated with candidate
self.make_pac(record, entity_type="Political Committee")

candidate_type, _ = models.EntityType.objects.get_or_create(
description="Candidate"
)

person, _ = models.Entity.objects.get_or_create(
user_id=record["OrgID"], entity_type=candidate_type
)

candidate = self._get_or_create_candidate(record, person)
candidate = self._get_or_create_candidate(record)

transaction_date = (
record["Transaction Date"]
Expand All @@ -318,9 +296,9 @@ def make_filing(self, record):
transaction_year = self.parse_date(transaction_date).year

try:
campaign = candidate.campaign_set.filter(
campaign = candidate.campaign_set.get(
election_season__year=transaction_year
).first()
)
except models.Campaign.DoesNotExist:
self.stderr.write(
f"Could not find campaign for {candidate} in {transaction_year}"
Expand All @@ -331,7 +309,7 @@ def make_filing(self, record):
campaign.committee_name = record["Committee Name"]
campaign.save()

filing_kwargs = {"entity": person, "campaign": campaign}
filing_kwargs = {"entity": candidate.entity, "campaign": campaign}

else:
pac = self.make_pac(record)
Expand Down Expand Up @@ -509,7 +487,7 @@ def total_filings(self, year):

self.stdout.write(f"Totalled {filing}")

def _get_or_create_candidate(self, record, entity):
def _get_or_create_candidate(self, record):

candidate = None

Expand All @@ -532,22 +510,38 @@ def _get_or_create_candidate(self, record, entity):
),
).strip()

candidate, _ = models.Candidate.objects.get_or_create(
first_name=record["Candidate First Name"] or None,
middle_name=record["Candidate Middle Name"] or None,
last_name=record["Candidate Last Name"] or None,
suffix=record["Candidate Suffix"] or None,
full_name=full_name,
entity=entity,
slug=slugify(
" ".join(
[
record["Candidate First Name"],
record["Candidate Last Name"],
]
)
),
)
try:
candidate = models.Candidate.objects.get(full_name=full_name)
except models.Candidate.DoesNotExist:
candidate_type, _ = models.EntityType.objects.get_or_create(
description="Candidate"
)

person, _ = models.Entity.objects.get_or_create(
user_id=record["OrgID"], entity_type=candidate_type
)

candidate = models.Candidate.objects.create(
first_name=record["Candidate First Name"] or None,
middle_name=record["Candidate Middle Name"] or None,
last_name=record["Candidate Last Name"] or None,
suffix=record["Candidate Suffix"] or None,
full_name=full_name,
slug=slugify(
" ".join(
[
record["Candidate First Name"],
record["Candidate Last Name"],
]
)
),
entity=person,
)

else:
if candidate.entity.user_id != record["OrgID"]:
candidate.entity.user_id = record["OrgID"]
candidate.entity.save()

else:
# Sometimes, a record says it is associated with a candidate,
Expand Down
44 changes: 14 additions & 30 deletions camp_fin/management/commands/import_office_api_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,6 @@ class Command(BaseCommand):
Data will be retrieved from S3 unless a local CSV is specified as --file
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

try:
self._next_entity_id = (
models.Entity.objects.aggregate(max_id=Max("user_id"))["max_id"] + 1
)
except TypeError:
self._next_entity_id = 1

def add_arguments(self, parser):
parser.add_argument(
"--file",
Expand Down Expand Up @@ -71,6 +61,8 @@ def handle(self, *args, **options):
candidates_skipped = 0
campaigns = []

models.Campaign.objects.filter(election_season__year__gte=2021).delete()

for record in tqdm(reader):
name_parts = record["CandidateName"].split(",")

Expand Down Expand Up @@ -103,21 +95,18 @@ def handle(self, *args, **options):
).strip()

try:
candidate = models.Candidate.objects.filter(
full_name=full_name
).first()
candidate = models.Candidate.objects.get(full_name=full_name)
candidates_linked += 1

except models.Candidate.DoesNotExist:
candidate_type = models.EntityType.objects.get(
candidate_type, _ = models.EntityType.objects.get_or_create(
description="Candidate"
)
person = models.Entity.objects.create(
user_id=self._next_entity_id,
entity_type=candidate_type,
)

candidate = models.Candidate(
candidate = models.Candidate.objects.create(
first_name=candidate_name.get("GivenName"),
middle_name=(
candidate_name.get("MiddleName")
Expand All @@ -139,9 +128,6 @@ def handle(self, *args, **options):
),
entity=person,
)
candidate.save()

self._next_entity_id += 1

candidates_created += 1

Expand Down Expand Up @@ -178,19 +164,17 @@ def handle(self, *args, **options):
else:
county = None

campaigns.append(
models.Campaign(
election_season=election_season,
candidate=candidate,
office=office,
district=district,
county=county,
political_party=political_party,
)
_, created = models.Campaign.objects.get_or_create(
election_season=election_season,
candidate=candidate,
office=office,
district=district,
county=county,
political_party=political_party,
)

models.Campaign.objects.filter(election_season__year__gte=2021).delete()
models.Campaign.objects.bulk_create(campaigns)
if created:
candidates_created += 1

finally:
f.close()
Expand Down

0 comments on commit d518fab

Please sign in to comment.