def voter_position_like_off_save_for_api(voter_device_id, position_like_id, position_entered_id): # Get voter_id from the voter_device_id so we can know who is doing the liking results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') 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", 'success': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') position_like_manager = PositionLikeManager() if positive_value_exists(position_like_id) or \ (positive_value_exists(voter_id) and positive_value_exists(position_entered_id)): results = position_like_manager.toggle_off_voter_position_like( position_like_id, voter_id, position_entered_id) status = results['status'] success = results['success'] else: status = 'UNABLE_TO_DELETE_POSITION_LIKE-INSUFFICIENT_VARIABLES' success = False json_data = { 'status': status, 'success': success, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_guide_possibility_retrieve_for_api(voter_device_id, voter_guide_possibility_url): results = is_voter_device_id_valid(voter_device_id) voter_guide_possibility_url = voter_guide_possibility_url # TODO Use scrapy here if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') # TODO We will need the voter_id here so we can control volunteer actions voter_guide_possibility_manager = VoterGuidePossibilityManager() results = voter_guide_possibility_manager.retrieve_voter_guide_possibility_from_url(voter_guide_possibility_url) json_data = { 'voter_device_id': voter_device_id, 'voter_guide_possibility_url': results['voter_guide_possibility_url'], 'voter_guide_possibility_id': results['voter_guide_possibility_id'], 'organization_we_vote_id': results['organization_we_vote_id'], 'public_figure_we_vote_id': results['public_figure_we_vote_id'], 'owner_we_vote_id': results['owner_we_vote_id'], 'status': results['status'], 'success': results['success'], } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_star_on_save_for_api(voter_device_id, office_id, candidate_id, measure_id): # Get voter_id from the voter_device_id so we can know who is doing the starring results = is_voter_device_id_valid(voter_device_id) if not results["success"]: json_data = {"status": "VALID_VOTER_DEVICE_ID_MISSING", "success": False} return HttpResponse(json.dumps(json_data), content_type="application/json") 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", "success": False} return HttpResponse(json.dumps(json_data), content_type="application/json") star_item_manager = StarItemManager() if positive_value_exists(office_id): results = star_item_manager.toggle_on_voter_starred_office(voter_id, office_id) status = "STAR_ON_OFFICE " + results["status"] success = results["success"] elif positive_value_exists(candidate_id): results = star_item_manager.toggle_on_voter_starred_candidate(voter_id, candidate_id) status = "STAR_ON_CANDIDATE " + results["status"] success = results["success"] elif positive_value_exists(measure_id): results = star_item_manager.toggle_on_voter_starred_measure(voter_id, measure_id) status = "STAR_ON_MEASURE " + results["status"] success = results["success"] else: status = "UNABLE_TO_SAVE_ON-OFFICE_ID_AND_CANDIDATE_ID_AND_MEASURE_ID_MISSING" success = False json_data = {"status": status, "success": success} return HttpResponse(json.dumps(json_data), content_type="application/json")
def voter_create(voter_device_id): results = is_voter_device_id_valid(voter_device_id) if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') voter_id = 0 # Make sure a voter record hasn't already been created for this existing_voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if existing_voter_id: json_data = { 'status': "VOTER_ALREADY_EXISTS", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') # Create a new voter and return the id voter_manager = VoterManager() results = voter_manager.create_voter() if results['voter_created']: voter = results['voter'] # Now save the voter_device_link voter_device_link_manager = VoterDeviceLinkManager() results = voter_device_link_manager.save_new_voter_device_link(voter_device_id, voter.id) if results['voter_device_link_created']: voter_device_link = results['voter_device_link'] voter_id_found = True if voter_device_link.voter_id > 0 else False if voter_id_found: voter_id = voter_device_link.voter_id if voter_id: json_data = { 'status': "VOTER_CREATED", 'success': False, 'voter_device_id': voter_device_id, 'voter_id': voter_id, # We may want to remove this after initial testing } return HttpResponse(json.dumps(json_data), content_type='application/json') else: json_data = { 'status': "VOTER_NOT_CREATED", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_position_like_status_retrieve_for_api(voter_device_id, position_entered_id): # Get voter_id from the voter_device_id so we can know who is doing the liking results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'is_liked': False, 'position_entered_id': position_entered_id, 'position_like_id': 0, } return HttpResponse(json.dumps(json_data), content_type='application/json') 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", 'success': False, 'voter_device_id': voter_device_id, 'is_liked': False, 'position_entered_id': position_entered_id, 'position_like_id': 0, } return HttpResponse(json.dumps(json_data), content_type='application/json') position_like_manager = PositionLikeManager() if positive_value_exists(position_entered_id): position_like_id = 0 results = position_like_manager.retrieve_position_like(position_like_id, voter_id, position_entered_id) status = results['status'] success = results['success'] is_liked = results['is_liked'] position_like_id = results['position_like_id'] else: status = 'UNABLE_TO_RETRIEVE-POSITION_ENTERED_ID_MISSING' success = False is_liked = False position_like_id = 0 json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_liked': is_liked, 'position_entered_id': position_entered_id, 'position_like_id': position_like_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_guide_possibility_save_for_api(voter_device_id, voter_guide_possibility_url): results = is_voter_device_id_valid(voter_device_id) if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') if not voter_guide_possibility_url: json_data = { 'status': "MISSING_POST_VARIABLE-URL", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VOTER_NOT_FOUND_FROM_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') # At this point, we have a valid voter voter_guide_possibility_manager = VoterGuidePossibilityManager() # We wrap get_or_create because we want to centralize error handling results = voter_guide_possibility_manager.update_or_create_voter_guide_possibility( voter_guide_possibility_url.strip()) if results['success']: json_data = { 'status': "VOTER_GUIDE_POSSIBILITY_SAVED", 'success': True, 'voter_device_id': voter_device_id, 'voter_guide_possibility_url': voter_guide_possibility_url, } # elif results['status'] == 'MULTIPLE_MATCHING_ADDRESSES_FOUND': # delete all currently matching addresses and save again? else: json_data = { 'status': results['status'], 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_address_save_for_api(voter_device_id, address_raw_text, address_variable_exists): results = is_voter_device_id_valid(voter_device_id) if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') if not address_variable_exists: json_data = { 'status': "MISSING_POST_VARIABLE-ADDRESS", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if voter_id < 0: json_data = { 'status': "VOTER_NOT_FOUND_FROM_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') # At this point, we have a valid voter voter_address_manager = VoterAddressManager() address_type = BALLOT_ADDRESS # We wrap get_or_create because we want to centralize error handling results = voter_address_manager.update_or_create_voter_address(voter_id, address_type, address_raw_text.strip()) if results['success']: json_data = { 'status': "VOTER_ADDRESS_SAVED", 'success': True, 'voter_device_id': voter_device_id, 'text_for_map_search': address_raw_text, } # elif results['status'] == 'MULTIPLE_MATCHING_ADDRESSES_FOUND': # delete all currently matching addresses and save again else: json_data = { 'status': results['status'], 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_address_retrieve_for_api(voter_device_id): results = is_voter_device_id_valid(voter_device_id) if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not voter_id > 0: json_data = { 'status': "VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_address_manager = VoterAddressManager() results = voter_address_manager.retrieve_ballot_address_from_voter_id(voter_id) if results['voter_address_found']: voter_address = results['voter_address'] json_data = { 'voter_device_id': voter_device_id, 'address_type': voter_address.address_type if voter_address.address_type else '', 'text_for_map_search': voter_address.text_for_map_search if voter_address.text_for_map_search else '', 'latitude': voter_address.latitude if voter_address.latitude else '', 'longitude': voter_address.longitude if voter_address.longitude else '', 'normalized_line1': voter_address.normalized_line1 if voter_address.normalized_line1 else '', 'normalized_line2': voter_address.normalized_line2 if voter_address.normalized_line2 else '', 'normalized_city': voter_address.normalized_city if voter_address.normalized_city else '', 'normalized_state': voter_address.normalized_state if voter_address.normalized_state else '', 'normalized_zip': voter_address.normalized_zip if voter_address.normalized_zip else '', 'success': True, } return HttpResponse(json.dumps(json_data), content_type='application/json') else: json_data = { 'status': "VOTER_ADDRESS_NOT_RETRIEVED", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def positions_count_for_api(voter_device_id, candidate_id, measure_id, stance_we_are_looking_for): # Get voter_id from the voter_device_id so we can know who is supporting/opposing results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') 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 ", 'success': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') show_positions_this_voter_follows = True if positive_value_exists(candidate_id): results = positions_count_for_candidate_campaign(voter_id, candidate_id, stance_we_are_looking_for, show_positions_this_voter_follows) json_data = results['json_data'] return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(measure_id): results = positions_count_for_contest_measure(voter_id, measure_id, stance_we_are_looking_for, show_positions_this_voter_follows) json_data = results['json_data'] return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_RETRIEVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_opposing_save(voter_device_id, candidate_id, measure_id): # Get voter_id from the voter_device_id so we can know who is supporting/opposing results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') 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", 'success': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') position_entered_manager = PositionEnteredManager() if positive_value_exists(candidate_id): results = position_entered_manager.toggle_on_voter_oppose_for_candidate_campaign(voter_id, candidate_id) # toggle_off_voter_support_for_candidate_campaign status = "OPPOSING_CANDIDATE " + results['status'] success = results['success'] elif positive_value_exists(measure_id): results = position_entered_manager.toggle_on_voter_oppose_for_contest_measure(voter_id, measure_id) status = "OPPOSING_MEASURE " + results['status'] success = results['success'] else: status = 'UNABLE_TO_SAVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def position_list_for_ballot_item_for_api(voter_device_id, office_id, candidate_id, measure_id, stance_we_are_looking_for=ANY_STANCE, show_positions_this_voter_follows=True): """ We want to return a JSON file with the position identifiers from orgs, friends and public figures the voter follows This list of information is used to retrieve the detailed information """ # Get voter_id from the voter_device_id so we can know who is supporting/opposing results = is_voter_device_id_valid(voter_device_id) if not results['success']: position_list = [] json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'count': 0, 'kind_of_ballot_item': "UNKNOWN", 'ballot_item_id': 0, 'position_list': position_list, } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): position_list = [] json_data = { 'status': "VALID_VOTER_ID_MISSING ", 'success': False, 'count': 0, 'kind_of_ballot_item': "UNKNOWN", 'ballot_item_id': 0, 'position_list': position_list, } return HttpResponse(json.dumps(json_data), content_type='application/json') position_list_manager = PositionListManager() if positive_value_exists(candidate_id): candidate_we_vote_id = '' all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_id, candidate_we_vote_id, stance_we_are_looking_for) kind_of_ballot_item = CANDIDATE ballot_item_id = candidate_id elif positive_value_exists(measure_id): measure_we_vote_id = '' all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure( measure_id, measure_we_vote_id, stance_we_are_looking_for) kind_of_ballot_item = MEASURE ballot_item_id = measure_id elif positive_value_exists(office_id): office_we_vote_id = '' all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office( office_id, office_we_vote_id, stance_we_are_looking_for) kind_of_ballot_item = OFFICE ballot_item_id = measure_id else: position_list = [] json_data = { 'status': 'POSITION_LIST_RETRIEVE_MISSING_BALLOT_ITEM_ID', 'success': False, 'count': 0, 'kind_of_ballot_item': "UNKNOWN", 'ballot_item_id': 0, 'position_list': position_list, } return HttpResponse(json.dumps(json_data), content_type='application/json') follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) if show_positions_this_voter_follows: position_objects = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list, organizations_followed_by_voter) positions_count = len(position_objects) status = 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_FOLLOWED' success = True else: position_objects = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) positions_count = len(position_objects) status = 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED' success = True position_list = [] for one_position in position_objects: # Whose position is it? if positive_value_exists(one_position.organization_we_vote_id): speaker_type = ORGANIZATION speaker_id = one_position.organization_id speaker_we_vote_id = one_position.organization_we_vote_id one_position_success = True elif positive_value_exists(one_position.voter_id): speaker_type = VOTER speaker_id = one_position.voter_id speaker_we_vote_id = one_position.voter_we_vote_id one_position_success = True elif positive_value_exists(one_position.public_figure_we_vote_id): speaker_type = PUBLIC_FIGURE speaker_id = one_position.public_figure_id speaker_we_vote_id = one_position.public_figure_we_vote_id one_position_success = True else: speaker_type = UNKNOWN_VOTER_GUIDE speaker_id = None speaker_we_vote_id = None one_position_success = False if one_position_success: one_position_dict_for_api = { 'position_id': one_position.id, 'position_we_vote_id': one_position.we_vote_id, 'speaker_label': 'Organization Name TEMP', # TODO DALE Add this to PositionEntered 'speaker_type': speaker_type, 'speaker_id': speaker_id, 'speaker_we_vote_id': speaker_we_vote_id, 'is_support': one_position.is_support(), 'is_oppose': one_position.is_oppose(), } position_list.append(one_position_dict_for_api) json_data = { 'status': status, 'success': success, 'count': positions_count, 'kind_of_ballot_item': kind_of_ballot_item, 'ballot_item_id': ballot_item_id, 'position_list': position_list, } return HttpResponse(json.dumps(json_data), content_type='application/json')
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_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'] # 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) # 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' 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 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_data_retrieved': election_data_retrieved, 'polling_location_retrieved': polling_location_retrieved, 'contests_retrieved': contests_retrieved, } return results
def voter_star_status_retrieve_for_api(voter_device_id, office_id, candidate_id, measure_id): # Get voter_id from the voter_device_id so we can know who is doing the starring results = is_voter_device_id_valid(voter_device_id) if not results["success"]: json_data = { "status": "VALID_VOTER_DEVICE_ID_MISSING", "success": False, "voter_device_id": voter_device_id, "is_starred": False, "office_id": office_id, "candidate_id": candidate_id, "measure_id": measure_id, } return HttpResponse(json.dumps(json_data), content_type="application/json") 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", "success": False, "voter_device_id": voter_device_id, "is_starred": False, "office_id": office_id, "candidate_id": candidate_id, "measure_id": measure_id, } return HttpResponse(json.dumps(json_data), content_type="application/json") star_item_manager = StarItemManager() if positive_value_exists(office_id): # Zero out the unused values star_item_id = 0 candidate_campaign_id = 0 contest_measure_id = 0 results = star_item_manager.retrieve_star_item( star_item_id, voter_id, office_id, candidate_campaign_id, contest_measure_id ) status = results["status"] success = results["success"] is_starred = results["is_starred"] elif positive_value_exists(candidate_id): # Zero out the unused values star_item_id = 0 contest_office_id = 0 contest_measure_id = 0 results = star_item_manager.retrieve_star_item( star_item_id, voter_id, contest_office_id, candidate_id, contest_measure_id ) status = results["status"] success = results["success"] is_starred = results["is_starred"] elif positive_value_exists(measure_id): # Zero out the unused values star_item_id = 0 contest_office_id = 0 candidate_campaign_id = 0 results = star_item_manager.retrieve_star_item( star_item_id, voter_id, contest_office_id, candidate_campaign_id, measure_id ) status = results["status"] success = results["success"] is_starred = results["is_starred"] else: status = "UNABLE_TO_SAVE-OFFICE_ID_AND_CANDIDATE_ID_AND_MEASURE_ID_MISSING" success = False is_starred = False json_data = { "status": status, "success": success, "voter_device_id": voter_device_id, "is_starred": is_starred, "office_id": office_id, "candidate_id": candidate_id, "measure_id": measure_id, } return HttpResponse(json.dumps(json_data), content_type="application/json")
def voter_retrieve_list_for_api(voter_device_id): results = is_voter_device_id_valid(voter_device_id) if not results['success']: results2 = { 'success': False, 'json_data': results['json_data'], } return results2 voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if voter_id > 0: voter_manager = VoterManager() results = voter_manager.retrieve_voter_by_id(voter_id) if results['voter_found']: voter_id = results['voter_id'] else: # If we are here, the voter_id could not be found from the voter_device_id json_data = { 'status': "VOTER_NOT_FOUND_FROM_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, } results = { 'success': False, 'json_data': json_data, } return results if voter_id: voter_list = Voter.objects.all() voter_list = voter_list.filter(id=voter_id) if len(voter_list): results = { 'success': True, 'voter_list': voter_list, } return results # Trying to mimic the Google Civic error codes scheme errors_list = [ { 'domain': "TODO global", 'reason': "TODO reason", 'message': "TODO Error message here", 'locationType': "TODO Error message here", 'location': "TODO location", } ] error_package = { 'errors': errors_list, 'code': 400, 'message': "Error message here", } json_data = { 'error': error_package, 'status': "VOTER_ID_COULD_NOT_BE_RETRIEVED", 'success': False, 'voter_device_id': voter_device_id, } results = { 'success': False, 'json_data': json_data, } return results
def voter_position_retrieve_for_api(voter_device_id, office_we_vote_id, candidate_we_vote_id, measure_we_vote_id): results = is_voter_device_id_valid(voter_device_id) if not results['success']: return HttpResponse(json.dumps(results['json_data']), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, } return HttpResponse(json.dumps(json_data), content_type='application/json') office_we_vote_id = office_we_vote_id.strip() candidate_we_vote_id = candidate_we_vote_id.strip() measure_we_vote_id = measure_we_vote_id.strip() if not positive_value_exists(office_we_vote_id) and \ not positive_value_exists(candidate_we_vote_id) and \ not positive_value_exists(measure_we_vote_id): json_data = { 'status': "POSITION_RETRIEVE_MISSING_AT_LEAST_ONE_BALLOT_ITEM_ID", 'success': False, 'position_id': 0, 'position_we_vote_id': '', 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': '', 'office_we_vote_id': '', 'candidate_we_vote_id': '', 'measure_we_vote_id': '', 'stance': '', 'statement_text': '', 'statement_html': '', 'more_info_url': '', 'last_updated': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') position_manager = PositionEnteredManager() if positive_value_exists(office_we_vote_id): results = position_manager.retrieve_voter_contest_office_position_with_we_vote_id( voter_id, office_we_vote_id) elif positive_value_exists(candidate_we_vote_id): results = position_manager.retrieve_voter_candidate_campaign_position_with_we_vote_id( voter_id, candidate_we_vote_id) elif positive_value_exists(measure_we_vote_id): results = position_manager.retrieve_voter_contest_measure_position_with_we_vote_id( voter_id, measure_we_vote_id) # retrieve_position results # results = { # 'error_result': error_result, # 'DoesNotExist': exception_does_not_exist, # 'MultipleObjectsReturned': exception_multiple_object_returned, # 'position_found': True if position_id > 0 else False, # 'position_id': position_id, # 'position': position_on_stage, # 'is_support': position_on_stage.is_support(), # 'is_oppose': position_on_stage.is_oppose(), # 'is_no_stance': position_on_stage.is_no_stance(), # 'is_information_only': position_on_stage.is_information_only(), # 'is_still_deciding': position_on_stage.is_still_deciding(), # } if results['position_found']: position = results['position'] json_data = { 'success': True, 'status': results['status'], 'position_id': position.id, 'position_we_vote_id': position.we_vote_id, 'is_support': results['is_support'], 'is_oppose': results['is_oppose'], 'is_information_only': results['is_information_only'], 'google_civic_election_id': position.google_civic_election_id, 'office_we_vote_id': position.contest_office_we_vote_id, 'candidate_we_vote_id': position.candidate_campaign_we_vote_id, 'measure_we_vote_id': position.contest_measure_we_vote_id, 'stance': position.stance, 'statement_text': position.statement_text, 'statement_html': position.statement_html, 'more_info_url': position.more_info_url, 'last_updated': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') else: json_data = { 'status': results['status'], 'success': False, 'position_id': 0, 'position_we_vote_id': '', 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': '', 'office_we_vote_id': '', 'candidate_we_vote_id': '', 'measure_we_vote_id': '', 'stance': '', 'statement_text': '', 'statement_html': '', 'more_info_url': '', 'last_updated': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def quick_info_text_save_for_api( # TODO to be converted voter_device_id, quick_info_id, quick_info_we_vote_id, google_civic_election_id, office_we_vote_id, candidate_we_vote_id, measure_we_vote_id, statement_text, statement_html ): results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data_from_results = results['json_data'] json_data = { 'status': json_data_from_results['status'], 'success': False, 'voter_device_id': voter_device_id, 'quick_info_id': quick_info_id, 'quick_info_we_vote_id': quick_info_we_vote_id, 'new_quick_info_created': False, 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': google_civic_election_id, 'office_we_vote_id': office_we_vote_id, 'candidate_we_vote_id': candidate_we_vote_id, 'measure_we_vote_id': measure_we_vote_id, 'statement_text': statement_text, 'statement_html': statement_html, 'last_updated': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_manager = VoterManager() voter_results = voter_manager.retrieve_voter_from_voter_device_id(voter_device_id) voter_id = voter_results['voter_id'] if not positive_value_exists(voter_id): json_data = { 'status': "VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'quick_info_id': quick_info_id, 'quick_info_we_vote_id': quick_info_we_vote_id, 'new_quick_info_created': False, 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': google_civic_election_id, 'office_we_vote_id': office_we_vote_id, 'candidate_we_vote_id': candidate_we_vote_id, 'measure_we_vote_id': measure_we_vote_id, 'statement_text': statement_text, 'statement_html': statement_html, 'last_updated': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') voter = voter_results['voter'] quick_info_id = convert_to_int(quick_info_id) quick_info_we_vote_id = quick_info_we_vote_id.strip() existing_unique_identifier_found = positive_value_exists(quick_info_id) \ or positive_value_exists(quick_info_we_vote_id) new_unique_identifier_found = positive_value_exists(voter_id) \ and positive_value_exists(google_civic_election_id) and ( positive_value_exists(office_we_vote_id) or positive_value_exists(candidate_we_vote_id) or positive_value_exists(measure_we_vote_id) ) unique_identifier_found = existing_unique_identifier_found or new_unique_identifier_found # We must have these variables in order to create a new entry required_variables_for_new_entry = positive_value_exists(voter_id) \ and positive_value_exists(google_civic_election_id) and ( positive_value_exists(office_we_vote_id) or positive_value_exists(candidate_we_vote_id) or positive_value_exists(measure_we_vote_id) ) if not unique_identifier_found: results = { 'status': "QUICK_INFO_REQUIRED_UNIQUE_IDENTIFIER_VARIABLES_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'quick_info_id': quick_info_id, 'quick_info_we_vote_id': quick_info_we_vote_id, 'new_quick_info_created': False, 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': google_civic_election_id, 'office_we_vote_id': office_we_vote_id, 'candidate_we_vote_id': candidate_we_vote_id, 'measure_we_vote_id': measure_we_vote_id, 'statement_text': statement_text, 'statement_html': statement_html, 'last_updated': '', } return results elif not existing_unique_identifier_found and not required_variables_for_new_entry: results = { 'status': "NEW_QUICK_INFO_REQUIRED_VARIABLES_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'quick_info_id': quick_info_id, 'quick_info_we_vote_id': quick_info_we_vote_id, 'new_quick_info_created': False, 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': google_civic_election_id, 'office_we_vote_id': office_we_vote_id, 'candidate_we_vote_id': candidate_we_vote_id, 'measure_we_vote_id': measure_we_vote_id, 'statement_text': statement_text, 'statement_html': statement_html, 'last_updated': '', } return results quick_info_manager = QuickInfoManager() save_results = quick_info_manager.update_or_create_quick_info( quick_info_id=quick_info_id, quick_info_we_vote_id=quick_info_we_vote_id, voter_we_vote_id=voter.we_vote_id, google_civic_election_id=google_civic_election_id, office_we_vote_id=office_we_vote_id, candidate_we_vote_id=candidate_we_vote_id, measure_we_vote_id=measure_we_vote_id, statement_text=statement_text, statement_html=statement_html, ) if save_results['success']: quick_info = save_results['quick_info'] results = { 'success': save_results['success'], 'status': save_results['status'], 'voter_device_id': voter_device_id, 'quick_info_id': quick_info.id, 'quick_info_we_vote_id': quick_info.we_vote_id, 'new_quick_info_created': save_results['new_quick_info_created'], 'is_support': quick_info.is_support(), 'is_oppose': quick_info.is_oppose(), 'is_information_only': quick_info.is_information_only(), 'google_civic_election_id': quick_info.google_civic_election_id, 'office_we_vote_id': quick_info.contest_office_we_vote_id, 'candidate_we_vote_id': quick_info.candidate_campaign_we_vote_id, 'measure_we_vote_id': quick_info.contest_measure_we_vote_id, 'statement_text': quick_info.statement_text, 'statement_html': quick_info.statement_html, 'last_updated': '', } return results else: results = { 'success': False, 'status': save_results['status'], 'voter_device_id': voter_device_id, 'quick_info_id': quick_info_id, 'quick_info_we_vote_id': quick_info_we_vote_id, 'new_quick_info_created': False, 'is_support': False, 'is_oppose': False, 'is_information_only': False, 'google_civic_election_id': google_civic_election_id, 'office_we_vote_id': office_we_vote_id, 'candidate_we_vote_id': candidate_we_vote_id, 'measure_we_vote_id': measure_we_vote_id, 'statement_text': statement_text, 'statement_html': statement_html, 'last_updated': '', } return results
def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): """ :param voter_device_id: :param google_civic_election_id: This variable either was stored in a cookie, or passed in explicitly so we can get the ballot items related to that election. :return: """ # 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']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_id': 0, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } results = { 'success': False, 'json_data': json_data, 'google_civic_election_id': 0, # Force the clearing of google_civic_election_id } return results 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", 'success': False, 'voter_id': voter_id, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } results = { 'success': False, 'json_data': json_data, 'google_civic_election_id': 0, # Force the clearing of google_civic_election_id } return results ballot_item_list_manager = BallotItemListManager() # If we get here without a google_civic_election_id, we need to choose one from the ballot items that are already # stored. If we proceed to retrieve_all_ballot_items_for_voter without a google_civic_election_id, we will get # ballot items from a variety of elections. # This logic looks for all of the elections we have ballot information for, and displays the most recent election # (not counting the test election) if not positive_value_exists(google_civic_election_id): google_civic_election_id = ballot_item_list_manager.fetch_most_recent_google_civic_election_id() # If an election id STILL wasn't found, then we probably don't have any ballot items stored locally, so we # need to go out to google civic. BUT we will proceed and attempt to retrieve ballot items without an election_id ballot_item_list = [] ballot_items_to_display = [] try: results = ballot_item_list_manager.retrieve_all_ballot_items_for_voter(voter_id, google_civic_election_id) success = results['success'] status = results['status'] ballot_item_list = results['ballot_item_list'] except Exception as e: status = 'FAILED voter_ballot_items_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for ballot_item in ballot_item_list: if ballot_item.contest_office_we_vote_id: kind_of_ballot_item = OFFICE ballot_item_id = ballot_item.contest_office_id we_vote_id = ballot_item.contest_office_we_vote_id elif ballot_item.contest_measure_we_vote_id: kind_of_ballot_item = MEASURE ballot_item_id = ballot_item.contest_measure_id we_vote_id = ballot_item.contest_measure_we_vote_id else: kind_of_ballot_item = '' if positive_value_exists(kind_of_ballot_item): one_ballot_item = { 'ballot_item_display_name': ballot_item.ballot_item_display_name, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'kind_of_ballot_item': kind_of_ballot_item, 'id': ballot_item_id, 'we_vote_id': we_vote_id, } ballot_items_to_display.append(one_ballot_item.copy()) json_data = { 'status': 'VOTER_BALLOT_ITEMS_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'ballot_item_list': ballot_items_to_display, 'google_civic_election_id': google_civic_election_id, } else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, # We want to save google_civic_election_id in cookie 'json_data': json_data, } return results
def position_like_count_for_api(voter_device_id, position_entered_id, limit_to_voters_network=False): # Get voter_id from the voter_device_id so we can know who is doing the liking results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'position_entered_id': position_entered_id, 'voter_network_likes': False, 'all_likes': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') 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", 'success': False, 'voter_device_id': voter_device_id, 'position_entered_id': position_entered_id, 'voter_network_likes': False, 'all_likes': False, } return HttpResponse(json.dumps(json_data), content_type='application/json') position_like_list_manager = PositionLikeListManager() type_of_count = False number_of_likes = False if positive_value_exists(position_entered_id): if limit_to_voters_network: if positive_value_exists(voter_id): results = position_like_list_manager.count_voter_network_position_likes(position_entered_id, voter_id) status = results['status'] success = results['success'] all_likes = False voter_network_likes = results['number_of_likes'] type_of_count = 'VOTER_NETWORK' number_of_likes = results['number_of_likes'] else: status = 'UNABLE_TO_RETRIEVE_VOTER_NETWORK_COUNT-VOTER_ID_MISSING' success = False all_likes = False voter_network_likes = False else: results = position_like_list_manager.count_all_position_likes(position_entered_id) status = results['status'] success = results['success'] all_likes = results['number_of_likes'] voter_network_likes = False type_of_count = 'ALL' number_of_likes = results['number_of_likes'] else: status = 'UNABLE_TO_RETRIEVE_COUNT-POSITION_ENTERED_ID_MISSING' success = False all_likes = False voter_network_likes = False json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'position_entered_id': position_entered_id, 'type_of_count': type_of_count, 'number_of_likes': number_of_likes, 'all_likes': all_likes, 'voter_network_likes': voter_network_likes, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_guides_to_follow_retrieve_for_api(voter_device_id, # voterGuidesToFollow kind_of_ballot_item='', ballot_item_we_vote_id='', google_civic_election_id=0, maximum_number_to_retrieve=0): # Get voter_id from the voter_device_id so we can figure out which voter_guides to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'ERROR_GUIDES_TO_FOLLOW_NO_VOTER_DEVICE_ID', 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results voter_guide_list = [] voter_guides = [] try: # results = voter_guide_list_object.retrieve_voter_guides_for_election(google_civic_election_id) if positive_value_exists(kind_of_ballot_item) and positive_value_exists(ballot_item_we_vote_id): results = retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] elif positive_value_exists(google_civic_election_id): results = retrieve_voter_guides_to_follow_by_election(voter_id, google_civic_election_id) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] else: success = False status = "NO_VOTER_GUIDES_FOUND-MISSING_REQUIRED_VARIABLES" except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve_for_api, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if success: number_added_to_list = 0 for voter_guide in voter_guide_list: one_voter_guide = { 'we_vote_id': voter_guide.we_vote_id, 'google_civic_election_id': voter_guide.google_civic_election_id, 'voter_guide_display_name': voter_guide.voter_guide_display_name(), 'voter_guide_image_url': voter_guide.voter_guide_image_url(), 'voter_guide_owner_type': voter_guide.voter_guide_owner_type, 'organization_we_vote_id': voter_guide.organization_we_vote_id, 'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id, 'owner_voter_id': voter_guide.owner_voter_id, 'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'), } voter_guides.append(one_voter_guide.copy()) if positive_value_exists(maximum_number_to_retrieve): number_added_to_list += 1 if number_added_to_list >= maximum_number_to_retrieve: break if len(voter_guides): json_data = { 'status': 'VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } else: json_data = { 'status': 'NO_VOTER_GUIDES_FOUND', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], 'google_civic_election_id': google_civic_election_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'maximum_number_to_retrieve': maximum_number_to_retrieve, } results = { 'success': False, 'google_civic_election_id': 0, # Force the reset of google_civic_election_id cookie 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'json_data': json_data, } return results