Пример #1
0
def 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):
    position_list_manager = PositionListManager()
    if show_positions_this_voter_follows:
        support_positions_followed = position_list_manager.calculate_positions_followed_by_voter(
            voter_id, support_positions_list_for_one_ballot_item, organizations_followed_by_voter)
        support_positions_count = len(support_positions_followed)

        oppose_positions_followed = position_list_manager.calculate_positions_followed_by_voter(
            voter_id, oppose_positions_list_for_one_ballot_item, organizations_followed_by_voter)
        oppose_positions_count = len(oppose_positions_followed)

    else:
        support_positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter(
            support_positions_list_for_one_ballot_item, organizations_followed_by_voter)
        support_positions_count = len(support_positions_not_followed)

        oppose_positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter(
            oppose_positions_list_for_one_ballot_item, organizations_followed_by_voter)
        oppose_positions_count = len(oppose_positions_not_followed)

    results = {
        'support_positions_count':  support_positions_count,
        'oppose_positions_count':   oppose_positions_count,
    }
    return results
Пример #2
0
def 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):
    position_list_manager = PositionListManager()
    if show_positions_this_voter_follows:
        support_positions_followed = position_list_manager.calculate_positions_followed_by_voter(
            voter_id, support_positions_list_for_one_ballot_item,
            organizations_followed_by_voter)
        support_positions_count = len(support_positions_followed)

        oppose_positions_followed = position_list_manager.calculate_positions_followed_by_voter(
            voter_id, oppose_positions_list_for_one_ballot_item,
            organizations_followed_by_voter)
        oppose_positions_count = len(oppose_positions_followed)

    else:
        support_positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter(
            support_positions_list_for_one_ballot_item,
            organizations_followed_by_voter)
        support_positions_count = len(support_positions_not_followed)

        oppose_positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter(
            oppose_positions_list_for_one_ballot_item,
            organizations_followed_by_voter)
        oppose_positions_count = len(oppose_positions_not_followed)

    results = {
        'support_positions_count': support_positions_count,
        'oppose_positions_count': oppose_positions_count,
    }
    return results
Пример #3
0
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
Пример #4
0
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
Пример #5
0
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
Пример #6
0
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
Пример #7
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.
        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
Пример #8
0
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
Пример #9
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
Пример #10
0
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