Beispiel #1
0
def quick_info_edit_process_view(request):
    """
    Process the new or edit quick_info forms
    :param request:
    :return:
    """
    authority_required = {'verified_volunteer'}  # admin, verified_volunteer
    if not voter_has_authority(request, authority_required):
        return redirect_to_sign_in_page(request, authority_required)

    quick_info_id = convert_to_int(request.POST.get('quick_info_id', False))
    quick_info_we_vote_id = convert_to_int(request.POST.get('quick_info_we_vote_id', False))

    language = request.POST.get('language', False)
    info_text = request.POST.get('info_text', False)
    info_html = request.POST.get('info_html', False)
    ballot_item_display_name = request.POST.get('ballot_item_display_name', False)
    more_info_credit = request.POST.get('more_info_credit', False)
    more_info_url = request.POST.get('more_info_url', False)

    contest_office_we_vote_id = request.POST.get('contest_office_we_vote_id', False)
    candidate_campaign_we_vote_id = request.POST.get('candidate_campaign_we_vote_id', False)
    politician_we_vote_id = request.POST.get('politician_we_vote_id', False)
    contest_measure_we_vote_id = request.POST.get('contest_measure_we_vote_id', False)

    quick_info_master_we_vote_id = request.POST.get('quick_info_master_we_vote_id', False)
    google_civic_election_id = request.POST.get('google_civic_election_id', False)

    change_election = request.POST.get('change_election', '0')  # *Just* switch the election we are looking at
    change_language = request.POST.get('change_language', '0')  # *Just* switch to different language
    use_unique_text = request.POST.get('use_unique_text', '0')
    use_master_entry = request.POST.get('use_master_entry', '0')
    change_text_vs_master = request.POST.get('change_text_vs_master', '0')  # *Just* switch between text entry & master
    change_quick_info_master = request.POST.get('change_quick_info_master', '0')  # *Just* update master display

    number_of_ballot_items = 0
    if positive_value_exists(contest_office_we_vote_id):
        number_of_ballot_items += 1
    if positive_value_exists(candidate_campaign_we_vote_id):
        number_of_ballot_items += 1
    if positive_value_exists(politician_we_vote_id):
        number_of_ballot_items += 1
    if positive_value_exists(contest_measure_we_vote_id):
        number_of_ballot_items += 1

    if positive_value_exists(change_election) or \
            positive_value_exists(change_language) or \
            positive_value_exists(change_text_vs_master) or \
            positive_value_exists(change_quick_info_master):
        # We are just changing an option, and not trying to save
        ready_to_save = False
    elif number_of_ballot_items is 0:
        messages.add_message(request, messages.ERROR, "You must choose at least one ballot item.")
        ready_to_save = False
    elif number_of_ballot_items > 1:
        messages.add_message(request, messages.ERROR, "Please choose only one ballot item.")
        ready_to_save = False
    # Do we have all of the required variables, unique to each mode?
    else:
        # If using a master entry
        if positive_value_exists(use_master_entry):
            if not positive_value_exists(quick_info_master_we_vote_id):
                messages.add_message(request, messages.ERROR, "Please choose a master entry for this ballot item.")
                ready_to_save = False
            else:
                ready_to_save = True
        # If entering text specific to this ballot item
        elif not positive_value_exists(use_master_entry) or positive_value_exists(use_unique_text):
            if not positive_value_exists(language):
                messages.add_message(request, messages.ERROR, "Please choose a language for your new entry.")
                ready_to_save = False
            elif not (positive_value_exists(info_text) or positive_value_exists(info_html)):
                messages.add_message(request, messages.ERROR,
                                     "Please enter the text/html information about this ballot item.")
                ready_to_save = False
            elif not positive_value_exists(more_info_url):
                messages.add_message(request, messages.ERROR, "Please enter the source URL for this description.")
                ready_to_save = False
            else:
                ready_to_save = True
        else:
            messages.add_message(request, messages.ERROR, "More information needed to save this entry.")
            ready_to_save = False

    if not ready_to_save:
        # Could we also just call the view directly with the request, instead of redirecting the browser?
        if positive_value_exists(quick_info_id):
            return quick_info_edit_view(request, quick_info_id)
            # return HttpResponseRedirect(reverse('quick_info:quick_info_edit', args=()) + url_variables)
        else:
            return quick_info_new_view(request)
            # return HttpResponseRedirect(reverse('quick_info:quick_info_new', args=()) + url_variables)

    # Now that we know we are ready to save, we want to wipe out the values we don't want to save
    if positive_value_exists(use_master_entry):
        info_html = ""
        info_text = ""
        more_info_url = ""
        more_info_credit = NOT_SPECIFIED
    else:
        quick_info_master_we_vote_id = ""

    # Figure out what text to use for the Ballot Item Label
    if not positive_value_exists(ballot_item_display_name):
        if positive_value_exists(contest_office_we_vote_id):
            contest_office_manager = ContestOfficeManager()
            results = contest_office_manager.retrieve_contest_office_from_we_vote_id(contest_office_we_vote_id)
            if results['success']:
                contest_office = results['contest_office']
                ballot_item_display_name = contest_office.office_name
            else:
                ballot_item_display_name = ''
        elif positive_value_exists(candidate_campaign_we_vote_id):
            candidate_campaign_manager = CandidateCampaignManager()
            results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(
                candidate_campaign_we_vote_id)
            if results['success']:
                candidate_campaign = results['candidate_campaign']
                ballot_item_display_name = candidate_campaign.candidate_name
            else:
                ballot_item_display_name = ''
        # if positive_value_exists(politician_we_vote_id):
        #     ballot_item_display_name = ''
        elif positive_value_exists(contest_measure_we_vote_id):
            contest_measure_manager = ContestMeasureManager()
            results = contest_measure_manager.retrieve_contest_measure_from_we_vote_id(contest_measure_we_vote_id)
            if results['success']:
                contest_measure = results['contest_measure']
                ballot_item_display_name = contest_measure.measure_title
            else:
                ballot_item_display_name = ""

    last_editor_we_vote_id = ""  # TODO We need to calculate this

    quick_info_manager = QuickInfoManager()
    results = quick_info_manager.update_or_create_quick_info(
        quick_info_id=quick_info_id,
        quick_info_we_vote_id=quick_info_we_vote_id,
        ballot_item_display_name=ballot_item_display_name,
        contest_office_we_vote_id=contest_office_we_vote_id,
        candidate_campaign_we_vote_id=candidate_campaign_we_vote_id,
        politician_we_vote_id=politician_we_vote_id,
        contest_measure_we_vote_id=contest_measure_we_vote_id,
        info_html=info_html,
        info_text=info_text,
        language=language,
        last_editor_we_vote_id=last_editor_we_vote_id,
        quick_info_master_we_vote_id=quick_info_master_we_vote_id,
        more_info_url=more_info_url,
        more_info_credit=more_info_credit,
        google_civic_election_id=google_civic_election_id
        )
    if results['success']:
        messages.add_message(request, messages.INFO, results['status'])
    else:
        messages.add_message(request, messages.ERROR,
                             results['status'] + "language: {language}".format(
                                     language=language,
                             ))
        if positive_value_exists(quick_info_id):
            return quick_info_edit_view(request, quick_info_id)
        else:
            return quick_info_new_view(request)

    return HttpResponseRedirect(reverse('quick_info:quick_info_list', args=()))
    def test_retrieve_with_no_cookie(self):
        #######################################
        # Without a cookie or required variables, we don't expect valid response
        response01 = self.client.get(self.candidates_retrieve_url)
        json_data01 = json.loads(response01.content)

        self.assertEqual('status' in json_data01, True, "status expected in the json response, and not found")
        self.assertEqual('success' in json_data01, True, "success expected in the json response, and not found")
        self.assertEqual('office_id' in json_data01, True, "office_id expected in the json response, and not found")
        self.assertEqual('office_we_vote_id' in json_data01, True,
                         "office_we_vote_id expected in the json response, and not found")
        self.assertEqual('google_civic_election_id' in json_data01, True,
                         "google_civic_election_id expected in the json response, and not found")
        self.assertEqual('candidate_list' in json_data01, True,
                         "candidate_list expected in the json response, and not found")

        self.assertEqual(
            json_data01['status'], 'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING',
            "status: {status} (VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING expected), ".format(
                status=json_data01['status']))
        self.assertEqual(json_data01['success'], False, "success: {success} (success 'False' expected), ".format(
                success=json_data01['success']))
        self.assertEqual(json_data01['office_id'], 0, "office_id: {office_id} ('0' expected), ".format(
                office_id=json_data01['office_id']))
        self.assertEqual(json_data01['office_we_vote_id'], '',
                         "office_we_vote_id: {office_we_vote_id} ('' expected), ".format(
                             office_we_vote_id=json_data01['office_we_vote_id']))
        self.assertEqual(json_data01['google_civic_election_id'], 0,
                         "google_civic_election_id: {google_civic_election_id} ('0' expected), ".format(
                             google_civic_election_id=json_data01['google_civic_election_id']))
        self.assertItemsEqual(json_data01['candidate_list'], [],
                              "candidate_list: {candidate_list} (Empty list expected), ".format(
                candidate_list=json_data01['candidate_list']))

        #######################################
        # We want to import some election-related data so we can test lists
        import_data_for_tests()

        #######################################
        # Retrieve contest_office so we can work with it
        contest_office_manager = ContestOfficeManager()
        results = contest_office_manager.retrieve_contest_office_from_we_vote_id(
            contest_office_we_vote_id='wv01off922')

        self.assertEqual('success' in results, True,
                         "Unable to retrieve contest_office with contest_office_we_vote_id='wv01off922'")

        if results['success']:
            contest_office = results['contest_office']
        else:
            contest_office = ContestOffice()

        self.assertEqual('wv01off922' in contest_office.we_vote_id, True,
                         "contest_office retrieved does not have contest_office.we_vote_id='wv01off922'")

        #######################################
        # We should get a valid response
        response02 = self.client.get(self.candidates_retrieve_url, {'office_we_vote_id': contest_office.we_vote_id})
        json_data02 = json.loads(response02.content)

        self.assertEqual('status' in json_data02, True, "status expected in the json response, and not found")
        self.assertEqual('success' in json_data02, True, "success expected in the json response, and not found")
        self.assertEqual('office_id' in json_data02, True, "office_id expected in the json response, and not found")
        self.assertEqual('office_we_vote_id' in json_data02, True,
                         "office_we_vote_id expected in the json response, and not found")
        self.assertEqual('google_civic_election_id' in json_data02, True,
                         "google_civic_election_id expected in the json response, and not found")
        self.assertEqual('candidate_list' in json_data02, True,
                         "candidate_list expected in the json response, and not found")

        self.assertEqual(
            json_data02['status'], 'CANDIDATES_RETRIEVED',
            "status: {status} (CANDIDATES_RETRIEVED expected), ".format(
                status=json_data02['status']))
        self.assertEqual(json_data02['success'], True, "success: {success} (success 'True' expected), ".format(
                success=json_data02['success']))
        # For this test we don't know what the internal office_id should be
        # self.assertEqual(json_data02['office_id'], 0, "office_id: {office_id} ('0' expected), ".format(
        #         office_id=json_data02['office_id']))
        self.assertEqual(json_data02['office_we_vote_id'], 'wv01off922',
                         "office_we_vote_id: {office_we_vote_id} ('wv01off922' expected), ".format(
                             office_we_vote_id=json_data02['office_we_vote_id']))
        self.assertEqual(json_data02['google_civic_election_id'], '4162',
                         "google_civic_election_id: {google_civic_election_id} ('4162' expected), ".format(
                             google_civic_election_id=json_data02['google_civic_election_id']))
        self.assertEqual(len(json_data02['candidate_list']), 3,
                         "len(candidate_list): {candidate_list_count} (3 candidates expected), ".format(
                             candidate_list_count=len(json_data02['candidate_list'])))
Beispiel #3
0
def position_list_for_ballot_item_for_api(voter_device_id,  # positionListForBallotItem
                                          office_id, office_we_vote_id,
                                          candidate_id, candidate_we_vote_id,
                                          measure_id, measure_we_vote_id,
                                          stance_we_are_looking_for=ANY_STANCE,
                                          show_positions_this_voter_follows=True):
    """
    We want to return a JSON file with the position identifiers from orgs, friends and public figures the voter follows
    This list of information is used to retrieve the detailed information
    """
    position_manager = PositionEnteredManager()
    # Get voter_id from the voter_device_id so we can know who is supporting/opposing
    results = is_voter_device_id_valid(voter_device_id)
    if not results['success']:
        position_list = []
        json_data = {
            'status': 'VALID_VOTER_DEVICE_ID_MISSING',
            'success': False,
            'count':            0,
            'kind_of_ballot_item': "UNKNOWN",
            'ballot_item_id':   0,
            'position_list':    position_list,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    voter_id = fetch_voter_id_from_voter_device_link(voter_device_id)
    if not positive_value_exists(voter_id):
        position_list = []
        json_data = {
            'status': "VALID_VOTER_ID_MISSING ",
            'success': False,
            'count':            0,
            'kind_of_ballot_item': "UNKNOWN",
            'ballot_item_id':   0,
            'position_list':    position_list,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    position_list_manager = PositionListManager()
    ballot_item_found = False
    if positive_value_exists(candidate_id) or positive_value_exists(candidate_we_vote_id):
        all_positions_list = position_list_manager.retrieve_all_positions_for_candidate_campaign(
                candidate_id, candidate_we_vote_id, stance_we_are_looking_for)
        kind_of_ballot_item = CANDIDATE

        # Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions
        # for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of
        # the WebApp team)
        candidate_campaign_manager = CandidateCampaignManager()
        if positive_value_exists(candidate_id):
            results = candidate_campaign_manager.retrieve_candidate_campaign_from_id(candidate_id)
        else:
            results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(candidate_we_vote_id)

        if results['candidate_campaign_found']:
            candidate_campaign = results['candidate_campaign']
            ballot_item_id = candidate_campaign.id
            ballot_item_we_vote_id = candidate_campaign.we_vote_id
            ballot_item_found = True
        else:
            ballot_item_id = candidate_id
            ballot_item_we_vote_id = candidate_we_vote_id
    elif positive_value_exists(measure_id) or positive_value_exists(measure_we_vote_id):
        all_positions_list = position_list_manager.retrieve_all_positions_for_contest_measure(
                measure_id, measure_we_vote_id, stance_we_are_looking_for)
        kind_of_ballot_item = MEASURE

        # Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions
        # for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of
        # the WebApp team)
        contest_measure_manager = ContestMeasureManager()
        if positive_value_exists(measure_id):
            results = contest_measure_manager.retrieve_contest_measure_from_id(measure_id)
        else:
            results = contest_measure_manager.retrieve_contest_measure_from_we_vote_id(measure_we_vote_id)

        if results['contest_measure_found']:
            contest_measure = results['contest_measure']
            ballot_item_id = contest_measure.id
            ballot_item_we_vote_id = contest_measure.we_vote_id
            ballot_item_found = True
        else:
            ballot_item_id = measure_id
            ballot_item_we_vote_id = measure_we_vote_id
    elif positive_value_exists(office_id) or positive_value_exists(office_we_vote_id):
        all_positions_list = position_list_manager.retrieve_all_positions_for_contest_office(
                office_id, office_we_vote_id, stance_we_are_looking_for)
        kind_of_ballot_item = OFFICE

        # Since we want to return the id and we_vote_id, and we don't know for sure that there are any positions
        # for this ballot_item, we retrieve the following so we can get the id and we_vote_id (per the request of
        # the WebApp team)
        contest_office_manager = ContestOfficeManager()
        if positive_value_exists(office_id):
            results = contest_office_manager.retrieve_contest_office_from_id(office_id)
        else:
            results = contest_office_manager.retrieve_contest_office_from_we_vote_id(office_we_vote_id)

        if results['contest_office_found']:
            contest_office = results['contest_office']
            ballot_item_id = contest_office.id
            ballot_item_we_vote_id = contest_office.we_vote_id
            ballot_item_found = True
        else:
            ballot_item_id = office_id
            ballot_item_we_vote_id = office_we_vote_id
    else:
        position_list = []
        json_data = {
            'status':                   'POSITION_LIST_RETRIEVE_MISSING_BALLOT_ITEM_ID',
            'success':                  False,
            'count':                    0,
            'kind_of_ballot_item':      "UNKNOWN",
            'ballot_item_id':           0,
            'ballot_item_we_vote_id':   '',
            'position_list':            position_list,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    if not ballot_item_found:
        position_list = []
        json_data = {
            'status':                   'POSITION_LIST_RETRIEVE_BALLOT_ITEM_NOT_FOUND',
            'success':                  False,
            'count':                    0,
            'kind_of_ballot_item':      "UNKNOWN",
            'ballot_item_id':           ballot_item_id,
            'ballot_item_we_vote_id':   ballot_item_we_vote_id,
            'position_list':            position_list,
        }
        return HttpResponse(json.dumps(json_data), content_type='application/json')

    follow_organization_list_manager = FollowOrganizationList()
    organizations_followed_by_voter = \
        follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id)

    if show_positions_this_voter_follows:
        position_objects = position_list_manager.calculate_positions_followed_by_voter(
            voter_id, all_positions_list, organizations_followed_by_voter)
        positions_count = len(position_objects)
        status = 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_FOLLOWED'
        success = True
    else:
        position_objects = position_list_manager.calculate_positions_not_followed_by_voter(
            all_positions_list, organizations_followed_by_voter)
        positions_count = len(position_objects)
        status = 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED'
        success = True

    position_list = []
    for one_position in position_objects:
        # Whose position is it?
        if positive_value_exists(one_position.organization_we_vote_id):
            speaker_type = ORGANIZATION
            speaker_id = one_position.organization_id
            speaker_we_vote_id = one_position.organization_we_vote_id
            one_position_success = True
            # Make sure we have this data to display
            if not positive_value_exists(one_position.speaker_display_name) \
                    or not positive_value_exists(one_position.speaker_image_url_https):
                one_position = position_manager.refresh_cached_position_info(one_position)
        elif positive_value_exists(one_position.voter_id):
            speaker_type = VOTER
            speaker_id = one_position.voter_id
            speaker_we_vote_id = one_position.voter_we_vote_id
            one_position_success = True
            # Make sure we have this data to display
            if not positive_value_exists(one_position.speaker_display_name):
                one_position = position_manager.refresh_cached_position_info(one_position)
        elif positive_value_exists(one_position.public_figure_we_vote_id):
            speaker_type = PUBLIC_FIGURE
            speaker_id = one_position.public_figure_id
            speaker_we_vote_id = one_position.public_figure_we_vote_id
            one_position_success = True
            # Make sure we have this data to display
            if not positive_value_exists(one_position.speaker_display_name) \
                    or not positive_value_exists(one_position.speaker_image_url_https):
                one_position = position_manager.refresh_cached_position_info(one_position)
        else:
            speaker_type = UNKNOWN_VOTER_GUIDE
            speaker_id = None
            speaker_we_vote_id = None
            one_position_success = False

        if one_position_success:
            one_position_dict_for_api = {
                'position_id':          one_position.id,
                'position_we_vote_id':  one_position.we_vote_id,
                'ballot_item_display_name': one_position.ballot_item_display_name,
                'speaker_display_name': one_position.speaker_display_name,
                'speaker_image_url_https': one_position.speaker_image_url_https,
                'speaker_type':         speaker_type,
                'speaker_id':           speaker_id,
                'speaker_we_vote_id':   speaker_we_vote_id,
                'is_support':           one_position.is_support(),
                'is_oppose':            one_position.is_oppose(),
                'vote_smart_rating':    one_position.vote_smart_rating,
                'vote_smart_time_span': one_position.vote_smart_time_span,
                'last_updated':         one_position.last_updated(),
            }
            position_list.append(one_position_dict_for_api)

    json_data = {
        'status':                   status,
        'success':                  success,
        'count':                    positions_count,
        'kind_of_ballot_item':      kind_of_ballot_item,
        'ballot_item_id':           ballot_item_id,
        'ballot_item_we_vote_id':   ballot_item_we_vote_id,
        'position_list':            position_list,
    }
    return HttpResponse(json.dumps(json_data), content_type='application/json')
Beispiel #4
0
    def test_retrieve_with_no_voter_device_id(self):
        #######################################
        # Without a cookie or required variables, we don't expect valid response
        response01 = self.client.get(self.candidates_retrieve_url)
        json_data01 = json.loads(response01.content.decode())

        self.assertEqual(
            'status' in json_data01, True,
            "status expected in the json response, and not found")
        self.assertEqual(
            'success' in json_data01, True,
            "success expected in the json response, and not found")
        self.assertEqual(
            'office_id' in json_data01, True,
            "office_id expected in the json response, and not found")
        self.assertEqual(
            'office_we_vote_id' in json_data01, True,
            "office_we_vote_id expected in the json response, and not found")
        self.assertEqual(
            'google_civic_election_id' in json_data01, True,
            "google_civic_election_id expected in the json response, and not found"
        )
        self.assertEqual(
            'candidate_list' in json_data01, True,
            "candidate_list expected in the json response, and not found")

        self.assertEqual(
            json_data01['status'],
            'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING',
            "status: {status} (VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING expected), "
            .format(status=json_data01['status']))
        self.assertEqual(
            json_data01['success'], False,
            "success: {success} (success 'False' expected), ".format(
                success=json_data01['success']))
        self.assertEqual(
            json_data01['office_id'], 0,
            "office_id: {office_id} ('0' expected), ".format(
                office_id=json_data01['office_id']))
        self.assertEqual(
            json_data01['office_we_vote_id'], '',
            "office_we_vote_id: {office_we_vote_id} ('' expected), ".format(
                office_we_vote_id=json_data01['office_we_vote_id']))
        self.assertEqual(
            json_data01['google_civic_election_id'], 0,
            "google_civic_election_id: {google_civic_election_id} ('0' expected), "
            .format(google_civic_election_id=json_data01[
                'google_civic_election_id']))
        self.assertListEqual(
            json_data01['candidate_list'], [],
            "candidate_list: {candidate_list} (Empty list expected), ".format(
                candidate_list=json_data01['candidate_list']))

        #######################################
        # We want to import some election-related data so we can test lists
        import_data_for_tests()

        #######################################
        # Retrieve contest_office so we can work with it
        contest_office_manager = ContestOfficeManager()
        results = contest_office_manager.retrieve_contest_office_from_we_vote_id(
            contest_office_we_vote_id='wv01off922')

        self.assertEqual(
            'success' in results, True,
            "Unable to retrieve contest_office with contest_office_we_vote_id='wv01off922'"
        )

        if results['success']:
            contest_office = results['contest_office']
        else:
            contest_office = ContestOffice()

        self.assertEqual(
            'wv01off922' in contest_office.we_vote_id, True,
            "contest_office retrieved does not have contest_office.we_vote_id='wv01off922'"
        )

        #######################################
        # We should get a valid response
        response02 = self.client.get(
            self.candidates_retrieve_url,
            {'office_we_vote_id': contest_office.we_vote_id})
        json_data02 = json.loads(response02.content.decode())

        self.assertEqual(
            'status' in json_data02, True,
            "status expected in the json response, and not found")
        self.assertEqual(
            'success' in json_data02, True,
            "success expected in the json response, and not found")
        self.assertEqual(
            'office_id' in json_data02, True,
            "office_id expected in the json response, and not found")
        self.assertEqual(
            'office_we_vote_id' in json_data02, True,
            "office_we_vote_id expected in the json response, and not found")
        self.assertEqual(
            'google_civic_election_id' in json_data02, True,
            "google_civic_election_id expected in the json response, and not found"
        )
        self.assertEqual(
            'candidate_list' in json_data02, True,
            "candidate_list expected in the json response, and not found")

        self.assertEqual(
            json_data02['status'], 'CANDIDATES_RETRIEVED',
            "status: {status} (CANDIDATES_RETRIEVED expected), ".format(
                status=json_data02['status']))
        self.assertEqual(
            json_data02['success'], True,
            "success: {success} (success 'True' expected), ".format(
                success=json_data02['success']))
        # For this test we don't know what the internal office_id should be
        # self.assertEqual(json_data02['office_id'], 0, "office_id: {office_id} ('0' expected), ".format(
        #         office_id=json_data02['office_id']))
        self.assertEqual(
            json_data02['office_we_vote_id'], 'wv01off922',
            "office_we_vote_id: {office_we_vote_id} ('wv01off922' expected), ".
            format(office_we_vote_id=json_data02['office_we_vote_id']))
        self.assertEqual(
            json_data02['google_civic_election_id'], '4162',
            "google_civic_election_id: {google_civic_election_id} ('4162' expected), "
            .format(google_civic_election_id=json_data02[
                'google_civic_election_id']))
        self.assertEqual(
            len(json_data02['candidate_list']), 3,
            "len(candidate_list): {candidate_list_count} (3 candidates expected), "
            .format(candidate_list_count=len(json_data02['candidate_list'])))
Beispiel #5
0
def quick_info_edit_process_view(request):
    """
    Process the new or edit quick_info forms
    :param request:
    :return:
    """
    authority_required = {'verified_volunteer'}  # admin, verified_volunteer
    if not voter_has_authority(request, authority_required):
        return redirect_to_sign_in_page(request, authority_required)

    quick_info_id = convert_to_int(request.POST.get('quick_info_id', False))
    quick_info_we_vote_id = convert_to_int(
        request.POST.get('quick_info_we_vote_id', False))

    language = request.POST.get('language', False)
    info_text = request.POST.get('info_text', False)
    info_html = request.POST.get('info_html', False)
    ballot_item_display_name = request.POST.get('ballot_item_display_name',
                                                False)
    more_info_credit = request.POST.get('more_info_credit', False)
    more_info_url = request.POST.get('more_info_url', False)

    contest_office_we_vote_id = request.POST.get('contest_office_we_vote_id',
                                                 False)
    candidate_campaign_we_vote_id = request.POST.get(
        'candidate_campaign_we_vote_id', False)
    politician_we_vote_id = request.POST.get('politician_we_vote_id', False)
    contest_measure_we_vote_id = request.POST.get('contest_measure_we_vote_id',
                                                  False)

    quick_info_master_we_vote_id = request.POST.get(
        'quick_info_master_we_vote_id', False)
    google_civic_election_id = request.POST.get('google_civic_election_id',
                                                False)

    change_election = request.POST.get(
        'change_election', '0')  # *Just* switch the election we are looking at
    change_language = request.POST.get(
        'change_language', '0')  # *Just* switch to different language
    use_unique_text = request.POST.get('use_unique_text', '0')
    use_master_entry = request.POST.get('use_master_entry', '0')
    change_text_vs_master = request.POST.get(
        'change_text_vs_master',
        '0')  # *Just* switch between text entry & master
    change_quick_info_master = request.POST.get(
        'change_quick_info_master', '0')  # *Just* update master display

    number_of_ballot_items = 0
    if positive_value_exists(contest_office_we_vote_id):
        number_of_ballot_items += 1
    if positive_value_exists(candidate_campaign_we_vote_id):
        number_of_ballot_items += 1
    if positive_value_exists(politician_we_vote_id):
        number_of_ballot_items += 1
    if positive_value_exists(contest_measure_we_vote_id):
        number_of_ballot_items += 1

    if positive_value_exists(change_election) or \
            positive_value_exists(change_language) or \
            positive_value_exists(change_text_vs_master) or \
            positive_value_exists(change_quick_info_master):
        # We are just changing an option, and not trying to save
        ready_to_save = False
    elif number_of_ballot_items is 0:
        messages.add_message(request, messages.ERROR,
                             "You must choose at least one ballot item.")
        ready_to_save = False
    elif number_of_ballot_items > 1:
        messages.add_message(request, messages.ERROR,
                             "Please choose only one ballot item.")
        ready_to_save = False
    # Do we have all of the required variables, unique to each mode?
    else:
        # If using a master entry
        if positive_value_exists(use_master_entry):
            if not positive_value_exists(quick_info_master_we_vote_id):
                messages.add_message(
                    request, messages.ERROR,
                    "Please choose a master entry for this ballot item.")
                ready_to_save = False
            else:
                ready_to_save = True
        # If entering text specific to this ballot item
        elif not positive_value_exists(
                use_master_entry) or positive_value_exists(use_unique_text):
            if not positive_value_exists(language):
                messages.add_message(
                    request, messages.ERROR,
                    "Please choose a language for your new entry.")
                ready_to_save = False
            elif not (positive_value_exists(info_text)
                      or positive_value_exists(info_html)):
                messages.add_message(
                    request, messages.ERROR,
                    "Please enter the text/html information about this ballot item."
                )
                ready_to_save = False
            elif not positive_value_exists(more_info_url):
                messages.add_message(
                    request, messages.ERROR,
                    "Please enter the source URL for this description.")
                ready_to_save = False
            else:
                ready_to_save = True
        else:
            messages.add_message(
                request, messages.ERROR,
                "More information needed to save this entry.")
            ready_to_save = False

    if not ready_to_save:
        # Could we also just call the view directly with the request, instead of redirecting the browser?
        if positive_value_exists(quick_info_id):
            return quick_info_edit_view(request, quick_info_id)
            # return HttpResponseRedirect(reverse('quick_info:quick_info_edit', args=()) + url_variables)
        else:
            return quick_info_new_view(request)
            # return HttpResponseRedirect(reverse('quick_info:quick_info_new', args=()) + url_variables)

    # Now that we know we are ready to save, we want to wipe out the values we don't want to save
    if positive_value_exists(use_master_entry):
        info_html = ""
        info_text = ""
        more_info_url = ""
        more_info_credit = NOT_SPECIFIED
    else:
        quick_info_master_we_vote_id = ""

    # Figure out what text to use for the Ballot Item Label
    if not positive_value_exists(ballot_item_display_name):
        if positive_value_exists(contest_office_we_vote_id):
            contest_office_manager = ContestOfficeManager()
            results = contest_office_manager.retrieve_contest_office_from_we_vote_id(
                contest_office_we_vote_id)
            if results['success']:
                contest_office = results['contest_office']
                ballot_item_display_name = contest_office.office_name
            else:
                ballot_item_display_name = ''
        elif positive_value_exists(candidate_campaign_we_vote_id):
            candidate_campaign_manager = CandidateCampaignManager()
            results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(
                candidate_campaign_we_vote_id)
            if results['success']:
                candidate_campaign = results['candidate_campaign']
                ballot_item_display_name = candidate_campaign.display_candidate_name(
                )
            else:
                ballot_item_display_name = ''
        # if positive_value_exists(politician_we_vote_id):
        #     ballot_item_display_name = ''
        elif positive_value_exists(contest_measure_we_vote_id):
            contest_measure_manager = ContestMeasureManager()
            results = contest_measure_manager.retrieve_contest_measure_from_we_vote_id(
                contest_measure_we_vote_id)
            if results['success']:
                contest_measure = results['contest_measure']
                ballot_item_display_name = contest_measure.measure_title
            else:
                ballot_item_display_name = ""

    last_editor_we_vote_id = ""  # TODO We need to calculate this

    quick_info_manager = QuickInfoManager()
    results = quick_info_manager.update_or_create_quick_info(
        quick_info_id=quick_info_id,
        quick_info_we_vote_id=quick_info_we_vote_id,
        ballot_item_display_name=ballot_item_display_name,
        contest_office_we_vote_id=contest_office_we_vote_id,
        candidate_campaign_we_vote_id=candidate_campaign_we_vote_id,
        politician_we_vote_id=politician_we_vote_id,
        contest_measure_we_vote_id=contest_measure_we_vote_id,
        info_html=info_html,
        info_text=info_text,
        language=language,
        last_editor_we_vote_id=last_editor_we_vote_id,
        quick_info_master_we_vote_id=quick_info_master_we_vote_id,
        more_info_url=more_info_url,
        more_info_credit=more_info_credit,
        google_civic_election_id=google_civic_election_id)
    if results['success']:
        messages.add_message(request, messages.INFO, results['status'])
    else:
        messages.add_message(
            request, messages.ERROR, results['status'] +
            "language: {language}".format(language=language, ))
        if positive_value_exists(quick_info_id):
            return quick_info_edit_view(request, quick_info_id)
        else:
            return quick_info_new_view(request)

    return HttpResponseRedirect(reverse('quick_info:quick_info_list', args=()))