Beispiel #1
0
def voter_ballot_items_retrieve_view(request):
    """
    (voterBallotItemsRetrieve) Request a skeleton of ballot data for this voter location,
    so that the web_app has all of the ids it needs to make more requests for data about each ballot item.
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    # If passed in, we want to look at
    google_civic_election_id = request.GET.get('google_civic_election_id', 0)
    use_test_election = request.GET.get('use_test_election', False)
    use_test_election = False if use_test_election == 'false' else use_test_election
    use_test_election = False if use_test_election == 'False' else use_test_election

    if positive_value_exists(use_test_election):
        google_civic_election_id = 2000  # The Google Civic API Test election
    elif not positive_value_exists(google_civic_election_id):
        # We look in the cookies for google_civic_election_id
        google_civic_election_id = get_google_civic_election_id_from_cookie(request)

    # This 'voter_ballot_items_retrieve_for_api' lives in apis_v1/controllers.py
    results = voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id)
    response = HttpResponse(json.dumps(results['json_data']), content_type='application/json')

    # Save google_civic_election_id in the cookie so the interface knows
    google_civic_election_id_from_ballot_retrieve = results['google_civic_election_id']
    if positive_value_exists(google_civic_election_id_from_ballot_retrieve):
        set_google_civic_election_id_cookie(request, response, results['google_civic_election_id'])

    return response
Beispiel #2
0
def voter_position_retrieve_view(request):
    """
    Retrieve all of the details about a single position based on unique identifier
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    kind_of_ballot_item = request.GET.get('kind_of_ballot_item', "")
    # ballot_item_id = request.GET.get('ballot_item_id', 0)
    ballot_item_we_vote_id = request.GET.get('ballot_item_we_vote_id', None)
    if kind_of_ballot_item == OFFICE:
        office_we_vote_id = ballot_item_we_vote_id
        candidate_we_vote_id = ''
        measure_we_vote_id = ''
    elif kind_of_ballot_item == CANDIDATE:
        office_we_vote_id = ''
        candidate_we_vote_id = ballot_item_we_vote_id
        measure_we_vote_id = ''
    elif kind_of_ballot_item == MEASURE:
        office_we_vote_id = ''
        candidate_we_vote_id = ''
        measure_we_vote_id = ballot_item_we_vote_id
    else:
        office_we_vote_id = ''
        candidate_we_vote_id = ''
        measure_we_vote_id = ''
    return voter_position_retrieve_for_api(
        voter_device_id=voter_device_id,
        office_we_vote_id=office_we_vote_id,
        candidate_we_vote_id=candidate_we_vote_id,
        measure_we_vote_id=measure_we_vote_id
    )
Beispiel #3
0
def voter_stop_asking_candidate_campaign_view(request, candidate_campaign_id):
    print "voter_stop_asking_candidate_campaign_view {candidate_campaign_id}".format(
        candidate_campaign_id=candidate_campaign_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    return JsonResponse({0: "not working yet - needs to be built"})
Beispiel #4
0
def voter_stop_asking_candidate_campaign_view(request, candidate_campaign_id):
    print "voter_stop_asking_candidate_campaign_view {candidate_campaign_id}".format(
        candidate_campaign_id=candidate_campaign_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    return JsonResponse({0: "not working yet - needs to be built"})
Beispiel #5
0
def voter_star_status_retrieve_view(request):
    """
    Retrieve whether or not a star is marked for an office, candidate or measure based on unique identifier
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    kind_of_ballot_item = request.GET.get('kind_of_ballot_item', "")
    ballot_item_id = request.GET.get('ballot_item_id', 0)
    # ballot_item_we_vote_id = request.GET.get('ballot_item_we_vote_id', None)
    if kind_of_ballot_item == OFFICE:
        office_id = ballot_item_id
        candidate_id = 0
        measure_id = 0
    elif kind_of_ballot_item == CANDIDATE:
        office_id = 0
        candidate_id = ballot_item_id
        measure_id = 0
    elif kind_of_ballot_item == MEASURE:
        office_id = 0
        candidate_id = 0
        measure_id = ballot_item_id
    else:
        office_id = 0
        candidate_id = 0
        measure_id = 0
    return voter_star_status_retrieve_for_api(
        voter_device_id=voter_device_id,
        office_id=office_id,
        candidate_id=candidate_id,
        measure_id=measure_id,
    )
Beispiel #6
0
def voter_position_comment_save_view(request):
    """
    Save a single position
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    position_id = request.POST.get('position_id', False)
    position_we_vote_id = request.POST.get('position_we_vote_id', False)
    google_civic_election_id = request.POST.get('google_civic_election_id', False)
    office_we_vote_id = request.POST.get('office_we_vote_id', False)
    candidate_we_vote_id = request.POST.get('candidate_we_vote_id', False)
    measure_we_vote_id = request.POST.get('measure_we_vote_id', False)
    statement_text = request.POST.get('statement_text', False)
    statement_html = request.POST.get('statement_html', False)

    results = voter_position_comment_save_for_api(
        voter_device_id=voter_device_id,
        position_id=position_id,
        position_we_vote_id=position_we_vote_id,
        google_civic_election_id=google_civic_election_id,
        office_we_vote_id=office_we_vote_id,
        candidate_we_vote_id=candidate_we_vote_id,
        measure_we_vote_id=measure_we_vote_id,
        statement_text=statement_text,
        statement_html=statement_html,
    )

    return HttpResponse(json.dumps(results), content_type='application/json')
Beispiel #7
0
def organization_follow_ignore_api_view(request):
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    try:
        organization_id = request.GET['organization_id']
    except KeyError:
        organization_id = 0
    return organization_follow_ignore(voter_device_id=voter_device_id, organization_id=organization_id)
Beispiel #8
0
def voter_star_on_save_view(request):
    """
    Mark the star for a single measure, office or candidate for one voter (voterStarOnSave)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    kind_of_ballot_item = request.GET.get('kind_of_ballot_item', "")
    ballot_item_id = request.GET.get('ballot_item_id', 0)
    # ballot_item_we_vote_id = request.GET.get('ballot_item_we_vote_id', None)
    if kind_of_ballot_item == OFFICE:
        office_id = ballot_item_id
        candidate_id = 0
        measure_id = 0
    elif kind_of_ballot_item == CANDIDATE:
        office_id = 0
        candidate_id = ballot_item_id
        measure_id = 0
    elif kind_of_ballot_item == MEASURE:
        office_id = 0
        candidate_id = 0
        measure_id = ballot_item_id
    else:
        office_id = 0
        candidate_id = 0
        measure_id = 0
    return voter_star_on_save_for_api(
        voter_device_id=voter_device_id, office_id=office_id, candidate_id=candidate_id, measure_id=measure_id)
Beispiel #9
0
def voter_address_retrieve_view(request):
    """
    Retrieve an address for this voter so we can figure out which ballot to display
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    return voter_address_retrieve(voter_device_id)
Beispiel #10
0
def voter_address_retrieve_view(request):
    """
    Retrieve an address for this voter so we can figure out which ballot to display
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(
        request)  # We look in the cookies for voter_device_id
    return voter_address_retrieve(voter_device_id)
Beispiel #11
0
def voter_position_like_on_save_view(request):
    """
    Mark the position_like for a single position for one voter (voterPositionLikeOnSave)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    position_entered_id = request.GET.get('position_entered_id', 0)
    return voter_position_like_on_save_for_api(
        voter_device_id=voter_device_id, position_entered_id=position_entered_id)
Beispiel #12
0
def voter_guide_possibility_save_view(request):
    """
    Save a website that may contain a voter guide (voterGuidePossibilitySave)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    voter_guide_possibility_url = request.POST.get('voter_guide_possibility_url', '')
    return voter_guide_possibility_save_for_api(voter_device_id=voter_device_id,
                                                voter_guide_possibility_url=voter_guide_possibility_url)
Beispiel #13
0
def voter_position_like_status_retrieve_view(request):
    """
    Retrieve whether or not a position_like is marked for position (voterPositionLikeStatusRetrieve)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    position_entered_id = request.GET.get('position_entered_id', 0)
    return voter_position_like_status_retrieve_for_api(
        voter_device_id=voter_device_id, position_entered_id=position_entered_id)
Beispiel #14
0
def voter_guides_to_follow_retrieve_view(request):
    """
    Retrieve a list of voter_guides that a voter might want to follow
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    # We look in the cookies for google_civic_election_id
    google_civic_election_id = get_google_civic_election_id_from_cookie(request)
    return voter_guides_to_follow_retrieve(voter_device_id, google_civic_election_id)
Beispiel #15
0
def voter_stop_asking_candidate_campaign_view(request, candidate_campaign_id):
    logger.debug("voter_stop_asking_candidate_campaign_view {candidate_campaign_id}".format(
        candidate_campaign_id=candidate_campaign_id
    ))
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    logger.debug("voter_stop_asking_candidate_campaign_view NOT BUILT YET, voter_id: {voter_id}".format(
        voter_id=voter_id
    ))

    return JsonResponse({0: "not working yet - needs to be built"})
Beispiel #16
0
def voter_stop_asking_candidate_campaign_view(request, candidate_campaign_id):
    logger.debug(
        "voter_stop_asking_candidate_campaign_view {candidate_campaign_id}".
        format(candidate_campaign_id=candidate_campaign_id))
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    logger.debug(
        "voter_stop_asking_candidate_campaign_view NOT BUILT YET, voter_id: {voter_id}"
        .format(voter_id=voter_id))

    return JsonResponse({0: "not working yet - needs to be built"})
Beispiel #17
0
def voter_supporting_candidate_campaign_view(request, candidate_campaign_id):
    # print "voter_supporting_candidate_campaign_view {candidate_campaign_id}".format(
    #     candidate_campaign_id=candidate_campaign_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    position_entered_manager = PositionEnteredManager()
    results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign(voter_id, candidate_campaign_id)
    if results['success']:
        return JsonResponse({0: "success"})
    else:
        return JsonResponse({0: "failure"})
Beispiel #18
0
def position_like_count_view(request):
    """
    Retrieve the total number of Likes that a position has received, either from the perspective of the voter's
    network of friends, or the entire network. (positionLikeCount)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    position_entered_id = request.GET.get('position_entered_id', 0)
    limit_to_voters_network = request.GET.get('limit_to_voters_network', False)
    return position_like_count_for_api(voter_device_id=voter_device_id, position_entered_id=position_entered_id,
                                       limit_to_voters_network=limit_to_voters_network)
Beispiel #19
0
def organization_unfollow_view(request, organization_id):
    print "organization_unfollow_view {organization_id}".format(
        organization_id=organization_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    follow_organization_manager = FollowOrganizationManager()
    results = follow_organization_manager.toggle_off_voter_following_organization(voter_id, organization_id)
    if results['success']:
        return JsonResponse({0: "success"})
    else:
        return JsonResponse({0: "failure"})
Beispiel #20
0
def organization_unfollow_view(request, organization_id):
    logger.debug("organization_unfollow_view {organization_id}".format(
        organization_id=organization_id))
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    follow_organization_manager = FollowOrganizationManager()
    results = follow_organization_manager.toggle_off_voter_following_organization(
        voter_id, organization_id)
    if results['success']:
        return JsonResponse({0: "success"})
    else:
        return JsonResponse({0: "failure"})
Beispiel #21
0
def voter_opposing_candidate_campaign_view(request, candidate_campaign_id):
    # print "voter_opposing_candidate_campaign_view {candidate_campaign_id}".format(
    #     candidate_campaign_id=candidate_campaign_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    position_entered_manager = PositionEnteredManager()
    results = position_entered_manager.toggle_on_voter_oppose_for_candidate_campaign(
        voter_id, candidate_campaign_id)
    if results['success']:
        return JsonResponse({0: "success"})
    else:
        return JsonResponse({0: "failure"})
Beispiel #22
0
def position_retrieve_view(request):
    """
    Retrieve all of the details about a single position based on unique identifier
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    position_id = request.GET.get('position_id', 0)
    position_we_vote_id = request.GET.get('position_we_vote_id', '')
    return position_retrieve_for_api(
        position_id=position_id,
        position_we_vote_id=position_we_vote_id,
        voter_device_id=voter_device_id
    )
Beispiel #23
0
    def get(self, request):  # Removed: , format=None
        voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
        results = voter_retrieve_list(voter_device_id)

        if 'success' not in results:
            json_data = results['json_data']
            return HttpResponse(json.dumps(json_data), content_type='application/json')
        elif not results['success']:
            json_data = results['json_data']
            return HttpResponse(json.dumps(json_data), content_type='application/json')
        else:
            voter_list = results['voter_list']
            serializer = VoterSerializer(voter_list, many=True)
            return Response(serializer.data)
Beispiel #24
0
def voter_address_save_view(request):
    """
    Save or update an address for this voter
    :param request:
    :return:
    """

    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    try:
        address = request.POST['address']
        address_variable_exists = True
    except KeyError:
        address = ''
        address_variable_exists = False
    return voter_address_save(voter_device_id, address, address_variable_exists)
Beispiel #25
0
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):
        print 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})
Beispiel #26
0
def my_ballot_view(request):
    generate_voter_device_id_if_needed = True
    voter_device_id = get_voter_device_id(request,
                                          generate_voter_device_id_if_needed)
    voter_id_found = False

    voter_device_link_manager = VoterDeviceLinkManager()
    results = voter_device_link_manager.retrieve_voter_device_link_from_voter_device_id(
        voter_device_id)
    if results['voter_device_link_found']:
        voter_device_link = results['voter_device_link']
        voter_id_found = True if voter_device_link.voter_id > 0 else False

    # If existing voter not found, create a new voter
    if not voter_id_found:
        # Create a new voter and return the id
        voter_manager = VoterManager()
        results = voter_manager.create_voter()

        if results['voter_created']:
            voter = results['voter']

            # Now save the voter_device_link
            results = voter_device_link_manager.save_new_voter_device_link(
                voter_device_id, voter.id)

            if results['voter_device_link_created']:
                voter_device_link = results['voter_device_link']
                voter_id_found = True if voter_device_link.voter_id > 0 else False

    if not voter_id_found:
        print "Voter ID not found, nor generated. This should not be possible. (Cookies may be turned off?)"
        ballot_item_list = []
    else:
        ballot_item_manager = BallotItemManager()
        results = ballot_item_manager.retrieve_all_ballot_items_for_voter(
            voter_device_link.voter_id)
        ballot_item_list = results['ballot_item_list']

    template_values = {
        'ballot_item_list': ballot_item_list,
    }
    response = render(request, 'ux_oak/my_ballot.html', template_values)

    set_voter_device_id(request, response, voter_device_id)
    return response
Beispiel #27
0
    def get(self, request):  # Removed: , format=None
        voter_device_id = get_voter_device_id(
            request)  # We look in the cookies for voter_device_id
        results = voter_retrieve_list(voter_device_id)

        if 'success' not in results:
            json_data = results['json_data']
            return HttpResponse(json.dumps(json_data),
                                content_type='application/json')
        elif not results['success']:
            json_data = results['json_data']
            return HttpResponse(json.dumps(json_data),
                                content_type='application/json')
        else:
            voter_list = results['voter_list']
            serializer = VoterSerializer(voter_list, many=True)
            return Response(serializer.data)
Beispiel #28
0
def voter_address_save_view(request):
    """
    Save or update an address for this voter
    :param request:
    :return:
    """

    voter_device_id = get_voter_device_id(
        request)  # We look in the cookies for voter_device_id
    try:
        address = request.POST['address']
        address_variable_exists = True
    except KeyError:
        address = ''
        address_variable_exists = False
    return voter_address_save(voter_device_id, address,
                              address_variable_exists)
Beispiel #29
0
def voter_ballot_items_retrieve_from_google_civic_view(request):
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    text_for_map_search = request.GET.get('text_for_map_search', '')
    use_test_election = request.GET.get('use_test_election', False)
    use_test_election = False if use_test_election == 'false' else use_test_election
    use_test_election = False if use_test_election == 'False' else use_test_election

    results = voter_ballot_items_retrieve_from_google_civic_for_api(
        voter_device_id, text_for_map_search, use_test_election)

    response = HttpResponse(json.dumps(results), content_type='application/json')

    # Save google_civic_election_id in the cookie so the interface knows
    google_civic_election_id_from_ballot_retrieve = results['google_civic_election_id']
    if positive_value_exists(google_civic_election_id_from_ballot_retrieve):
        set_google_civic_election_id_cookie(request, response, results['google_civic_election_id'])

    return response
Beispiel #30
0
def position_save_view(request):
    """
    Save a single position
    :param request:
    :return:
    """
    # We set values that aren't passed in, to False so we know to treat them as null or unchanged. This allows us to
    #  only change the values we want to
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    position_id = request.POST.get('position_id', False)
    position_we_vote_id = request.POST.get('position_we_vote_id', False)
    organization_we_vote_id = request.POST.get('organization_we_vote_id', False)
    public_figure_we_vote_id = request.POST.get('public_figure_we_vote_id', False)
    voter_we_vote_id = request.POST.get('voter_we_vote_id', False)
    google_civic_election_id = request.POST.get('google_civic_election_id', False)
    ballot_item_display_name = request.POST.get('ballot_item_display_name', False)
    office_we_vote_id = request.POST.get('office_we_vote_id', False)
    candidate_we_vote_id = request.POST.get('candidate_we_vote_id', False)
    measure_we_vote_id = request.POST.get('measure_we_vote_id', False)
    stance = request.POST.get('stance', False)
    statement_text = request.POST.get('statement_text', False)
    statement_html = request.POST.get('statement_html', False)
    more_info_url = request.POST.get('more_info_url', False)

    results = position_save_for_api(
        voter_device_id=voter_device_id,
        position_id=position_id,
        position_we_vote_id=position_we_vote_id,
        organization_we_vote_id=organization_we_vote_id,
        public_figure_we_vote_id=public_figure_we_vote_id,
        voter_we_vote_id=voter_we_vote_id,
        google_civic_election_id=google_civic_election_id,
        ballot_item_display_name=ballot_item_display_name,
        office_we_vote_id=office_we_vote_id,
        candidate_we_vote_id=candidate_we_vote_id,
        measure_we_vote_id=measure_we_vote_id,
        stance=stance,
        statement_text=statement_text,
        statement_html=statement_html,
        more_info_url=more_info_url,
    )

    return HttpResponse(json.dumps(results), content_type='application/json')
Beispiel #31
0
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):
        print 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})
Beispiel #32
0
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_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_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})
Beispiel #33
0
def my_ballot_view(request):
    generate_voter_device_id_if_needed = True
    voter_device_id = get_voter_device_id(request, generate_voter_device_id_if_needed)
    voter_id_found = False

    voter_device_link_manager = VoterDeviceLinkManager()
    results = voter_device_link_manager.retrieve_voter_device_link_from_voter_device_id(voter_device_id)
    if results['voter_device_link_found']:
        voter_device_link = results['voter_device_link']
        voter_id_found = True if voter_device_link.voter_id > 0 else False

    # If existing voter not found, create a new voter
    if not voter_id_found:
        # Create a new voter and return the id
        voter_manager = VoterManager()
        results = voter_manager.create_voter()

        if results['voter_created']:
            voter = results['voter']

            # Now save the voter_device_link
            results = voter_device_link_manager.save_new_voter_device_link(voter_device_id, voter.id)

            if results['voter_device_link_created']:
                voter_device_link = results['voter_device_link']
                voter_id_found = True if voter_device_link.voter_id > 0 else False

    if not voter_id_found:
        print "Voter ID not found, nor generated. This should not be possible. (Cookies may be turned off?)"
        ballot_item_list = []
    else:
        ballot_item_manager = BallotItemManager()
        results = ballot_item_manager.retrieve_all_ballot_items_for_voter(voter_device_link.voter_id)
        ballot_item_list = results['ballot_item_list']

    template_values = {
        'ballot_item_list':     ballot_item_list,
    }
    response = render(request, 'ux_birch/my_ballot.html', template_values)

    set_voter_device_id(request, response, voter_device_id)
    return response
Beispiel #34
0
def voter_setup(request):
    generate_voter_device_id_if_needed = True
    voter_device_id = get_voter_device_id(request, generate_voter_device_id_if_needed)
    voter_id_found = False
    store_new_voter_device_id_in_cookie = True

    voter_device_link_manager = VoterDeviceLinkManager()
    results = voter_device_link_manager.retrieve_voter_device_link_from_voter_device_id(voter_device_id)
    if results['voter_device_link_found']:
        voter_device_link = results['voter_device_link']
        voter_id = voter_device_link.voter_id
        voter_id_found = True if positive_value_exists(voter_id) else False
        store_new_voter_device_id_in_cookie = False if positive_value_exists(voter_id_found) else True

    # If existing voter not found, create a new voter
    if not voter_id_found:
        # Create a new voter and return the id
        voter_manager = VoterManager()
        results = voter_manager.create_voter()

        if results['voter_created']:
            voter = results['voter']
            voter_id = voter.id

            # Now save the voter_device_link
            results = voter_device_link_manager.save_new_voter_device_link(voter_device_id, voter_id)

            if results['voter_device_link_created']:
                voter_device_link = results['voter_device_link']
                voter_id = voter_device_link.voter_id
                voter_id_found = True if voter_id > 0 else False
            else:
                voter_id = 0
                voter_id_found = False

    final_results = {
        'voter_id':                 voter_id,
        'voter_device_id':          voter_device_id,
        'voter_id_found':           voter_id_found,
        'store_new_voter_device_id_in_cookie':   store_new_voter_device_id_in_cookie,
    }
    return final_results
Beispiel #35
0
def voter_stance_for_candidate_campaign_view(request, candidate_campaign_id):
    # print "voter_stance_for_candidate_campaign_view {candidate_campaign_id}".format(
    #     candidate_campaign_id=candidate_campaign_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    position_entered_manager = PositionEnteredManager()
    results = position_entered_manager.retrieve_voter_candidate_campaign_position(voter_id, candidate_campaign_id)
    if results['position_found']:
        if results['is_support']:
            return JsonResponse({0: "support"})
        elif results['is_oppose']:
            return JsonResponse({0: "oppose"})
        elif results['is_no_stance']:
            return JsonResponse({0: "no_stance"})
        elif results['is_information_only']:
            return JsonResponse({0: "information_only"})
        elif results['is_still_deciding']:
            return JsonResponse({0: "still_deciding"})
    return JsonResponse({0: "failure"})
Beispiel #36
0
def voter_stop_supporting_save_view(request):
    """
    Save support for a single measure or candidate for one voter (voterStopSupportingSave)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    kind_of_ballot_item = request.GET.get('kind_of_ballot_item', "")
    ballot_item_id = request.GET.get('ballot_item_id', 0)
    # ballot_item_we_vote_id = request.GET.get('ballot_item_we_vote_id', None)
    if kind_of_ballot_item == CANDIDATE:
        candidate_id = ballot_item_id
        measure_id = 0
    elif kind_of_ballot_item == MEASURE:
        candidate_id = 0
        measure_id = ballot_item_id
    else:
        candidate_id = 0
        measure_id = 0
    return voter_stop_supporting_save(voter_device_id=voter_device_id, candidate_id=candidate_id, measure_id=measure_id)
Beispiel #37
0
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)
Beispiel #38
0
def voter_stance_for_candidate_campaign_view(request, candidate_campaign_id):
    # print "voter_stance_for_candidate_campaign_view {candidate_campaign_id}".format(
    #     candidate_campaign_id=candidate_campaign_id)
    voter_device_id = get_voter_device_id(request)
    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)

    position_entered_manager = PositionEnteredManager()
    results = position_entered_manager.retrieve_voter_candidate_campaign_position(
        voter_id, candidate_campaign_id)
    if results['position_found']:
        if results['is_support']:
            return JsonResponse({0: "support"})
        elif results['is_oppose']:
            return JsonResponse({0: "oppose"})
        elif results['is_no_stance']:
            return JsonResponse({0: "no_stance"})
        elif results['is_information_only']:
            return JsonResponse({0: "information_only"})
        elif results['is_still_deciding']:
            return JsonResponse({0: "still_deciding"})
    return JsonResponse({0: "failure"})
Beispiel #39
0
def position_support_count_for_ballot_item_view(request):
    """
    Retrieve the number of orgs and friends that support this (positionSupportCountForBallotItem)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    kind_of_ballot_item = request.GET.get('kind_of_ballot_item', "")
    ballot_item_id = request.GET.get('ballot_item_id', 0)
    # ballot_item_we_vote_id = request.GET.get('ballot_item_we_vote_id', None)
    if kind_of_ballot_item == CANDIDATE:
        candidate_id = ballot_item_id
        measure_id = 0
    elif kind_of_ballot_item == MEASURE:
        candidate_id = 0
        measure_id = ballot_item_id
    else:
        candidate_id = 0
        measure_id = 0
    return position_support_count_for_ballot_item_for_api(voter_device_id=voter_device_id, candidate_id=candidate_id,
                                                          measure_id=measure_id)
Beispiel #40
0
def voter_address_save_view(request):
    """
    Save or update an address for this voter
    :param request:
    :return:
    """

    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    try:
        text_for_map_search = request.POST['text_for_map_search']
        address_variable_exists = True
    except KeyError:
        text_for_map_search = ''
        address_variable_exists = False

    response = voter_address_save_for_api(voter_device_id, text_for_map_search, address_variable_exists)

    # Reset google_civic_election_id whenever we save a new address
    google_civic_election_id = 0
    set_google_civic_election_id_cookie(request, response, google_civic_election_id)

    return response
Beispiel #41
0
def position_list_for_ballot_item_view(request):
    """
    Retrieve the number of orgs and friends that support this (positionSupportCountForBallotItem)
    :param request:
    :return:
    """
    voter_device_id = get_voter_device_id(request)  # We look in the cookies for voter_device_id
    stance = request.GET.get('stance', ANY_STANCE)
    if stance in(ANY_STANCE, SUPPORT, STILL_DECIDING, INFORMATION_ONLY, NO_STANCE, OPPOSE, PERCENT_RATING):
        stance_we_are_looking_for = stance
    else:
        stance_we_are_looking_for = ANY_STANCE
    show_positions_this_voter_follows = request.GET.get('show_positions_this_voter_follows', True)
    kind_of_ballot_item = request.GET.get('kind_of_ballot_item', "")
    ballot_item_id = request.GET.get('ballot_item_id', 0)
    # ballot_item_we_vote_id = request.GET.get('ballot_item_we_vote_id', None)
    if kind_of_ballot_item == OFFICE:
        office_id = ballot_item_id
        candidate_id = 0
        measure_id = 0
    elif kind_of_ballot_item == CANDIDATE:
        office_id = 0
        candidate_id = ballot_item_id
        measure_id = 0
    elif kind_of_ballot_item == MEASURE:
        office_id = 0
        candidate_id = 0
        measure_id = ballot_item_id
    else:
        office_id = 0
        candidate_id = 0
        measure_id = 0
    return position_list_for_ballot_item_for_api(voter_device_id=voter_device_id,
                                                 office_id=office_id,
                                                 candidate_id=candidate_id,
                                                 measure_id=measure_id,
                                                 stance_we_are_looking_for=stance_we_are_looking_for,
                                                 show_positions_this_voter_follows=show_positions_this_voter_follows)
Beispiel #42
0
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})
Beispiel #43
0
def voter_create_view(request):
    voter_device_id = get_voter_device_id(
        request)  # We look in the cookies for voter_device_id
    return voter_create(voter_device_id)