def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() if value_exists(voter_id) and value_exists( google_civic_election_id) and value_exists( google_civic_district_ocd_id): try: google_civic_ballot_item_on_stage = GoogleCivicBallotItem.objects.get( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except GoogleCivicBallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except GoogleCivicBallotItem.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'success': True if google_civic_ballot_item_id > 0 else False, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'google_civic_ballot_item': google_civic_ballot_item_on_stage, } return results
def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() if ( value_exists(voter_id) and value_exists(google_civic_election_id) and value_exists(google_civic_district_ocd_id) ): try: google_civic_ballot_item_on_stage = GoogleCivicBallotItem.objects.get( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except GoogleCivicBallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True except GoogleCivicBallotItem.DoesNotExist as e: exception_does_not_exist = True results = { "success": True if google_civic_ballot_item_id > 0 else False, "DoesNotExist": exception_does_not_exist, "MultipleObjectsReturned": exception_multiple_object_returned, "google_civic_ballot_item": google_civic_ballot_item_on_stage, } return results
def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() if value_exists(voter_id) and value_exists(google_civic_election_id) and value_exists( google_civic_district_ocd_id): try: google_civic_ballot_item_on_stage = GoogleCivicBallotItem.objects.get( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except GoogleCivicBallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except GoogleCivicBallotItem.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'success': True if google_civic_ballot_item_id > 0 else False, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'google_civic_ballot_item': google_civic_ballot_item_on_stage, } return results
def process_contest_office_from_structured_json( one_contest_office_structured_json, google_civic_election_id, local_ballot_order, save_to_db): voter_id = 1 # TODO Temp logger.debug("General contest_type") office = one_contest_office_structured_json['office'] # The number of candidates that a voter may vote for in this contest. if 'numberVotingFor' in one_contest_office_structured_json: number_voting_for = one_contest_office_structured_json['numberVotingFor'] else: number_voting_for = 1 # The number of candidates that will be elected to office in this contest. if 'numberElected' in one_contest_office_structured_json: number_elected = one_contest_office_structured_json['numberElected'] else: number_elected = 1 results = process_contest_common_fields_from_structured_json(one_contest_office_structured_json) ballot_placement = results['ballot_placement'] # A number specifying the position of this contest # on the voter's ballot. primary_party = results['primary_party'] # If this is a partisan election, the name of the party it is for. district_name = results['district_name'] # The name of the district. district_scope = results['district_scope'] # The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_ocd_id = results['district_ocd_id'] electorate_specifications = results['electorate_specifications'] # A description of any additional # eligibility requirements for voting in this contest. special = results['special'] # "Yes" or "No" depending on whether this a contest being held # outside the normal election cycle. # We want to convert this from an array to three fields for the same table # levels: string, A list of office levels to filter by. Only offices that serve at least one of these levels # will be returned. Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # administrativeArea1 - # administrativeArea2 - # country - # international - # locality - # regional - # special - # subLocality1 - # subLocality2 - # The levels of government of the office for this contest. There may be more than one in cases where a # jurisdiction effectively acts at two different levels of government; for example, the mayor of the # District of Columbia acts at "locality" level, but also effectively at both "administrative-area-2" # and "administrative-area-1". level_structured_json = one_contest_office_structured_json['level'] contest_level = [] for one_level in level_structured_json: contest_level.append(one_level) if 0 in contest_level: contest_level0 = contest_level[0] else: contest_level0 = '' if 1 in contest_level: contest_level1 = contest_level[1] else: contest_level1 = '' if 2 in contest_level: contest_level2 = contest_level[2] else: contest_level2 = '' # roles: string, A list of office roles to filter by. Only offices fulfilling one of these roles will be returned. # Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # deputyHeadOfGovernment - # executiveCouncil - # governmentOfficer - # headOfGovernment - # headOfState - # highestCourtJudge - # judge - # legislatorLowerBody - # legislatorUpperBody - # schoolBoard - # specialPurposeOfficer - roles_structured_json = one_contest_office_structured_json['roles'] # for one_role in roles_structured_json: # Figure out how we are going to use level info candidates = one_contest_office_structured_json['candidates'] internal_contest_office_id = 0 # Set to 0 in case a new one is not created # Note that all of the information saved here is independent of a particular voter if save_to_db: if office and district_name and district_scope and district_ocd_id and google_civic_election_id: try: # Try to find earlier version based on name of the office google_civic_election_id query1 = GoogleCivicContestOffice.objects.all() query1 = query1.filter(google_civic_election_id__exact=google_civic_election_id) query1 = query1.filter(district_scope__exact=district_scope) query1 = query1.filter(office__exact=office) # Was at least one existing entry found based on the above criteria? if len(query1): google_civic_contest_office = query1[0] # If no entries found previously, create a new entry else: google_civic_contest_office = \ GoogleCivicContestOffice.objects.create(office=office, google_civic_election_id=google_civic_election_id, number_voting_for=number_voting_for, number_elected=number_elected, contest_level0=contest_level0, contest_level1=contest_level1, contest_level2=contest_level2, ballot_placement=ballot_placement, primary_party=primary_party, district_name=district_name, district_scope=district_scope, district_ocd_id=district_ocd_id, electorate_specifications=electorate_specifications, special=special, ) # The internal id is needed since there isn't a ContestOffice google identifier internal_contest_office_id = google_civic_contest_office.id except Exception as e: handle_record_not_found_exception(e, logger=logger) if value_exists(voter_id) and value_exists(google_civic_election_id) and value_exists(district_ocd_id): google_civic_ballot_item_manager = GoogleCivicBallotItemManager() google_civic_ballot_item_manager.save_ballot_item_for_voter( voter_id, google_civic_election_id, district_ocd_id, ballot_placement, local_ballot_order) process_candidates_from_structured_json( candidates, google_civic_election_id, internal_contest_office_id, save_to_db) return
def google_civic_get_or_create_contest_office(google_civic_candidate_campaign_entry, election_on_stage): error_result = False ballot_item_on_stage = BallotItem() ballot_item_on_stage_found = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() contest_office_on_stage = ContestOffice() contest_office_created = False google_civic_contest_office_on_stage = GoogleCivicContestOffice() try: # When we import from google, we link a google_civic_contest_office entry (with an internal id) to # google_civic_candidate_campaign_entry # print "Retrieving google_civic_contest_office" google_civic_contest_office_query = GoogleCivicContestOffice.objects.all() google_civic_contest_office_query = google_civic_contest_office_query.filter( id=google_civic_candidate_campaign_entry.google_civic_contest_office_id) if len(google_civic_contest_office_query) == 1: google_civic_contest_office_on_stage = google_civic_contest_office_query[0] else: print "Single google_civic_contest_office NOT found" return { 'error_result': True, } # Try to find earlier version based on the google_civic_election_id identifier # print "Retrieving contest_office" contest_office_query = ContestOffice.objects.all() contest_office_query = contest_office_query.filter( google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id) contest_office_query = contest_office_query.filter( district_name=google_civic_contest_office_on_stage.district_name) contest_office_query = contest_office_query.filter(office_name=google_civic_contest_office_on_stage.office) # TODO: If the 'office' text from Google Civic changes slightly, we would create a new ContestOffice entry # (which would not be correct) Should we make this more robust and error-proof? # Was at least one existing entry found based on the above criteria? if len(contest_office_query): contest_office_on_stage = contest_office_query[0] contest_office_created = False # TODO Update contest_office information here elif len(contest_office_query) > 1: # We have bad data - a duplicate print "We have bad data, a duplicate ContestOffice entry: {office}".format( office=google_civic_contest_office_on_stage.office) return { 'error_result': True, } else: # Create a new ContestOffice entry # print "Creating contest_office" contest_office_on_stage = ContestOffice( office_name=google_civic_contest_office_on_stage.office, election_id=election_on_stage.id, google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id, number_voting_for=google_civic_contest_office_on_stage.number_voting_for, number_elected=google_civic_contest_office_on_stage.number_elected, primary_party=google_civic_contest_office_on_stage.primary_party, district_name=google_civic_contest_office_on_stage.district_name, district_scope=google_civic_contest_office_on_stage.district_scope, district_ocd_id=google_civic_contest_office_on_stage.district_ocd_id, ) contest_office_on_stage.save() contest_office_created = True google_civic_contest_office_on_stage.we_vote_election_id = election_on_stage.id google_civic_contest_office_on_stage.we_vote_contest_office_id = contest_office_on_stage.id google_civic_contest_office_on_stage.save() # Save the ballot_placement # Try to find earlier version based on the google_civic_election_id identifier # print "Retrieving BallotItem" ballot_item_query = BallotItem.objects.all() ballot_item_query = ballot_item_query.filter(voter_id=1) ballot_item_query = ballot_item_query.filter( google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id) ballot_item_query = ballot_item_query.filter(contest_office_id=contest_office_on_stage.id) if len(ballot_item_query) == 1: ballot_item_on_stage = ballot_item_query[0] ballot_item_on_stage_found = True except Exception as e: error_result = True handle_record_not_found_exception(e) try: voter_id = 1 if value_exists(google_civic_contest_office_on_stage.ballot_placement): # If the ballot order is specified by Google, use that. local_ballot_order = google_civic_contest_office_on_stage.ballot_placement else: # Pull the ballot order from the google_civic_ballot_item_manager = GoogleCivicBallotItemManager() local_ballot_order = google_civic_ballot_item_manager.fetch_ballot_order( voter_id, google_civic_contest_office_on_stage.google_civic_election_id, google_civic_contest_office_on_stage.district_ocd_id) if ballot_item_on_stage_found: # Update the values ballot_item_on_stage.election_id = election_on_stage.id # TODO Add all values here else: # print "Creating BallotItem" ballot_item_on_stage = BallotItem( voter_id=voter_id, election_id=election_on_stage.id, google_civic_election_id=google_civic_contest_office_on_stage.google_civic_election_id, contest_office_id=contest_office_on_stage.id, # contest_measure_id: Used for measures/referendum/initiatives ballot_order=local_ballot_order, ballot_item_label=google_civic_contest_office_on_stage.office, ) ballot_item_on_stage.save() except Exception as e: error_result = True handle_record_not_saved_exception(e) results = { 'error_result': error_result, 'contest_office_on_stage': contest_office_on_stage, 'contest_office_created': contest_office_created, } return results
def process_contest_office_from_structured_json( one_contest_office_structured_json, google_civic_election_id, local_ballot_order, save_to_db): voter_id = 1 # TODO Temp # print "General contest_type" office = one_contest_office_structured_json['office'] # The number of candidates that a voter may vote for in this contest. if 'numberVotingFor' in one_contest_office_structured_json: number_voting_for = one_contest_office_structured_json[ 'numberVotingFor'] else: number_voting_for = 1 # The number of candidates that will be elected to office in this contest. if 'numberElected' in one_contest_office_structured_json: number_elected = one_contest_office_structured_json['numberElected'] else: number_elected = 1 results = process_contest_common_fields_from_structured_json( one_contest_office_structured_json) ballot_placement = results[ 'ballot_placement'] # A number specifying the position of this contest # on the voter's ballot. primary_party = results[ 'primary_party'] # If this is a partisan election, the name of the party it is for. district_name = results['district_name'] # The name of the district. district_scope = results[ 'district_scope'] # The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_ocd_id = results['district_ocd_id'] electorate_specifications = results[ 'electorate_specifications'] # A description of any additional # eligibility requirements for voting in this contest. special = results[ 'special'] # "Yes" or "No" depending on whether this a contest being held # outside the normal election cycle. # We want to convert this from an array to three fields for the same table # levels: string, A list of office levels to filter by. Only offices that serve at least one of these levels # will be returned. Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # administrativeArea1 - # administrativeArea2 - # country - # international - # locality - # regional - # special - # subLocality1 - # subLocality2 - # The levels of government of the office for this contest. There may be more than one in cases where a # jurisdiction effectively acts at two different levels of government; for example, the mayor of the # District of Columbia acts at "locality" level, but also effectively at both "administrative-area-2" # and "administrative-area-1". level_structured_json = one_contest_office_structured_json['level'] contest_level = [] for one_level in level_structured_json: contest_level.append(one_level) if 0 in contest_level: contest_level0 = contest_level[0] else: contest_level0 = '' if 1 in contest_level: contest_level1 = contest_level[1] else: contest_level1 = '' if 2 in contest_level: contest_level2 = contest_level[2] else: contest_level2 = '' # roles: string, A list of office roles to filter by. Only offices fulfilling one of these roles will be returned. # Divisions that don't contain a matching office will not be returned. (repeated) # Allowed values # deputyHeadOfGovernment - # executiveCouncil - # governmentOfficer - # headOfGovernment - # headOfState - # highestCourtJudge - # judge - # legislatorLowerBody - # legislatorUpperBody - # schoolBoard - # specialPurposeOfficer - roles_structured_json = one_contest_office_structured_json['roles'] # for one_role in roles_structured_json: # Figure out how we are going to use level info candidates = one_contest_office_structured_json['candidates'] internal_contest_office_id = 0 # Set to 0 in case a new one is not created # Note that all of the information saved here is independent of a particular voter if save_to_db: if office and district_name and district_scope and district_ocd_id and google_civic_election_id: try: # Try to find earlier version based on name of the office google_civic_election_id query1 = GoogleCivicContestOffice.objects.all() query1 = query1.filter( google_civic_election_id__exact=google_civic_election_id) query1 = query1.filter(district_scope__exact=district_scope) query1 = query1.filter(office__exact=office) # Was at least one existing entry found based on the above criteria? if len(query1): google_civic_contest_office = query1[0] # If no entries found previously, create a new entry else: google_civic_contest_office = \ GoogleCivicContestOffice.objects.create(office=office, google_civic_election_id=google_civic_election_id, number_voting_for=number_voting_for, number_elected=number_elected, contest_level0=contest_level0, contest_level1=contest_level1, contest_level2=contest_level2, ballot_placement=ballot_placement, primary_party=primary_party, district_name=district_name, district_scope=district_scope, district_ocd_id=district_ocd_id, electorate_specifications=electorate_specifications, special=special, ) # The internal id is needed since there isn't a ContestOffice google identifier internal_contest_office_id = google_civic_contest_office.id except Exception as e: handle_record_not_found_exception(e) if value_exists(voter_id) and value_exists( google_civic_election_id) and value_exists(district_ocd_id): google_civic_ballot_item_manager = GoogleCivicBallotItemManager() google_civic_ballot_item_manager.save_ballot_item_for_voter( voter_id, google_civic_election_id, district_ocd_id, ballot_placement, local_ballot_order) process_candidates_from_structured_json(candidates, google_civic_election_id, internal_contest_office_id, save_to_db) return
def google_civic_get_or_create_contest_office( google_civic_candidate_campaign_entry, election_on_stage): error_result = False ballot_item_on_stage = BallotItem() ballot_item_on_stage_found = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() contest_office_on_stage = ContestOffice() contest_office_created = False google_civic_contest_office_on_stage = GoogleCivicContestOffice() try: # When we import from google, we link a google_civic_contest_office entry (with an internal id) to # google_civic_candidate_campaign_entry logger.debug("Retrieving google_civic_contest_office") google_civic_contest_office_query = GoogleCivicContestOffice.objects.all( ) google_civic_contest_office_query = google_civic_contest_office_query.filter( id=google_civic_candidate_campaign_entry. google_civic_contest_office_id) if len(google_civic_contest_office_query) == 1: google_civic_contest_office_on_stage = google_civic_contest_office_query[ 0] else: logger.error("Single google_civic_contest_office NOT found") return { 'error_result': True, } # Try to find earlier version based on the google_civic_election_id identifier logger.debug("Retrieving contest_office") contest_office_query = ContestOffice.objects.all() contest_office_query = contest_office_query.filter( google_civic_election_id=google_civic_contest_office_on_stage. google_civic_election_id) contest_office_query = contest_office_query.filter( district_name=google_civic_contest_office_on_stage.district_name) contest_office_query = contest_office_query.filter( office_name=google_civic_contest_office_on_stage.office) # TODO: If the 'office' text from Google Civic changes slightly, we would create a new ContestOffice entry # (which would not be correct) Should we make this more robust and error-proof? # Was at least one existing entry found based on the above criteria? if len(contest_office_query): contest_office_on_stage = contest_office_query[0] contest_office_created = False # TODO Update contest_office information here elif len(contest_office_query) > 1: # We have bad data - a duplicate logger.error( u"We have bad data, a duplicate ContestOffice entry: {office}". format(office=google_civic_contest_office_on_stage.office)) return { 'error_result': True, } else: # Create a new ContestOffice entry logger.debug("Creating contest_office") contest_office_on_stage = ContestOffice( office_name=google_civic_contest_office_on_stage.office, election_id=election_on_stage.id, google_civic_election_id=google_civic_contest_office_on_stage. google_civic_election_id, number_voting_for=google_civic_contest_office_on_stage. number_voting_for, number_elected=google_civic_contest_office_on_stage. number_elected, primary_party=google_civic_contest_office_on_stage. primary_party, district_name=google_civic_contest_office_on_stage. district_name, district_scope=google_civic_contest_office_on_stage. district_scope, district_ocd_id=google_civic_contest_office_on_stage. district_ocd_id, ) contest_office_on_stage.save() contest_office_created = True google_civic_contest_office_on_stage.we_vote_election_id = election_on_stage.id google_civic_contest_office_on_stage.we_vote_contest_office_id = contest_office_on_stage.id google_civic_contest_office_on_stage.save() # Save the ballot_placement # Try to find earlier version based on the google_civic_election_id identifier logger.debug("Retrieving BallotItem") ballot_item_query = BallotItem.objects.all() ballot_item_query = ballot_item_query.filter(voter_id=1) ballot_item_query = ballot_item_query.filter( google_civic_election_id=google_civic_contest_office_on_stage. google_civic_election_id) ballot_item_query = ballot_item_query.filter( contest_office_id=contest_office_on_stage.id) if len(ballot_item_query) == 1: ballot_item_on_stage = ballot_item_query[0] ballot_item_on_stage_found = True except Exception as e: error_result = True handle_record_not_found_exception(e, logger=logger) try: voter_id = 1 if value_exists(google_civic_contest_office_on_stage.ballot_placement): # If the ballot order is specified by Google, use that. local_ballot_order = google_civic_contest_office_on_stage.ballot_placement else: # Pull the ballot order from the google_civic_ballot_item_manager = GoogleCivicBallotItemManager() local_ballot_order = google_civic_ballot_item_manager.fetch_ballot_order( voter_id, google_civic_contest_office_on_stage.google_civic_election_id, google_civic_contest_office_on_stage.district_ocd_id) if ballot_item_on_stage_found: # Update the values ballot_item_on_stage.election_id = election_on_stage.id # TODO Add all values here else: logger.debug("Creating BallotItem") ballot_item_on_stage = BallotItem( voter_id=voter_id, election_id=election_on_stage.id, google_civic_election_id=google_civic_contest_office_on_stage. google_civic_election_id, contest_office_id=contest_office_on_stage.id, # contest_measure_id: Used for measures/referendum/initiatives ballot_order=local_ballot_order, ballot_item_label=google_civic_contest_office_on_stage.office, ) ballot_item_on_stage.save() except Exception as e: error_result = True handle_record_not_saved_exception(e, logger=logger) results = { 'error_result': error_result, 'contest_office_on_stage': contest_office_on_stage, 'contest_office_created': contest_office_created, } return results
def transfer_maplight_data_to_we_vote_tables(request): # TODO We need to perhaps set up a table for these mappings that volunteers can add to? # We need a plan for how volunteers can help us add to these mappings # One possibility -- ask volunteers to update this Google Sheet, then write a csv importer: # https://docs.google.com/spreadsheets/d/1havD7GCxmBhi-zLLMdOpSJlU_DtBjvb5IJNiXgno9Bk/edit#gid=0 politician_name_mapping_list = [] one_mapping = { "google_civic_name": "Betty T. Yee", "maplight_display_name": "Betty Yee", "maplight_original_name": "Betty T Yee", } politician_name_mapping_list.append(one_mapping) one_mapping = { "google_civic_name": "Edmund G. \"Jerry\" Brown", "maplight_display_name": "Jerry Brown", "maplight_original_name": "", } politician_name_mapping_list.append(one_mapping) candidate_campaign_manager = CandidateCampaignManager() maplight_candidates_current_query = MapLightCandidate.objects.all() for one_candidate_from_maplight_table in maplight_candidates_current_query: found_by_id = False # Try to find a matching candidate results = candidate_campaign_manager.retrieve_candidate_campaign_from_id_maplight( one_candidate_from_maplight_table.candidate_id) if not results['success']: logger.warn(u"Candidate NOT found by MapLight id: {name}".format( name=one_candidate_from_maplight_table.candidate_id )) results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name( one_candidate_from_maplight_table.display_name) if not results['success']: logger.warn(u"Candidate NOT found by display_name: {name}".format( name=one_candidate_from_maplight_table.display_name )) results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name( one_candidate_from_maplight_table.original_name) if not results['success']: logger.warn(u"Candidate NOT found by original_name: {name}".format( name=one_candidate_from_maplight_table.original_name )) one_mapping_google_civic_name = '' for one_mapping_found in politician_name_mapping_list: if value_exists(one_mapping_found['maplight_display_name']) \ and one_mapping_found['maplight_display_name'] == \ one_candidate_from_maplight_table.display_name: one_mapping_google_civic_name = one_mapping_found['google_civic_name'] break if value_exists(one_mapping_google_civic_name): results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name( one_mapping_google_civic_name) if not results['success'] or not value_exists(one_mapping_google_civic_name): logger.warn(u"Candidate NOT found by mapping to google_civic name: {name}".format( name=one_mapping_google_civic_name )) continue # Go to the next candidate candidate_campaign_on_stage = results['candidate_campaign'] # Just in case the logic above let us through to here accidentally without a candidate_name value, don't proceed if not value_exists(candidate_campaign_on_stage.candidate_name): continue logger.debug(u"Candidate {name} found".format( name=candidate_campaign_on_stage.candidate_name )) try: # Tie the maplight id to our record if not found_by_id: candidate_campaign_on_stage.id_maplight = one_candidate_from_maplight_table.candidate_id # Bring over the photo candidate_campaign_on_stage.photo_url_from_maplight = one_candidate_from_maplight_table.photo # We can bring over other data as needed, like gender for example candidate_campaign_on_stage.save() except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.INFO, 'MapLight data woven into We Vote tables.') return HttpResponseRedirect(reverse('import_export:import_export_index', args=()))
def transfer_maplight_data_to_we_vote_tables(request): # TODO We need to perhaps set up a table for these mappings that volunteers can add to? # We need a plan for how volunteers can help us add to these mappings # One possibility -- ask volunteers to update this Google Sheet, then write a csv importer: # https://docs.google.com/spreadsheets/d/1havD7GCxmBhi-zLLMdOpSJlU_DtBjvb5IJNiXgno9Bk/edit#gid=0 politician_name_mapping_list = [] one_mapping = { "google_civic_name": "Betty T. Yee", "maplight_display_name": "Betty Yee", "maplight_original_name": "Betty T Yee", } politician_name_mapping_list.append(one_mapping) one_mapping = { "google_civic_name": "Edmund G. \"Jerry\" Brown", "maplight_display_name": "Jerry Brown", "maplight_original_name": "", } politician_name_mapping_list.append(one_mapping) candidate_campaign_manager = CandidateCampaignManager() maplight_candidates_current_query = MapLightCandidate.objects.all() for one_candidate_from_maplight_table in maplight_candidates_current_query: found_by_id = False # Try to find a matching candidate results = candidate_campaign_manager.retrieve_candidate_campaign_from_id_maplight( one_candidate_from_maplight_table.candidate_id) if not results['success']: logger.warn(u"Candidate NOT found by MapLight id: {name}".format( name=one_candidate_from_maplight_table.candidate_id)) results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name( one_candidate_from_maplight_table.display_name) if not results['success']: logger.warn( u"Candidate NOT found by display_name: {name}".format( name=one_candidate_from_maplight_table.display_name)) results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name( one_candidate_from_maplight_table.original_name) if not results['success']: logger.warn( u"Candidate NOT found by original_name: {name}".format( name=one_candidate_from_maplight_table. original_name)) one_mapping_google_civic_name = '' for one_mapping_found in politician_name_mapping_list: if value_exists(one_mapping_found['maplight_display_name']) \ and one_mapping_found['maplight_display_name'] == \ one_candidate_from_maplight_table.display_name: one_mapping_google_civic_name = one_mapping_found[ 'google_civic_name'] break if value_exists(one_mapping_google_civic_name): results = candidate_campaign_manager.retrieve_candidate_campaign_from_candidate_name( one_mapping_google_civic_name) if not results['success'] or not value_exists( one_mapping_google_civic_name): logger.warn( u"Candidate NOT found by mapping to google_civic name: {name}" .format(name=one_mapping_google_civic_name)) continue # Go to the next candidate candidate_campaign_on_stage = results['candidate_campaign'] # Just in case the logic above let us through to here accidentally without a candidate_name value, don't proceed if not value_exists(candidate_campaign_on_stage.candidate_name): continue logger.debug(u"Candidate {name} found".format( name=candidate_campaign_on_stage.candidate_name)) try: # Tie the maplight id to our record if not found_by_id: candidate_campaign_on_stage.id_maplight = one_candidate_from_maplight_table.candidate_id # Bring over the photo candidate_campaign_on_stage.photo_url_from_maplight = one_candidate_from_maplight_table.photo # We can bring over other data as needed, like gender for example candidate_campaign_on_stage.save() except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.INFO, 'MapLight data woven into We Vote tables.') return HttpResponseRedirect( reverse('import_export:import_export_index', args=()))