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_organizations_followed(voter_id): organization_list_found = False organization_list = [] follow_organization_list_manager = FollowOrganizationList() organization_ids_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) organization_list_manager = OrganizationListManager() results = organization_list_manager.retrieve_organizations_by_id_list(organization_ids_followed_by_voter) if results['organization_list_found']: organization_list = results['organization_list'] success = True if len(organization_list): organization_list_found = True status = 'SUCCESSFUL_RETRIEVE_OF_ORGANIZATIONS_FOLLOWED' else: status = 'ORGANIZATIONS_FOLLOWED_NOT_FOUND' else: status = results['status'] success = results['success'] results = { 'success': success, 'status': status, 'organization_list_found': organization_list_found, 'organization_list': organization_list, } return results
def retrieve_voter_guides_to_follow_generic_for_api(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 return_we_vote_id = True follow_organization_list_manager = FollowOrganizationList() if positive_value_exists(search_string): # If we are searching for organizations, we don't want to limit the search organization_we_vote_ids_followed_by_voter = [] organization_we_vote_ids_ignored_by_voter = [] else: organization_we_vote_ids_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id, return_we_vote_id) organization_we_vote_ids_ignored_by_voter = \ follow_organization_list_manager.retrieve_ignore_organization_by_voter_id_simple_id_array( voter_id, return_we_vote_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(organization_we_vote_ids_followed_by_voter, organization_we_vote_ids_ignored_by_voter)) voter_guide_list_manager = VoterGuideListManager() # 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 positions_count_for_candidate_campaign(voter_id, candidate_id, stance_we_are_looking_for, show_positions_this_voter_follows=True): """ We want to return a JSON file with the number of orgs, friends and public figures the voter follows who support this particular candidate's campaign """ # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListManager() candidate_we_vote_id = '' all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_id, candidate_we_vote_id, stance_we_are_looking_for) 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: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_followed_count = len(positions_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_FOLLOWED_RE_CANDIDATE', 'success': True, 'count': positions_followed_count, } results = { 'json_data': json_data, } return results else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED_CC', 'success': True, 'count': positions_not_followed_count, } results = { 'json_data': json_data, } return results
def positions_related_to_candidate_campaign_view(request, candidate_campaign_id, stance_we_are_looking_for): # TODO DEPRECATE """ We want to return a JSON file with the support positions for a particular candidate's campaign :param request: :param candidate_campaign_id: :return: """ if stance_we_are_looking_for not in(SUPPORT, NO_STANCE, INFORMATION_ONLY, STILL_DECIDING, OPPOSE, PERCENT_RATING): logger.debug(stance_we_are_looking_for) return JsonResponse({0: "stance not recognized"}) # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListManager() candidate_campaign_we_vote_id = '' all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, candidate_campaign_we_vote_id, stance_we_are_looking_for) voter_api_device_id = get_voter_api_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) # TODO: Below we return a snippet of HTML, but this should be converted to returning just the org's name # and id, so the "x, y, and z support" can be assembled and rendered by the client # VERSION 1 # position_html = assemble_candidate_campaign_position_stance_html( # all_positions_list_for_candidate_campaign, stance_we_are_looking_for, candidate_campaign_id) # VERSION 2 position_html = assemble_candidate_campaign_stance_html( candidate_campaign_id, stance_we_are_looking_for, positions_followed, positions_not_followed) return JsonResponse({0: position_html})
def positions_related_to_candidate_campaign_view(request, candidate_campaign_id, stance_we_are_looking_for): """ We want to return a JSON file with the support positions for a particular candidate's campaign :param request: :param candidate_campaign_id: :return: """ if stance_we_are_looking_for not in(SUPPORT, NO_STANCE, INFORMATION_ONLY, STILL_DECIDING, OPPOSE): logger.debug(stance_we_are_looking_for) return JsonResponse({0: "stance not recognized"}) # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) # TODO: Below we return a snippet of HTML, but this should be converted to returning just the org's name # and id, so the "x, y, and z support" can be assembled and rendered by the client # VERSION 1 # position_html = assemble_candidate_campaign_position_stance_html( # all_positions_list_for_candidate_campaign, stance_we_are_looking_for, candidate_campaign_id) # VERSION 2 position_html = assemble_candidate_campaign_stance_html( candidate_campaign_id, stance_we_are_looking_for, positions_followed, positions_not_followed) return JsonResponse({0: position_html})
def positions_display_list_related_to_candidate_campaign( request, candidate_campaign_id, stance_we_are_looking_for): show_only_followed_positions = convert_to_int(request.GET.get('f', 0)) show_only_not_followed_positions = convert_to_int(request.GET.get('nf', 0)) messages_on_stage = get_messages(request) candidate_campaign_id = convert_to_int(candidate_campaign_id) position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) if show_only_followed_positions == 1: logger.debug("positions_display_list: show only followed positions") list_to_display = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) elif show_only_not_followed_positions == 1: logger.debug( "positions_display_list: show only NOT followed positions") list_to_display = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) else: list_to_display = all_positions_list_for_candidate_campaign template_values = { 'error': True, 'messages_on_stage': messages_on_stage, 'position_list': list_to_display, 'organizations_followed_by_voter': organizations_followed_by_voter, } return render(request, 'position/position_list.html', template_values)
def positions_display_list_related_to_candidate_campaign(request, candidate_campaign_id, stance_we_are_looking_for): show_only_followed_positions = convert_to_int(request.GET.get('f', 0)) show_only_not_followed_positions = convert_to_int(request.GET.get('nf', 0)) messages_on_stage = get_messages(request) candidate_campaign_id = convert_to_int(candidate_campaign_id) position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) if show_only_followed_positions == 1: logger.debug("positions_display_list: show only followed positions") list_to_display = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) elif show_only_not_followed_positions == 1: logger.debug("positions_display_list: show only NOT followed positions") list_to_display = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) else: list_to_display = all_positions_list_for_candidate_campaign template_values = { 'error': True, 'messages_on_stage': messages_on_stage, 'position_list': list_to_display, 'organizations_followed_by_voter': organizations_followed_by_voter, } return render(request, 'position/position_list.html', template_values)
def positions_count_for_candidate_campaign_view(request, candidate_campaign_id, stance_we_are_looking_for, show_followed_positions=True): """ We want to return a JSON file with the support positions for a particular candidate's campaign :param request: :param candidate_campaign_id: :return: """ if stance_we_are_looking_for not in(ANY, SUPPORT, NO_STANCE, INFORMATION_ONLY, STILL_DECIDING, OPPOSE): logger.debug(stance_we_are_looking_for) return JsonResponse({0: "stance not recognized"}) # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) if show_followed_positions: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_followed_count = len(positions_followed) return JsonResponse({0: positions_followed_count}) else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) return JsonResponse({0: positions_not_followed_count})
def positions_count_for_candidate_campaign_view(request, candidate_campaign_id, stance_we_are_looking_for, show_followed_positions=True): """ We want to return a JSON file with the support positions for a particular candidate's campaign :param request: :param candidate_campaign_id: :return: """ if stance_we_are_looking_for not in(ANY, SUPPORT, NO_STANCE, INFORMATION_ONLY, STILL_DECIDING, OPPOSE): logger.debug(stance_we_are_looking_for) return JsonResponse({0: "stance not recognized"}) # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListForCandidateCampaign() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_campaign_id, stance_we_are_looking_for) voter_device_id = get_voter_device_id(request) voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_info_for_voter_simple_array(voter_id) if show_followed_positions: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_followed_count = len(positions_followed) return JsonResponse({0: positions_followed_count}) else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) return JsonResponse({0: positions_not_followed_count})
def positions_count_for_candidate_campaign( voter_id, candidate_id, candidate_we_vote_id, stance_we_are_looking_for, show_positions_this_voter_follows=True): """ We want to return a JSON file with the number of orgs, friends and public figures the voter follows who support this particular candidate's campaign """ # This implementation is built to make limited database calls. We do as many calculations as we can here in the # application layer position_list_manager = PositionListManager() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_id, candidate_we_vote_id, stance_we_are_looking_for) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the candidate object # so we make sure we have both of these values to return if positive_value_exists(candidate_id): candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_id( candidate_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] candidate_we_vote_id = candidate_campaign.we_vote_id elif positive_value_exists(candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id( candidate_we_vote_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] candidate_id = candidate_campaign.id if show_positions_this_voter_follows: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_followed_count = len(positions_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_FOLLOWED_RE_CANDIDATE', 'success': True, 'count': positions_followed_count, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } results = { 'json_data': json_data, } return results else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED_CC', 'success': True, 'count': positions_not_followed_count, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } results = { 'json_data': json_data, } 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
def retrieve_voter_guides_to_follow_generic_for_api( 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 return_we_vote_id = True follow_organization_list_manager = FollowOrganizationList() if positive_value_exists(search_string): # If we are searching for organizations, we don't want to limit the search organization_we_vote_ids_followed_by_voter = [] organization_we_vote_ids_ignored_by_voter = [] else: organization_we_vote_ids_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id, return_we_vote_id) organization_we_vote_ids_ignored_by_voter = \ follow_organization_list_manager.retrieve_ignore_organization_by_voter_id_simple_id_array( voter_id, return_we_vote_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(organization_we_vote_ids_followed_by_voter, organization_we_vote_ids_ignored_by_voter)) voter_guide_list_manager = VoterGuideListManager() # 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 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 positions_count_for_candidate_campaign(voter_id, candidate_id, candidate_we_vote_id, stance_we_are_looking_for, show_positions_this_voter_follows=True): """ We want to return a JSON file with the number of orgs, friends and public figures the voter follows who support this particular candidate's campaign """ # This implementation is built to make limited database calls. We do as many calculations as we can here in the # application layer position_list_manager = PositionListManager() all_positions_list_for_candidate_campaign = \ position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_id, candidate_we_vote_id, stance_we_are_looking_for) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the candidate object # so we make sure we have both of these values to return if positive_value_exists(candidate_id): candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_id(candidate_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] candidate_we_vote_id = candidate_campaign.we_vote_id elif positive_value_exists(candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(candidate_we_vote_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] candidate_id = candidate_campaign.id if show_positions_this_voter_follows: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_followed_count = len(positions_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_FOLLOWED_RE_CANDIDATE', 'success': True, 'count': positions_followed_count, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } results = { 'json_data': json_data, } return results else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_candidate_campaign, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED_CC', 'success': True, 'count': positions_not_followed_count, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } results = { 'json_data': json_data, } 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
def retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string): voter_guide_list_found = False position_list_manager = PositionListManager() if (kind_of_ballot_item == CANDIDATE) and positive_value_exists(ballot_item_we_vote_id): candidate_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign( candidate_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == MEASURE) and positive_value_exists(ballot_item_we_vote_id): measure_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure( measure_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == OFFICE) and positive_value_exists(ballot_item_we_vote_id): office_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office( office_id, ballot_item_we_vote_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'search_string': search_string, 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_list = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) voter_guide_list = [] # Cycle through the positions held by groups that you don't currently follow voter_guide_manager = VoterGuideManager() for one_position in positions_list: if positive_value_exists(one_position.organization_we_vote_id): if one_position.google_civic_election_id: results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=one_position. google_civic_election_id, vote_smart_time_span=None, organization_we_vote_id=one_position. organization_we_vote_id) else: # vote_smart_time_span results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=0, vote_smart_time_span=one_position.vote_smart_time_span, organization_we_vote_id=one_position. organization_we_vote_id) elif positive_value_exists(one_position.public_figure_we_vote_id): results['voter_guide_found'] = False elif positive_value_exists(one_position.voter_we_vote_id): results['voter_guide_found'] = False else: results['voter_guide_found'] = False if results['voter_guide_found']: voter_guide = results['voter_guide'] # If we passed in search_string, make sure they are in this entry. # If they aren't, don't return voter guide if positive_value_exists(search_string): search_string = str( search_string) # Make sure search_string is a string twitter_handle = str(voter_guide.twitter_handle) display_name = str(voter_guide.display_name) if search_string.lower() in twitter_handle.lower( ) or search_string.lower() in display_name.lower(): voter_guide_list.append(voter_guide) else: voter_guide_list.append(voter_guide) status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_BY_BALLOT_ITEM' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'search_string': search_string, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def retrieve_voter_guides_to_follow_by_election(voter_id, google_civic_election_id): voter_guide_list_found = False position_list_manager = PositionListManager() if positive_value_exists(google_civic_election_id): all_positions_list = 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 follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_list = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) voter_guide_list = [] # Cycle through the positions held by groups that you don't currently follow voter_guide_manager = VoterGuideManager() for one_position in positions_list: if positive_value_exists(one_position.organization_we_vote_id): if one_position.google_civic_election_id: results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=one_position.google_civic_election_id, vote_smart_time_span=None, organization_we_vote_id=one_position.organization_we_vote_id) else: # vote_smart_time_span results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=0, vote_smart_time_span=one_position.vote_smart_time_span, organization_we_vote_id=one_position.organization_we_vote_id) elif positive_value_exists(one_position.public_figure_we_vote_id): results['voter_guide_found'] = False elif positive_value_exists(one_position.voter_we_vote_id): results['voter_guide_found'] = False else: results['voter_guide_found'] = False if results['voter_guide_found']: voter_guide_list.append(results['voter_guide']) status = 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED' 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_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 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. public_only = True # Do not return positions that are from friends only since we only want public positions all_positions_list_for_election = position_list_manager.retrieve_all_positions_for_election( google_civic_election_id, ANY_STANCE, public_only) 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 = VoterGuideListManager() # 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 retrieve_voter_guides_to_follow_by_ballot_item(voter_id, kind_of_ballot_item, ballot_item_we_vote_id, search_string): voter_guide_list_found = False retrieve_public_positions = True # The alternate is positions for friends-only. Since this method returns positions # to follow, we never need to return friend's positions here position_list_manager = PositionListManager() if (kind_of_ballot_item == CANDIDATE) and positive_value_exists(ballot_item_we_vote_id): candidate_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign( retrieve_public_positions, candidate_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == MEASURE) and positive_value_exists(ballot_item_we_vote_id): measure_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure( retrieve_public_positions, measure_id, ballot_item_we_vote_id, ANY_STANCE) elif (kind_of_ballot_item == OFFICE) and positive_value_exists(ballot_item_we_vote_id): office_id = 0 all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office( office_id, ballot_item_we_vote_id, ANY_STANCE) else: voter_guide_list = [] results = { 'success': False, 'status': "VOTER_GUIDES_BALLOT_RELATED_VARIABLES_MISSING", 'search_string': search_string, 'voter_guide_list_found': False, 'voter_guide_list': voter_guide_list, } return results follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) positions_list = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list, organizations_followed_by_voter) voter_guide_list = [] # Cycle through the positions held by groups that you don't currently follow voter_guide_manager = VoterGuideManager() for one_position in positions_list: if positive_value_exists(one_position.organization_we_vote_id): if one_position.google_civic_election_id: results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=one_position.google_civic_election_id, vote_smart_time_span=None, organization_we_vote_id=one_position.organization_we_vote_id) else: # vote_smart_time_span results = voter_guide_manager.retrieve_voter_guide( voter_guide_id=0, google_civic_election_id=0, vote_smart_time_span=one_position.vote_smart_time_span, organization_we_vote_id=one_position.organization_we_vote_id) elif positive_value_exists(one_position.public_figure_we_vote_id): results['voter_guide_found'] = False elif positive_value_exists(one_position.voter_we_vote_id): results['voter_guide_found'] = False else: results['voter_guide_found'] = False if results['voter_guide_found']: voter_guide = results['voter_guide'] # If we passed in search_string, make sure they are in this entry. # If they aren't, don't return voter guide if positive_value_exists(search_string): search_string = str(search_string) # Make sure search_string is a string twitter_handle = str(voter_guide.twitter_handle) display_name = str(voter_guide.display_name) if search_string.lower() in twitter_handle.lower() or search_string.lower() in display_name.lower(): voter_guide_list.append(voter_guide) else: voter_guide_list.append(voter_guide) status = 'SUCCESSFUL_RETRIEVE_OF_VOTER_GUIDES_BY_BALLOT_ITEM' success = True if len(voter_guide_list): voter_guide_list_found = True results = { 'success': success, 'status': status, 'search_string': search_string, 'voter_guide_list_found': voter_guide_list_found, 'voter_guide_list': voter_guide_list, } return results
def position_list_for_ballot_item_for_api(voter_device_id, # positionListForBallotItem office_id, office_we_vote_id, candidate_id, candidate_we_vote_id, measure_id, measure_we_vote_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 """ position_manager = PositionEnteredManager() # 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() ballot_item_found = False if positive_value_exists(candidate_id) or positive_value_exists(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 # Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions # for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of # the WebApp team) candidate_campaign_manager = CandidateCampaignManager() if positive_value_exists(candidate_id): results = candidate_campaign_manager.retrieve_candidate_campaign_from_id(candidate_id) else: results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(candidate_we_vote_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] ballot_item_id = candidate_campaign.id ballot_item_we_vote_id = candidate_campaign.we_vote_id ballot_item_found = True else: ballot_item_id = candidate_id ballot_item_we_vote_id = candidate_we_vote_id elif positive_value_exists(measure_id) or positive_value_exists(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 # Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions # for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of # the WebApp team) contest_measure_manager = ContestMeasureManager() if positive_value_exists(measure_id): results = contest_measure_manager.retrieve_contest_measure_from_id(measure_id) else: results = contest_measure_manager.retrieve_contest_measure_from_we_vote_id(measure_we_vote_id) if results['contest_measure_found']: contest_measure = results['contest_measure'] ballot_item_id = contest_measure.id ballot_item_we_vote_id = contest_measure.we_vote_id ballot_item_found = True else: ballot_item_id = measure_id ballot_item_we_vote_id = measure_we_vote_id elif positive_value_exists(office_id) or positive_value_exists(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 # Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions # for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of # the WebApp team) contest_office_manager = ContestOfficeManager() if positive_value_exists(office_id): results = contest_office_manager.retrieve_contest_office_from_id(office_id) else: results = contest_office_manager.retrieve_contest_office_from_we_vote_id(office_we_vote_id) if results['contest_office_found']: contest_office = results['contest_office'] ballot_item_id = contest_office.id ballot_item_we_vote_id = contest_office.we_vote_id ballot_item_found = True else: ballot_item_id = office_id ballot_item_we_vote_id = office_we_vote_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, 'ballot_item_we_vote_id': '', 'position_list': position_list, } return HttpResponse(json.dumps(json_data), content_type='application/json') if not ballot_item_found: position_list = [] json_data = { 'status': 'POSITION_LIST_RETRIEVE_BALLOT_ITEM_NOT_FOUND', 'success': False, 'count': 0, 'kind_of_ballot_item': "UNKNOWN", 'ballot_item_id': ballot_item_id, 'ballot_item_we_vote_id': ballot_item_we_vote_id, '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 # Make sure we have this data to display if not positive_value_exists(one_position.speaker_display_name) \ or not positive_value_exists(one_position.speaker_image_url_https): one_position = position_manager.refresh_cached_position_info(one_position) 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 # Make sure we have this data to display if not positive_value_exists(one_position.speaker_display_name): one_position = position_manager.refresh_cached_position_info(one_position) 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 # Make sure we have this data to display if not positive_value_exists(one_position.speaker_display_name) \ or not positive_value_exists(one_position.speaker_image_url_https): one_position = position_manager.refresh_cached_position_info(one_position) 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, 'ballot_item_display_name': one_position.ballot_item_display_name, 'speaker_display_name': one_position.speaker_display_name, 'speaker_image_url_https': one_position.speaker_image_url_https, '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(), 'vote_smart_rating': one_position.vote_smart_rating, 'vote_smart_time_span': one_position.vote_smart_time_span, 'last_updated': one_position.last_updated(), } 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, 'ballot_item_we_vote_id': ballot_item_we_vote_id, 'position_list': position_list, } return HttpResponse(json.dumps(json_data), content_type='application/json')