def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): status = '' # We retrieve voter_device_link voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link(voter_device_id) if not voter_device_link_results['voter_device_link_found']: status += "VALID_VOTER_DEVICE_ID_MISSING " error_json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_device_link = voter_device_link_results['voter_device_link'] voter_id = voter_device_link.voter_id if not positive_value_exists(voter_id): status += " " + "VALID_VOTER_ID_MISSING" error_json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_address_manager = VoterAddressManager() voter_address_id = 0 address_type = BALLOT_ADDRESS voter_address_results = voter_address_manager.retrieve_address(voter_address_id, voter_id, address_type) status += " " + voter_address_results['status'] if not positive_value_exists(voter_address_results['voter_address_has_value']): error_json_data = { 'status': status, 'success': voter_address_results['success'], 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_address = voter_address_results['voter_address'] results = choose_election_and_prepare_ballot_data(voter_device_link, google_civic_election_id, voter_address) status += " " + results['status'] if not results['voter_ballot_saved_found']: if positive_value_exists(voter_address.text_for_map_search): ballot_caveat = "We could not find a ballot near '{text_for_map_search}'.".format( text_for_map_search=voter_address.text_for_map_search) else: ballot_caveat = "Please save your address so we can find your ballot." error_json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': voter_address.text_for_map_search, 'substituted_address_nearby': '', 'ballot_caveat': ballot_caveat, 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data google_civic_election_id = results['google_civic_election_id'] voter_ballot_saved = results['voter_ballot_saved'] # Update voter_device_link if voter_device_link.google_civic_election_id != google_civic_election_id: voter_device_link_manager.update_voter_device_link_with_election_id(voter_device_link, google_civic_election_id) # Update voter_address to include matching google_civic_election_id and voter_ballot_saved entry if positive_value_exists(google_civic_election_id): voter_address.google_civic_election_id = google_civic_election_id voter_address_manager.update_existing_voter_address_object(voter_address) # Get and return the ballot_item_list results = voter_ballot_items_retrieve_for_one_election_for_api(voter_device_id, voter_id, google_civic_election_id) status += " " + results['status'] json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': True, 'ballot_item_list': results['ballot_item_list'], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': voter_ballot_saved.original_text_for_map_search, 'substituted_address_nearby': voter_ballot_saved.substituted_address_nearby, 'ballot_caveat': voter_ballot_saved.ballot_caveat(), 'is_from_substituted_address': voter_ballot_saved.is_from_substituted_address, 'is_from_test_ballot': voter_ballot_saved.is_from_test_ballot, } return json_data status += " " + "NO_VOTER_BALLOT_SAVED_FOUND" error_json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data
def positions_count_for_all_ballot_items_for_api( voter_device_id, google_civic_election_id=0, show_positions_this_voter_follows=True): """ We want to return a JSON file with the a list of the support and oppose counts from the orgs, friends and public figures the voter follows """ # Get voter_id from the voter_device_id so we can know whose stars to retrieve results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': "VALID_VOTER_DEVICE_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VALID_VOTER_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data if not positive_value_exists(google_civic_election_id): # We must have an election id to proceed -- otherwise we don't know what ballot items to work with voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link( voter_device_id) if voter_device_link_results['voter_device_link_found']: voter_device_link = voter_device_link_results['voter_device_link'] if positive_value_exists( voter_device_link.google_civic_election_id): google_civic_election_id = voter_device_link.google_civic_election_id if not positive_value_exists(google_civic_election_id): voter_address_manager = VoterAddressManager() voter_address_results = voter_address_manager.retrieve_address( 0, voter_id) if voter_address_results['voter_address_found']: voter_address = voter_address_results['voter_address'] if positive_value_exists( voter_address.google_civic_election_id): google_civic_election_id = voter_address.google_civic_election_id google_civic_election_id = convert_to_int(google_civic_election_id) if not positive_value_exists(google_civic_election_id): json_data = { 'status': "GOOGLE_CIVIC_ELECTION_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data position_list_manager = PositionListManager() candidate_list_object = CandidateCampaignList() follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Get a list of all candidates and measures from this election (in the active election) ballot_item_results = voter_ballot_items_retrieve_for_one_election_for_api( voter_device_id, voter_id, google_civic_election_id) ballot_item_list = ballot_item_results['ballot_item_list'] # The list where we capture results ballot_item_list_results = [] # ballot_item_list is populated with contest_office and contest_measure entries for one_ballot_item in ballot_item_list: # Retrieve all positions for each ballot item if one_ballot_item['kind_of_ballot_item'] == OFFICE: results = candidate_list_object.retrieve_all_candidates_for_office( 0, one_ballot_item['we_vote_id']) success = results['success'] candidate_list = results['candidate_list'] if success: for candidate in candidate_list: # Loop through all candidates under this office support_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( 0, candidate.we_vote_id, SUPPORT) oppose_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( 0, candidate.we_vote_id, OPPOSE) finalize_results = finalize_support_and_oppose_positions_count( voter_id, show_positions_this_voter_follows, organizations_followed_by_voter, support_positions_list_for_one_ballot_item, oppose_positions_list_for_one_ballot_item) one_ballot_item_results = { 'ballot_item_we_vote_id': candidate.we_vote_id, 'support_count': finalize_results['support_positions_count'], 'oppose_count': finalize_results['oppose_positions_count'], } ballot_item_list_results.append(one_ballot_item_results) elif one_ballot_item['kind_of_ballot_item'] == MEASURE: support_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_contest_measure(0, one_ballot_item['we_vote_id'], SUPPORT) oppose_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_contest_measure(0, one_ballot_item['we_vote_id'], OPPOSE) finalize_results = finalize_support_and_oppose_positions_count( voter_id, show_positions_this_voter_follows, organizations_followed_by_voter, support_positions_list_for_one_ballot_item, oppose_positions_list_for_one_ballot_item) one_ballot_item_results = { 'ballot_item_we_vote_id': one_ballot_item['we_vote_id'], 'support_count': finalize_results['support_positions_count'], 'oppose_count': finalize_results['oppose_positions_count'], } ballot_item_list_results.append(one_ballot_item_results) else: # Skip the rest of this loop continue json_data = { 'success': True, 'status': "POSITIONS_COUNT_FOR_ALL_BALLOT_ITEMS", 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': ballot_item_list_results, } return json_data
def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): status = '' # We retrieve voter_device_link voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link( voter_device_id) if not voter_device_link_results['voter_device_link_found']: status += "VALID_VOTER_DEVICE_ID_MISSING " error_json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_device_link = voter_device_link_results['voter_device_link'] voter_id = voter_device_link.voter_id if not positive_value_exists(voter_id): status += " " + "VALID_VOTER_ID_MISSING" error_json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_address_manager = VoterAddressManager() voter_address_id = 0 address_type = BALLOT_ADDRESS voter_address_results = voter_address_manager.retrieve_address( voter_address_id, voter_id, address_type) status += " " + voter_address_results['status'] if not positive_value_exists( voter_address_results['voter_address_has_value']): error_json_data = { 'status': status, 'success': voter_address_results['success'], 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data voter_address = voter_address_results['voter_address'] results = choose_election_and_prepare_ballot_data( voter_device_link, google_civic_election_id, voter_address) status += " " + results['status'] if not results['voter_ballot_saved_found']: if positive_value_exists(voter_address.text_for_map_search): ballot_caveat = "We could not find a ballot near '{text_for_map_search}'.".format( text_for_map_search=voter_address.text_for_map_search) else: ballot_caveat = "Please save your address so we can find your ballot." error_json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': voter_address.text_for_map_search, 'substituted_address_nearby': '', 'ballot_caveat': ballot_caveat, 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data google_civic_election_id = results['google_civic_election_id'] voter_ballot_saved = results['voter_ballot_saved'] # Update voter_device_link if voter_device_link.google_civic_election_id != google_civic_election_id: voter_device_link_manager.update_voter_device_link_with_election_id( voter_device_link, google_civic_election_id) # Update voter_address to include matching google_civic_election_id and voter_ballot_saved entry if positive_value_exists(google_civic_election_id): voter_address.google_civic_election_id = google_civic_election_id voter_address_manager.update_existing_voter_address_object( voter_address) # Get and return the ballot_item_list results = voter_ballot_items_retrieve_for_one_election_for_api( voter_device_id, voter_id, google_civic_election_id) if not positive_value_exists(voter_ballot_saved.election_description_text) \ or not positive_value_exists(voter_ballot_saved.election_date_text()): try: election_manager = ElectionManager() election_results = election_manager.retrieve_election( google_civic_election_id) if election_results['election_found']: election = election_results['election'] if not positive_value_exists( voter_ballot_saved.election_description_text): voter_ballot_saved.election_description_text = election.election_name if not positive_value_exists( voter_ballot_saved.election_date_text()): voter_ballot_saved.election_date = \ datetime.strptime(election.election_day_text, "%Y-%m-%d").date() voter_ballot_saved.save() except Exception as e: status += "Failed to update election_name" status += " " + results['status'] json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': True, 'ballot_item_list': results['ballot_item_list'], 'google_civic_election_id': google_civic_election_id, 'election_name': voter_ballot_saved.election_description_text, 'election_date': voter_ballot_saved.election_date_text(), 'text_for_map_search': voter_ballot_saved.original_text_for_map_search, 'substituted_address_nearby': voter_ballot_saved.substituted_address_nearby, 'ballot_caveat': voter_ballot_saved.ballot_caveat(), 'is_from_substituted_address': voter_ballot_saved.is_from_substituted_address, 'is_from_test_ballot': voter_ballot_saved.is_from_test_ballot, } return json_data status += " " + "NO_VOTER_BALLOT_SAVED_FOUND" error_json_data = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'ballot_found': False, 'ballot_item_list': [], 'google_civic_election_id': 0, 'text_for_map_search': '', 'substituted_address_nearby': '', 'ballot_caveat': '', 'is_from_substituted_address': False, 'is_from_test_ballot': False, } return error_json_data
def figure_out_google_civic_election_id_voter_is_watching(voter_device_id): status = '' # We zero out this value since we will never have this coming in for this function google_civic_election_id = 0 # We retrieve voter_device_link voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link( voter_device_id) if not voter_device_link_results['voter_device_link_found']: status += "VALID_VOTER_DEVICE_ID_MISSING: " + voter_device_link_results[ 'status'] results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': False, 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_device_link = voter_device_link_results['voter_device_link'] voter_id = voter_device_link.voter_id if not positive_value_exists(voter_id): status += " " + "VALID_VOTER_ID_MISSING" results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': False, 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_address_manager = VoterAddressManager() voter_address_id = 0 address_type = BALLOT_ADDRESS voter_address_results = voter_address_manager.retrieve_address( voter_address_id, voter_id, address_type) status += " " + voter_address_results['status'] if not positive_value_exists( voter_address_results['voter_address_has_value']): # If there isn't an address with a value, then there won't be a voter_ballot_saved_found results = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': voter_address_results['voter_address_found'], 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_address = voter_address_results['voter_address'] # This routine finds a ballot saved for this voter choose_election_results = choose_election_from_existing_data( voter_device_link, google_civic_election_id, voter_address) status += " " + choose_election_results['status'] results = { 'status': status, 'success': choose_election_results['success'], 'voter_device_id': voter_device_id, 'voter_device_link_found': True, 'voter_address_object_found': voter_address_results['voter_address_found'], 'voter_ballot_saved_found': choose_election_results['voter_ballot_saved_found'], 'google_civic_election_id': choose_election_results['google_civic_election_id'], } return results
def figure_out_google_civic_election_id_voter_is_watching(voter_device_id): status = '' # We zero out this value since we will never have this coming in for this function google_civic_election_id = 0 # We retrieve voter_device_link voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link(voter_device_id) if not voter_device_link_results['voter_device_link_found']: status += "VALID_VOTER_DEVICE_ID_MISSING: " + voter_device_link_results['status'] results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': False, 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_device_link = voter_device_link_results['voter_device_link'] voter_id = voter_device_link.voter_id if not positive_value_exists(voter_id): status += " " + "VALID_VOTER_ID_MISSING" results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': False, 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_address_manager = VoterAddressManager() voter_address_id = 0 address_type = BALLOT_ADDRESS voter_address_results = voter_address_manager.retrieve_address(voter_address_id, voter_id, address_type) status += " " + voter_address_results['status'] if not positive_value_exists(voter_address_results['voter_address_has_value']): # If there isn't an address with a value, then there won't be a voter_ballot_saved_found results = { 'status': status, 'success': True, 'voter_device_id': voter_device_id, 'voter_device_link_found': False, 'voter_address_object_found': voter_address_results['voter_address_found'], 'voter_ballot_saved_found': False, 'google_civic_election_id': 0, } return results voter_address = voter_address_results['voter_address'] # This routine finds a ballot saved for this voter choose_election_results = choose_election_from_existing_data(voter_device_link, google_civic_election_id, voter_address) status += " " + choose_election_results['status'] results = { 'status': status, 'success': choose_election_results['success'], 'voter_device_id': voter_device_id, 'voter_device_link_found': True, 'voter_address_object_found': voter_address_results['voter_address_found'], 'voter_ballot_saved_found': choose_election_results['voter_ballot_saved_found'], 'google_civic_election_id': choose_election_results['google_civic_election_id'], } return results
def positions_count_for_all_ballot_items_for_api(voter_device_id, google_civic_election_id=0, show_positions_this_voter_follows=True): """ We want to return a JSON file with the a list of the support and oppose counts from the orgs, friends and public figures the voter follows """ # Get voter_id from the voter_device_id so we can know whose stars to retrieve results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': "VALID_VOTER_DEVICE_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VALID_VOTER_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data if not positive_value_exists(google_civic_election_id): # We must have an election id to proceed -- otherwise we don't know what ballot items to work with voter_device_link_manager = VoterDeviceLinkManager() voter_device_link_results = voter_device_link_manager.retrieve_voter_device_link(voter_device_id) if voter_device_link_results['voter_device_link_found']: voter_device_link = voter_device_link_results['voter_device_link'] if positive_value_exists(voter_device_link.google_civic_election_id): google_civic_election_id = voter_device_link.google_civic_election_id if not positive_value_exists(google_civic_election_id): voter_address_manager = VoterAddressManager() voter_address_results = voter_address_manager.retrieve_address(0, voter_id) if voter_address_results['voter_address_found']: voter_address = voter_address_results['voter_address'] if positive_value_exists(voter_address.google_civic_election_id): google_civic_election_id = voter_address.google_civic_election_id google_civic_election_id = convert_to_int(google_civic_election_id) if not positive_value_exists(google_civic_election_id): json_data = { 'status': "GOOGLE_CIVIC_ELECTION_ID_MISSING-COUNT_FOR_ALL_BALLOT_ITEMS", 'success': False, 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': [], } return json_data position_list_manager = PositionListManager() candidate_list_object = CandidateCampaignList() follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Get a list of all candidates and measures from this election (in the active election) ballot_item_results = voter_ballot_items_retrieve_for_one_election_for_api(voter_device_id, voter_id, google_civic_election_id) ballot_item_list = ballot_item_results['ballot_item_list'] # The list where we capture results ballot_item_list_results = [] # ballot_item_list is populated with contest_office and contest_measure entries for one_ballot_item in ballot_item_list: # Retrieve all positions for each ballot item if one_ballot_item['kind_of_ballot_item'] == OFFICE: results = candidate_list_object.retrieve_all_candidates_for_office(0, one_ballot_item['we_vote_id']) success = results['success'] candidate_list = results['candidate_list'] if success: for candidate in candidate_list: # Loop through all candidates under this office support_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( 0, candidate.we_vote_id, SUPPORT) oppose_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( 0, candidate.we_vote_id, OPPOSE) finalize_results = finalize_support_and_oppose_positions_count( voter_id, show_positions_this_voter_follows, organizations_followed_by_voter, support_positions_list_for_one_ballot_item, oppose_positions_list_for_one_ballot_item) one_ballot_item_results = { 'ballot_item_we_vote_id': candidate.we_vote_id, 'support_count': finalize_results['support_positions_count'], 'oppose_count': finalize_results['oppose_positions_count'], } ballot_item_list_results.append(one_ballot_item_results) elif one_ballot_item['kind_of_ballot_item'] == MEASURE: support_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_contest_measure(0, one_ballot_item['we_vote_id'], SUPPORT) oppose_positions_list_for_one_ballot_item = \ position_list_manager.retrieve_all_positions_for_contest_measure(0, one_ballot_item['we_vote_id'], OPPOSE) finalize_results = finalize_support_and_oppose_positions_count( voter_id, show_positions_this_voter_follows, organizations_followed_by_voter, support_positions_list_for_one_ballot_item, oppose_positions_list_for_one_ballot_item) one_ballot_item_results = { 'ballot_item_we_vote_id': one_ballot_item['we_vote_id'], 'support_count': finalize_results['support_positions_count'], 'oppose_count': finalize_results['oppose_positions_count'], } ballot_item_list_results.append(one_ballot_item_results) else: # Skip the rest of this loop continue json_data = { 'success': True, 'status': "POSITIONS_COUNT_FOR_ALL_BALLOT_ITEMS", 'google_civic_election_id': google_civic_election_id, 'ballot_item_list': ballot_item_list_results, } return json_data