Skip to content

Commit

Permalink
#87
Browse files Browse the repository at this point in the history
  • Loading branch information
gordonje committed Jul 14, 2017
1 parent 307e685 commit 318328b
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
20 changes: 19 additions & 1 deletion calaccess_processed/management/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,15 @@ def get_or_create_candidacy(self, contest_obj, sort_name, registration_status, f
candidacy.registration_status = registration_status
candidacy.save()

# make sure Person name is same as most recent candidate_name
person.refresh_from_db()
latest_candidate_name = person.candidacies.latest(
'contest__election__date',
).candidate_name
if person.name != latest_candidate_name:
person.name = latest_candidate_name
person.save()

return (candidacy, candidacy_created)

def link_form501_to_candidacy(self, form501_id, candidacy_obj):
Expand Down Expand Up @@ -692,7 +701,7 @@ def merge_persons(self, persons):
else:
cand_to_keep = cands.latest('filed_date')

# loop over all the other candidacy in the group
# loop over all the other candidacies in the group
for cand_to_discard in cands.exclude(id=cand_to_keep.id).all():
# assuming the only thing in extras is form501_filing_ids
if 'form501_filing_ids' in cand_to_discard.extras:
Expand Down Expand Up @@ -745,4 +754,13 @@ def merge_persons(self, persons):
cand_to_discard.delete()

keep.refresh_from_db()

# make sure Person name is same as most recent candidate_name
latest_candidate_name = keep.candidacies.latest(
'contest__election__date',
).candidate_name
if keep.name != latest_candidate_name:
keep.name = latest_candidate_name
keep.save()

return keep
17 changes: 17 additions & 0 deletions calaccess_processed/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from calaccess_processed.models import ProcessedDataVersion
from calaccess_scraped.models import Candidate as ScrapedCandidate
from calaccess_scraped.models import Proposition as ScrapedProposition
from opencivicdata.core.models import Person
from opencivicdata.elections.models import (
BallotMeasureContest,
Candidacy,
Expand Down Expand Up @@ -212,6 +213,22 @@ def test_commands(self):
),
)

# For each Person...
for person in Person.objects.all():
# Confirm name is same as most recent candidate_name
latest_candidate_name = person.candidacies.latest(
'contest__election__date'
).candidate_name

self.assertEqual(
person.name,
latest_candidate_name,
msg='Person.name "{0}" doesn\'t match latest candidate_name "{1}!'.format(
person.name,
latest_candidate_name,
)
)

processed_version = ProcessedDataVersion.objects.latest('process_start_datetime')

# Confirm that the version finished
Expand Down

0 comments on commit 318328b

Please sign in to comment.