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')
def candidate_edit_process_view(request): """ Process the new or edit candidate 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) candidate_id = convert_to_int(request.POST['candidate_id']) candidate_name = request.POST.get('candidate_name', False) candidate_twitter_handle = request.POST.get('candidate_twitter_handle', False) if positive_value_exists(candidate_twitter_handle): candidate_twitter_handle = extract_twitter_handle_from_text_string(candidate_twitter_handle) candidate_url = request.POST.get('candidate_url', False) contest_office_id = request.POST.get('contest_office_id', False) ballot_guide_official_statement = request.POST.get('ballot_guide_official_statement', False) party = request.POST.get('party', False) remove_duplicate_process = request.POST.get('remove_duplicate_process', False) google_civic_election_id = request.POST.get('google_civic_election_id', 0) # Check to see if this candidate is already being used anywhere candidate_on_stage_found = False candidate_on_stage = CandidateCampaign() if positive_value_exists(candidate_id): try: candidate_query = CandidateCampaign.objects.filter(id=candidate_id) if len(candidate_query): candidate_on_stage = candidate_query[0] candidate_on_stage_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) contest_office_we_vote_id = '' if positive_value_exists(contest_office_id): contest_office_manager = ContestOfficeManager() results = contest_office_manager.retrieve_contest_office_from_id(contest_office_id) if results['contest_office_found']: contest_office = results['contest_office'] contest_office_we_vote_id = contest_office.we_vote_id try: if candidate_on_stage_found: # Update if candidate_twitter_handle is not False: candidate_on_stage.candidate_twitter_handle = candidate_twitter_handle if candidate_url is not False: candidate_on_stage.candidate_url = candidate_url if ballot_guide_official_statement is not False: candidate_on_stage.ballot_guide_official_statement = ballot_guide_official_statement if party is not False: candidate_on_stage.party = party # Check to see if this is a We Vote-created election # is_we_vote_google_civic_election_id = True \ # if convert_to_int(candidate_on_stage.google_civic_election_id) >= 1000000 \ # else False if contest_office_id is not False: # We only allow updating of candidates within the We Vote Admin in candidate_on_stage.contest_office_id = contest_office_id candidate_on_stage.contest_office_we_vote_id = contest_office_we_vote_id candidate_on_stage.save() messages.add_message(request, messages.INFO, 'Candidate Campaign updated.') else: # Create new # election must be found election_manager = ElectionManager() election_results = election_manager.retrieve_election(google_civic_election_id) if election_results['election_found']: election = election_results['election'] state_code = election.get_election_state() else: messages.add_message(request, messages.ERROR, 'Could not find election -- required to save candidate.') return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,))) required_candidate_variables = True \ if positive_value_exists(candidate_name) and positive_value_exists(contest_office_id) \ else False if required_candidate_variables: candidate_on_stage = CandidateCampaign( candidate_name=candidate_name, google_civic_election_id=google_civic_election_id, contest_office_id=contest_office_id, contest_office_we_vote_id=contest_office_we_vote_id, state_code=state_code, ) if candidate_twitter_handle is not False: candidate_on_stage.candidate_twitter_handle = candidate_twitter_handle if candidate_url is not False: candidate_on_stage.candidate_url = candidate_url if ballot_guide_official_statement is not False: candidate_on_stage.ballot_guide_official_statement = ballot_guide_official_statement if party is not False: candidate_on_stage.party = party candidate_on_stage.save() candidate_id = candidate_on_stage.id messages.add_message(request, messages.INFO, 'New candidate saved.') else: messages.add_message(request, messages.INFO, 'Could not save -- missing required variables.') if positive_value_exists(candidate_id): return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)) + "?google_civic_election_id=" + str(google_civic_election_id) + "&contest_office_id=" + str(contest_office_id) ) else: return HttpResponseRedirect(reverse('candidate:candidate_new', args=()) + "?google_civic_election_id=" + str(google_civic_election_id) + "&contest_office_id=" + str(contest_office_id) ) except Exception as e: handle_record_not_saved_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'Could not save candidate.') return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,))) if remove_duplicate_process: return HttpResponseRedirect(reverse('candidate:find_and_remove_duplicate_candidates', args=()) + "?google_civic_election_id=" + str(google_civic_election_id)) else: return HttpResponseRedirect(reverse('candidate:candidate_edit', args=(candidate_id,)))