def retrieve_voter_guides_followed(voter_id): voter_guide_list_found = False follow_organization_list_manager = FollowOrganizationList() organization_we_vote_ids_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_we_vote_id_array(voter_id) voter_guide_list_object = VoterGuideList() results = voter_guide_list_object.retrieve_voter_guides_by_organization_list( organization_we_vote_ids_followed_by_voter) voter_guide_list = [] if results['voter_guide_list_found']: voter_guide_list = results['voter_guide_list'] status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_FOLLOWED' success = True if len(voter_guide_list): voter_guide_list_found = True else: status = results['status'] success = False results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def retrieve_voter_guides_followed(voter_id): # voterGuidesFollowedRetrieve voter_guide_list_found = False follow_organization_list_manager = FollowOrganizationList() organization_we_vote_ids_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_we_vote_id_array(voter_id) voter_guide_list_object = VoterGuideList() results = voter_guide_list_object.retrieve_voter_guides_by_organization_list( organization_we_vote_ids_followed_by_voter) voter_guide_list = [] if results['voter_guide_list_found']: voter_guide_list = results['voter_guide_list'] status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_FOLLOWED' success = True if len(voter_guide_list): voter_guide_list_found = True else: status = results['status'] success = False results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def retrieve_voter_guides_to_follow_generic(voter_id, search_string, maximum_number_to_retrieve=0, sort_by='', sort_order=''): """ Separate from an election or a ballot item, return a list of voter_guides the voter has not already followed :param voter_id: :param search_string: :param maximum_number_to_retrieve: :param sort_by: :param sort_order: :return: """ voter_guide_list_found = False # Start with orgs followed and ignored by this voter follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) organizations_ignored_by_voter = \ follow_organization_list_manager.retrieve_ignore_organization_by_voter_id_simple_id_array(voter_id) # This is a list of orgs that the voter is already following or ignoring organization_we_vote_ids_followed_or_ignored_by_voter = list( chain(organizations_followed_by_voter, organizations_ignored_by_voter)) voter_guide_list_manager = VoterGuideList() # First, retrieve the voter_guides stored by org and google_civic_election_id voter_guide_results = voter_guide_list_manager.retrieve_voter_guides_to_follow_generic( organization_we_vote_ids_followed_or_ignored_by_voter, search_string, maximum_number_to_retrieve, sort_by, sort_order) if voter_guide_results['voter_guide_list_found']: voter_guide_list = voter_guide_results['voter_guide_list'] else: voter_guide_list = [] status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_GENERIC' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def retrieve_voter_guides_to_follow_generic(voter_id, search_string, maximum_number_to_retrieve=0, sort_by='', sort_order=''): """ Separate from an election or a ballot item, return a list of voter_guides the voter has not already followed :param voter_id: :param search_string: :param maximum_number_to_retrieve: :param sort_by: :param sort_order: :return: """ voter_guide_list_found = False # Start with orgs followed and ignored by this voter follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) organizations_ignored_by_voter = \ follow_organization_list_manager.retrieve_ignore_organization_by_voter_id_simple_id_array(voter_id) # This is a list of orgs that the voter is already following or ignoring organization_we_vote_ids_followed_or_ignored_by_voter = list(chain(organizations_followed_by_voter, organizations_ignored_by_voter)) voter_guide_list_manager = VoterGuideList() # First, retrieve the voter_guides stored by org and google_civic_election_id voter_guide_results = voter_guide_list_manager.retrieve_voter_guides_to_follow_generic( organization_we_vote_ids_followed_or_ignored_by_voter, search_string, maximum_number_to_retrieve, sort_by, sort_order) if voter_guide_results['voter_guide_list_found']: voter_guide_list = voter_guide_results['voter_guide_list'] else: voter_guide_list = [] status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_GENERIC' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
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 len(voter_guide_list): # We want to order these voter guides by most twitter followers to least twitter followers # This serves as a rough indicator of the influence of the group voter_guide_list_manager = VoterGuideList() voter_guide_list = voter_guide_list_manager.reorder_voter_guide_list(voter_guide_list, 'twitter_followers_count', 'desc') 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, 'twitter_followers_count': voter_guide.twitter_followers_count, '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
def retrieve_voter_guides_to_follow_by_election_for_api( voter_id, google_civic_election_id, search_string, maximum_number_to_retrieve=0, sort_by='', sort_order=''): voter_guide_list_found = False # Start with orgs followed and ignored by this voter follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) organizations_ignored_by_voter = \ follow_organization_list_manager.retrieve_ignore_organization_by_voter_id_simple_id_array(voter_id) position_list_manager = PositionListManager() if positive_value_exists(google_civic_election_id): # This method finds all ballot_items in this election, and then retrieves *all* positions by any org or person # about each ballot_item. This will pick up We Vote positions or Vote Smart ratings, regardless of what time # period they were entered for. all_positions_list_for_election = position_list_manager.retrieve_all_positions_for_election( google_civic_election_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results positions_list_minus_ignored = position_list_manager.remove_positions_ignored_by_voter( all_positions_list_for_election, organizations_ignored_by_voter) positions_list_minus_ignored_and_followed = position_list_manager.calculate_positions_not_followed_by_voter( positions_list_minus_ignored, organizations_followed_by_voter) if not len(positions_list_minus_ignored_and_followed): # If no positions are found, exit voter_guide_list = [] results = { 'success': True, 'status': "NO_VOTER_GUIDES_FOUND_FOR_THIS_ELECTION", 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results # We want to retrieve an ordered list of organization_we_vote_id's (not followed or ignored) that have a position # in this election. For speed we only retrieve full voter_guide data for the limited list that we need voter_guide_list_manager = VoterGuideList() # This is a list of orgs that the voter isn't following or ignoring org_list_found_by_google_civic_election_id = [] for one_position in positions_list_minus_ignored_and_followed: # If the if positive_value_exists(one_position.organization_we_vote_id) and \ positive_value_exists(one_position.google_civic_election_id): # Make sure we haven't already recorded that we want to retrieve the voter_guide for this org if one_position.organization_we_vote_id in org_list_found_by_google_civic_election_id: continue org_list_found_by_google_civic_election_id.append( one_position.organization_we_vote_id) # First, retrieve the voter_guides stored by org and google_civic_election_id if positive_value_exists(len(org_list_found_by_google_civic_election_id)): voter_guide_results = voter_guide_list_manager.retrieve_voter_guides_to_follow_by_election( google_civic_election_id, org_list_found_by_google_civic_election_id, search_string, maximum_number_to_retrieve, sort_by, sort_order) if voter_guide_results['voter_guide_list_found']: voter_guide_list_from_election_id = voter_guide_results[ 'voter_guide_list'] else: voter_guide_list_from_election_id = [] else: voter_guide_list_from_election_id = [] # Second, retrieve the voter_guides stored by org & vote_smart_time_span # All positions were found above with position_list_manager.retrieve_all_positions_for_election # We give precedence to full voter guides from above, where we have an actual position of an org (as opposed to # Vote Smart ratings) maximum_number_of_guides_to_retrieve_by_time_span = \ maximum_number_to_retrieve - len(voter_guide_list_from_election_id) voter_guide_list = [] if positive_value_exists( maximum_number_of_guides_to_retrieve_by_time_span): org_list_found_by_time_span = [] orgs_we_need_found_by_position_and_time_span_list_of_dicts = [] for one_position in positions_list_minus_ignored_and_followed: # If this was a position found that was based on vote_smart_time_span... # (That is, ignore the positions already retrieved based on google_civic_election_id) if positive_value_exists(one_position.organization_we_vote_id) and \ positive_value_exists(one_position.vote_smart_time_span): # This shouldn't be possible, but we have it here for safety org_found_by_election_id_above = one_position.organization_we_vote_id in \ org_list_found_by_google_civic_election_id # If we already recorded that we want to look for this org under a different time span... org_found_by_different_time_span = one_position.organization_we_vote_id in \ org_list_found_by_time_span # Don't record that we want to look for a voter guide by this org we_vote_id or time span if org_found_by_election_id_above or org_found_by_different_time_span: continue org_list_found_by_time_span.append( one_position.organization_we_vote_id) one_position_dict = { 'organization_we_vote_id': one_position.organization_we_vote_id, 'vote_smart_time_span': one_position.vote_smart_time_span } orgs_we_need_found_by_position_and_time_span_list_of_dicts.append( one_position_dict) voter_guide_time_span_results = voter_guide_list_manager.retrieve_voter_guides_to_follow_by_time_span( orgs_we_need_found_by_position_and_time_span_list_of_dicts, search_string, maximum_number_of_guides_to_retrieve_by_time_span, sort_by, sort_order) if voter_guide_time_span_results['voter_guide_list_found']: voter_guide_list_from_time_span = voter_guide_time_span_results[ 'voter_guide_list'] else: voter_guide_list_from_time_span = [] # Merge these two lists # IFF we wanted to sort here: # voter_guide_list = sorted( # chain(voter_guide_list_from_election_id, voter_guide_list_from_time_span), # key=attrgetter(sort_by)) # But we don't, we just want to combine them with existing order voter_guide_list = list( chain(voter_guide_list_from_election_id, voter_guide_list_from_time_span)) status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_BY_ELECTION' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def voter_guides_to_follow_retrieve(voter_device_id, google_civic_election_id=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': [], } 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': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') # If the google_civic_election_id was found cached in a cookie and passed in, use that # If not, fetch it for this voter if not positive_value_exists(google_civic_election_id): google_civic_election_id = fetch_google_civic_election_id_for_voter_id(voter_id) voter_guide_list = [] voter_guides = [] try: voter_guide_list_object = VoterGuideList() results = voter_guide_list_object.retrieve_voter_guides_for_election(google_civic_election_id) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for voter_guide in voter_guide_list: one_voter_guide = { 'google_civic_election_id': voter_guide.google_civic_election_id, '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()) json_data = { 'status': 'VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, } else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], } return HttpResponse(json.dumps(json_data), content_type='application/json')
def retrieve_voter_guides_to_follow_by_election_for_api(voter_id, google_civic_election_id, search_string, maximum_number_to_retrieve=0, sort_by='', sort_order=''): voter_guide_list_found = False # Start with orgs followed and ignored by this voter follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) organizations_ignored_by_voter = \ follow_organization_list_manager.retrieve_ignore_organization_by_voter_id_simple_id_array(voter_id) position_list_manager = PositionListManager() if positive_value_exists(google_civic_election_id): # This method finds all ballot_items in this election, and then retrieves *all* positions by any org or person # about each ballot_item. This will pick up We Vote positions or Vote Smart ratings, regardless of what time # period they were entered for. all_positions_list_for_election = position_list_manager.retrieve_all_positions_for_election( google_civic_election_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results positions_list_minus_ignored = position_list_manager.remove_positions_ignored_by_voter( all_positions_list_for_election, organizations_ignored_by_voter) positions_list_minus_ignored_and_followed = position_list_manager.calculate_positions_not_followed_by_voter( positions_list_minus_ignored, organizations_followed_by_voter) if not len(positions_list_minus_ignored_and_followed): # If no positions are found, exit voter_guide_list = [] results = { 'success': True, 'status': "NO_VOTER_GUIDES_FOUND_FOR_THIS_ELECTION", 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results # We want to retrieve an ordered list of organization_we_vote_id's (not followed or ignored) that have a position # in this election. For speed we only retrieve full voter_guide data for the limited list that we need voter_guide_list_manager = VoterGuideList() # This is a list of orgs that the voter isn't following or ignoring org_list_found_by_google_civic_election_id = [] for one_position in positions_list_minus_ignored_and_followed: # If the if positive_value_exists(one_position.organization_we_vote_id) and \ positive_value_exists(one_position.google_civic_election_id): # Make sure we haven't already recorded that we want to retrieve the voter_guide for this org if one_position.organization_we_vote_id in org_list_found_by_google_civic_election_id: continue org_list_found_by_google_civic_election_id.append(one_position.organization_we_vote_id) # First, retrieve the voter_guides stored by org and google_civic_election_id if positive_value_exists(len(org_list_found_by_google_civic_election_id)): voter_guide_results = voter_guide_list_manager.retrieve_voter_guides_to_follow_by_election( google_civic_election_id, org_list_found_by_google_civic_election_id, search_string, maximum_number_to_retrieve, sort_by, sort_order) if voter_guide_results['voter_guide_list_found']: voter_guide_list_from_election_id = voter_guide_results['voter_guide_list'] else: voter_guide_list_from_election_id = [] else: voter_guide_list_from_election_id = [] # Second, retrieve the voter_guides stored by org & vote_smart_time_span # All positions were found above with position_list_manager.retrieve_all_positions_for_election # We give precedence to full voter guides from above, where we have an actual position of an org (as opposed to # Vote Smart ratings) maximum_number_of_guides_to_retrieve_by_time_span = \ maximum_number_to_retrieve - len(voter_guide_list_from_election_id) voter_guide_list = [] if positive_value_exists(maximum_number_of_guides_to_retrieve_by_time_span): org_list_found_by_time_span = [] orgs_we_need_found_by_position_and_time_span_list_of_dicts = [] for one_position in positions_list_minus_ignored_and_followed: # If this was a position found that was based on vote_smart_time_span... # (That is, ignore the positions already retrieved based on google_civic_election_id) if positive_value_exists(one_position.organization_we_vote_id) and \ positive_value_exists(one_position.vote_smart_time_span): # This shouldn't be possible, but we have it here for safety org_found_by_election_id_above = one_position.organization_we_vote_id in \ org_list_found_by_google_civic_election_id # If we already recorded that we want to look for this org under a different time span... org_found_by_different_time_span = one_position.organization_we_vote_id in \ org_list_found_by_time_span # Don't record that we want to look for a voter guide by this org we_vote_id or time span if org_found_by_election_id_above or org_found_by_different_time_span: continue org_list_found_by_time_span.append(one_position.organization_we_vote_id) one_position_dict = {'organization_we_vote_id': one_position.organization_we_vote_id, 'vote_smart_time_span': one_position.vote_smart_time_span} orgs_we_need_found_by_position_and_time_span_list_of_dicts.append(one_position_dict) voter_guide_time_span_results = voter_guide_list_manager.retrieve_voter_guides_to_follow_by_time_span( orgs_we_need_found_by_position_and_time_span_list_of_dicts, search_string, maximum_number_of_guides_to_retrieve_by_time_span, sort_by, sort_order) if voter_guide_time_span_results['voter_guide_list_found']: voter_guide_list_from_time_span = voter_guide_time_span_results['voter_guide_list'] else: voter_guide_list_from_time_span = [] # Merge these two lists # IFF we wanted to sort here: # voter_guide_list = sorted( # chain(voter_guide_list_from_election_id, voter_guide_list_from_time_span), # key=attrgetter(sort_by)) # But we don't, we just want to combine them with existing order voter_guide_list = list(chain(voter_guide_list_from_election_id, voter_guide_list_from_time_span)) status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_BY_ELECTION' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results