Ejemplo n.º 1
0
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
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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