def retrieve_and_store_ballot_for_voter(voter_id, text_for_map_search=''): google_civic_election_id = 0 if not positive_value_exists(text_for_map_search): # Retrieve it from voter address voter_address_manager = VoterAddressManager() text_for_map_search = voter_address_manager.retrieve_ballot_map_text_from_voter_id(voter_id) if positive_value_exists(text_for_map_search): one_ballot_results = retrieve_one_ballot_from_google_civic_api(text_for_map_search) if one_ballot_results['success']: one_ballot_json = one_ballot_results['structured_json'] # We update VoterAddress with normalized address data in store_one_ballot_from_google_civic_api store_one_ballot_results = store_one_ballot_from_google_civic_api(one_ballot_json, voter_id) if store_one_ballot_results['success']: status = 'RETRIEVED_AND_STORED_BALLOT_FOR_VOTER' success = True google_civic_election_id = store_one_ballot_results['google_civic_election_id'] else: status = 'UNABLE_TO-store_one_ballot_from_google_civic_api' success = False else: status = 'UNABLE_TO-retrieve_one_ballot_from_google_civic_api' success = False else: status = 'MISSING_ADDRESS_TEXT_FOR_BALLOT_SEARCH' success = False results = { 'google_civic_election_id': google_civic_election_id, 'success': success, 'status': status, } return results
def voter_ballot_items_retrieve_from_google_civic_for_api( voter_device_id, text_for_map_search='', use_test_election=False): """ We are telling the server to explicitly reach out to the Google Civic API and retrieve the ballot items for this voter. """ # Confirm that we have a Google Civic API Key (GOOGLE_CIVIC_API_KEY) if not positive_value_exists(GOOGLE_CIVIC_API_KEY): results = { 'status': 'NO_GOOGLE_CIVIC_API_KEY', 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results # Confirm that we have the URL where we retrieve voter ballots (VOTER_INFO_URL) if not positive_value_exists(VOTER_INFO_URL): results = { 'status': 'MISSING VOTER_INFO_URL in config/environment_variables.json', 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results # Get voter_id from the voter_device_id so we can figure out which ballot_items to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: results = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): results = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results google_civic_election_id = 0 status = '' success = False election_date_text = '' election_description_text = '' election_data_retrieved = False polling_location_retrieved = False contests_retrieved = False if not positive_value_exists(text_for_map_search): # Retrieve it from voter address voter_address_manager = VoterAddressManager() text_for_map_search = voter_address_manager.retrieve_ballot_map_text_from_voter_id(voter_id) if positive_value_exists(text_for_map_search): one_ballot_results = retrieve_one_ballot_from_google_civic_api( text_for_map_search, google_civic_election_id, use_test_election) if one_ballot_results['success']: one_ballot_json = one_ballot_results['structured_json'] election_date_text = one_ballot_json['election']['electionDay'] election_description_text = one_ballot_json['election']['name'] # We may receive some election data, but not all of the data we need if one_ballot_results['election_data_retrieved']: election_data_retrieved = True success = True if one_ballot_results['polling_location_retrieved']: polling_location_retrieved = True success = True if one_ballot_results['contests_retrieved']: contests_retrieved = True # Now that we know we have new ballot data, we need to delete prior ballot data for this election # because when we change voterAddress, we usually get different ballot items ballot_item_list_manager = BallotItemListManager() # We include a google_civic_election_id, so only the ballot info for this election is removed google_civic_election_id_to_delete = one_ballot_json['election']['id'] # '0' would mean "delete all" if positive_value_exists(google_civic_election_id_to_delete): ballot_item_list_manager.delete_all_ballot_items_for_voter( voter_id, google_civic_election_id_to_delete) # store_on_ballot... adds an entry to the BallotReturned table # We update VoterAddress with normalized address data in store_one_ballot_from_google_civic_api store_one_ballot_results = store_one_ballot_from_google_civic_api(one_ballot_json, voter_id) if store_one_ballot_results['success']: status += 'RETRIEVED_FROM_GOOGLE_CIVIC_AND_STORED_BALLOT_FOR_VOTER ' success = True google_civic_election_id = store_one_ballot_results['google_civic_election_id'] else: status += 'UNABLE_TO-store_one_ballot_from_google_civic_api' elif 'error' in one_ballot_results['structured_json']: if one_ballot_results['structured_json']['error']['message'] == 'Election unknown': success = False # It is only successful if new ballot data is retrieved. else: success = False status += "GOOGLE_CIVIC_API_ERROR: " + one_ballot_results['structured_json']['error']['message'] else: status += 'UNABLE_TO-retrieve_one_ballot_from_google_civic_api' success = False else: status += 'MISSING_ADDRESS_TEXT_FOR_BALLOT_SEARCH' success = False # If a google_civic_election_id was not returned, outside of this function we search again using a test election, # so that during our initial user testing, ballot data is returned in areas where elections don't currently exist results = { 'success': success, 'status': status, 'voter_device_id': voter_device_id, 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': text_for_map_search, 'election_date_text': election_date_text, 'election_description_text': election_description_text, 'election_data_retrieved': election_data_retrieved, 'polling_location_retrieved': polling_location_retrieved, 'contests_retrieved': contests_retrieved, } return results
def voter_ballot_items_retrieve_from_google_civic_for_api( voter_device_id, text_for_map_search='', use_test_election=False): """ We are telling the server to explicitly reach out to the Google Civic API and retrieve the ballot items for this voter. """ # Confirm that we have a Google Civic API Key (GOOGLE_CIVIC_API_KEY) if not positive_value_exists(GOOGLE_CIVIC_API_KEY): results = { 'status': 'NO_GOOGLE_CIVIC_API_KEY', 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results # Confirm that we have the URL where we retrieve voter ballots (VOTER_INFO_URL) if not positive_value_exists(VOTER_INFO_URL): results = { 'status': 'MISSING VOTER_INFO_URL in config/environment_variables.json', 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results # Get voter_id from the voter_device_id so we can figure out which ballot_items to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: results = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): results = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'google_civic_election_id': 0, 'text_for_map_search': text_for_map_search, } return results google_civic_election_id = 0 status = '' success = False election_date_text = '' election_description_text = '' election_data_retrieved = False polling_location_retrieved = False contests_retrieved = False if not positive_value_exists(text_for_map_search): # Retrieve it from voter address voter_address_manager = VoterAddressManager() text_for_map_search = voter_address_manager.retrieve_ballot_map_text_from_voter_id( voter_id) if positive_value_exists(text_for_map_search): one_ballot_results = retrieve_one_ballot_from_google_civic_api( text_for_map_search, google_civic_election_id, use_test_election) if one_ballot_results['success']: one_ballot_json = one_ballot_results['structured_json'] election_date_text = one_ballot_json['election']['electionDay'] election_description_text = one_ballot_json['election']['name'] # We may receive some election data, but not all of the data we need if one_ballot_results['election_data_retrieved']: election_data_retrieved = True success = True if one_ballot_results['polling_location_retrieved']: polling_location_retrieved = True success = True if one_ballot_results['contests_retrieved']: contests_retrieved = True # Now that we know we have new ballot data, we need to delete prior ballot data for this election # because when we change voterAddress, we usually get different ballot items ballot_item_list_manager = BallotItemListManager() # We include a google_civic_election_id, so only the ballot info for this election is removed google_civic_election_id_to_delete = one_ballot_json[ 'election']['id'] # '0' would mean "delete all" if positive_value_exists(google_civic_election_id_to_delete): ballot_item_list_manager.delete_all_ballot_items_for_voter( voter_id, google_civic_election_id_to_delete) # store_on_ballot... adds an entry to the BallotReturned table # We update VoterAddress with normalized address data in store_one_ballot_from_google_civic_api store_one_ballot_results = store_one_ballot_from_google_civic_api( one_ballot_json, voter_id) if store_one_ballot_results['success']: status += 'RETRIEVED_FROM_GOOGLE_CIVIC_AND_STORED_BALLOT_FOR_VOTER ' success = True google_civic_election_id = store_one_ballot_results[ 'google_civic_election_id'] else: status += 'UNABLE_TO-store_one_ballot_from_google_civic_api' elif 'error' in one_ballot_results['structured_json']: if one_ballot_results['structured_json']['error'][ 'message'] == 'Election unknown': success = False # It is only successful if new ballot data is retrieved. else: success = False status += "GOOGLE_CIVIC_API_ERROR: " + one_ballot_results[ 'structured_json']['error']['message'] else: status += 'UNABLE_TO-retrieve_one_ballot_from_google_civic_api' success = False else: status += 'MISSING_ADDRESS_TEXT_FOR_BALLOT_SEARCH' success = False # If a google_civic_election_id was not returned, outside of this function we search again using a test election, # so that during our initial user testing, ballot data is returned in areas where elections don't currently exist results = { 'success': success, 'status': status, 'voter_device_id': voter_device_id, 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': text_for_map_search, 'election_date_text': election_date_text, 'election_description_text': election_description_text, 'election_data_retrieved': election_data_retrieved, 'polling_location_retrieved': polling_location_retrieved, 'contests_retrieved': contests_retrieved, } return results