def positions_public_count_for_contest_measure(measure_id, measure_we_vote_id, stance_we_are_looking_for): """ We want to return a JSON file with the number of orgs and public figures who support this particular measure """ # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListManager() all_positions_count_for_contest_measure = \ position_list_manager.retrieve_public_positions_count_for_contest_measure( measure_id, measure_we_vote_id, stance_we_are_looking_for) if positive_value_exists(measure_id) or positive_value_exists(measure_we_vote_id): contest_measure_manager = ContestMeasureManager() # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(measure_id): measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(measure_id) elif positive_value_exists(measure_we_vote_id): measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id(measure_we_vote_id) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_PUBLIC_POSITION_COUNT_FOR_CONTEST_MEASURE', 'success': True, 'count': all_positions_count_for_contest_measure, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } results = { 'json_data': json_data, } return results
def ballot_item_we_vote_id(self): if self.candidate_campaign_we_vote_id: return self.candidate_campaign_we_vote_id elif self.contest_office_we_vote_id: return self.contest_office_we_vote_id elif self.contest_measure_we_vote_id: return self.contest_measure_we_vote_id elif self.candidate_campaign_id: candidate_campaign_manager = CandidateCampaignManager() return candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(self.candidate_campaign_id) elif self.contest_measure_id: contest_measure_manager = ContestMeasureManager() return contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(self.contest_measure_id) elif self.contest_office_id: contest_office_manager = ContestOfficeManager() return contest_office_manager.fetch_contest_office_we_vote_id_from_id(self.contest_office_id) else: return 'not_found'
def ballot_item_we_vote_id(self): if self.candidate_campaign_we_vote_id: return self.candidate_campaign_we_vote_id elif self.contest_office_we_vote_id: return self.contest_office_we_vote_id elif self.contest_measure_we_vote_id: return self.contest_measure_we_vote_id elif self.candidate_campaign_id: candidate_campaign_manager = CandidateCampaignManager() return candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id( self.candidate_campaign_id) elif self.contest_measure_id: contest_measure_manager = ContestMeasureManager() return contest_measure_manager.fetch_contest_measure_we_vote_id_from_id( self.contest_measure_id) elif self.contest_office_id: contest_office_manager = ContestOfficeManager() return contest_office_manager.fetch_contest_office_we_vote_id_from_id( self.contest_office_id) else: return 'not_found'
def positions_public_count_for_contest_measure(measure_id, measure_we_vote_id, stance_we_are_looking_for): """ We want to return a JSON file with the number of orgs and public figures who support this particular measure """ # This implementation is built to make only two database calls. All other calculations are done here in the # application layer position_list_manager = PositionListManager() all_positions_count_for_contest_measure = \ position_list_manager.retrieve_public_positions_count_for_contest_measure( measure_id, measure_we_vote_id, stance_we_are_looking_for) if positive_value_exists(measure_id) or positive_value_exists( measure_we_vote_id): contest_measure_manager = ContestMeasureManager() # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(measure_id): measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id( measure_id) elif positive_value_exists(measure_we_vote_id): measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id( measure_we_vote_id) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_PUBLIC_POSITION_COUNT_FOR_CONTEST_MEASURE', 'success': True, 'count': all_positions_count_for_contest_measure, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } results = { 'json_data': json_data, } return results
def positions_count_for_contest_measure( voter_id, measure_id, measure_we_vote_id, stance_we_are_looking_for, show_positions_this_voter_follows=True): """ We want to return a JSON file with the number of orgs, friends and public figures the voter follows who support this particular measure """ # This implementation is built to make limited database calls. We do as many calculations as we can here in the # application layer position_list_manager = PositionListManager() all_positions_list_for_contest_measure = \ position_list_manager.retrieve_all_positions_for_contest_measure( measure_id, measure_we_vote_id, stance_we_are_looking_for) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the measure object # so we make sure we have both of these values to return if positive_value_exists(measure_id): contest_measure_manager = ContestMeasureManager() results = contest_measure_manager.retrieve_contest_measure_from_id( measure_id) if results['contest_measure_found']: contest_measure = results['contest_measure'] measure_we_vote_id = contest_measure.we_vote_id elif positive_value_exists(measure_we_vote_id): contest_measure_manager = ContestMeasureManager() 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'] measure_id = contest_measure.id if show_positions_this_voter_follows: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_contest_measure, organizations_followed_by_voter) positions_followed_count = len(positions_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITION_COUNT_FOR_CONTEST_MEASURE', 'success': True, 'count': positions_followed_count, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } results = { 'json_data': json_data, } return results else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_contest_measure, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED_COUNT_FOR_CONTEST_MEASURE', 'success': True, 'count': positions_not_followed_count, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } results = { 'json_data': json_data, } return results
def process_contest_referendum_from_structured_json( one_contest_referendum_structured_json, google_civic_election_id, ocd_division_id, local_ballot_order, state_code, voter_id, polling_location_we_vote_id): """ "referendumTitle": "Proposition 45", "referendumSubtitle": "Healthcare Insurance. Rate Changes. Initiative Statute.", "referendumUrl": "http://vig.cdn.sos.ca.gov/2014/general/en/pdf/proposition-45-title-summary-analysis.pdf", "district" <= this is an array """ referendum_title = one_contest_referendum_structured_json['referendumTitle'] if \ 'referendumTitle' in one_contest_referendum_structured_json else '' referendum_subtitle = one_contest_referendum_structured_json['referendumSubtitle'] if \ 'referendumSubtitle' in one_contest_referendum_structured_json else '' referendum_url = one_contest_referendum_structured_json['referendumUrl'] if \ 'referendumUrl' in one_contest_referendum_structured_json else '' referendum_text = one_contest_referendum_structured_json['referendumText'] if \ 'referendumText' in one_contest_referendum_structured_json else '' # These following fields exist for both candidates and referendum results = process_contest_common_fields_from_structured_json(one_contest_referendum_structured_json) google_ballot_placement = results['ballot_placement'] # A number specifying the position of this contest # on the voter's ballot. primary_party = results['primary_party'] # If this is a partisan election, the name of the party it is for. district_name = results['district_name'] # The name of the district. district_scope = results['district_scope'] # The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_id = results['district_id'] # Note that all of the information saved here is independent of a particular voter we_vote_id = '' if google_civic_election_id and (district_id or district_name) and referendum_title: update_contest_measure_values = { # Values we search against 'google_civic_election_id': google_civic_election_id, 'state_code': state_code.lower(), # Not required for cases of federal offices 'district_id': district_id, 'district_name': district_name, 'measure_title': referendum_title, # The rest of the values 'measure_subtitle': referendum_subtitle, 'measure_url': referendum_url, 'measure_text': referendum_text, 'ocd_division_id': ocd_division_id, 'primary_party': primary_party, 'district_scope': district_scope, } contest_measure_manager = ContestMeasureManager() update_or_create_contest_measure_results = contest_measure_manager.update_or_create_contest_measure( we_vote_id, google_civic_election_id, district_id, district_name, referendum_title, state_code, update_contest_measure_values) # , create_contest_measure_values else: update_or_create_contest_measure_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } if update_or_create_contest_measure_results['success']: contest_measure = update_or_create_contest_measure_results['contest_measure'] contest_measure_id = contest_measure.id contest_measure_we_vote_id = contest_measure.we_vote_id ballot_item_display_name = contest_measure.measure_title measure_subtitle = contest_measure.measure_subtitle ballot_item_manager = BallotItemManager() # If a voter_id was passed in, save an entry for this office for the voter's ballot if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_voter( voter_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location_we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) return update_or_create_contest_measure_results
def ballot_item_list_edit_process_view(request): """ Process the new or edit ballot form :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) ballot_returned_id = convert_to_int( request.POST.get('ballot_returned_id', 0)) google_civic_election_id = request.POST.get('google_civic_election_id', 0) polling_location_id = convert_to_int( request.POST.get('polling_location_id', 0)) polling_location_city = request.POST.get('polling_location_city', '') polling_location_zip = request.POST.get('polling_location_zip', '') contest_office1_id = request.POST.get('contest_office1_id', 0) contest_office1_order = request.POST.get('contest_office1_order', 0) contest_measure1_id = request.POST.get('contest_measure1_id', 0) election_local_id = 0 # Find existing ballot_returned ballot_returned_found = False ballot_returned = BallotReturned() if positive_value_exists(ballot_returned_id): try: ballot_returned_query = BallotReturned.objects.filter( id=ballot_returned_id) if len(ballot_returned_query): ballot_returned = ballot_returned_query[0] ballot_returned_found = True except Exception as e: pass election_manager = ElectionManager() polling_location_manager = PollingLocationManager() polling_location = PollingLocation() polling_location_found = False try: if ballot_returned_found: # Update # Check to see if this is a We Vote-created election is_we_vote_google_civic_election_id = True \ if convert_to_int(ballot_returned.google_civic_election_id) >= 1000000 \ else False results = election_manager.retrieve_election( ballot_returned.google_civic_election_id) if results['election_found']: election = results['election'] election_local_id = election.id # polling_location must be found # We cannot change a polling location once saved, so we ignore the incoming polling_location_id here results = polling_location_manager.retrieve_polling_location_by_id( 0, ballot_returned.polling_location_we_vote_id) if results['polling_location_found']: polling_location = results['polling_location'] polling_location_found = True else: # Create new ballot_returned entry # election must be found election_results = election_manager.retrieve_election( google_civic_election_id) if election_results['election_found']: election = election_results['election'] election_local_id = election.id state_code = election.get_election_state() else: messages.add_message( request, messages.ERROR, 'Could not find election -- ' 'required to save ballot_returned.') return HttpResponseRedirect( reverse('ballot:ballot_item_list_edit', args=(ballot_returned_id, )) + "?google_civic_election_id=" + str(google_civic_election_id) + "&polling_location_id=" + str(polling_location_id) + "&polling_location_city=" + polling_location_city + "&polling_location_zip=" + str(polling_location_zip)) # polling_location must be found if positive_value_exists(polling_location_id): results = polling_location_manager.retrieve_polling_location_by_id( polling_location_id) if results['polling_location_found']: polling_location = results['polling_location'] polling_location_found = True if not polling_location_found: messages.add_message( request, messages.ERROR, 'Could not find polling_location -- ' 'required to save ballot_returned.') return HttpResponseRedirect( reverse('ballot:ballot_item_list_edit', args=(ballot_returned_id, )) + "?google_civic_election_id=" + str(google_civic_election_id) + "&polling_location_id=" + str(polling_location_id) + "&polling_location_city=" + polling_location_city + "&polling_location_zip=" + str(polling_location_zip)) ballot_returned = BallotReturned( election_date=election.election_day_text, election_description_text=election.election_name, google_civic_election_id=google_civic_election_id, polling_location_we_vote_id=polling_location.we_vote_id, normalized_city=polling_location.city, normalized_line1=polling_location.line1, normalized_line2=polling_location.line2, normalized_state=polling_location.state, normalized_zip=polling_location.get_formatted_zip(), text_for_map_search=polling_location.get_text_for_map_search(), ) ballot_returned.save() ballot_returned_id = ballot_returned.id ballot_returned_found = True messages.add_message(request, messages.INFO, 'New ballot_returned saved.') # ####################################### # Make sure we have saved a latitude and longitude for the ballot_returned entry if ballot_returned_found and positive_value_exists( ballot_returned.text_for_map_search): if not ballot_returned.latitude or not ballot_returned.longitude: google_client = get_geocoder_for_service('google')() location = google_client.geocode( ballot_returned.text_for_map_search) if location is None: status = 'Could not find location matching "{}"'.format( ballot_returned.text_for_map_search) else: ballot_returned.latitude = location.latitude ballot_returned.longitude = location.longitude ballot_returned.save() # ####################################### # Now create new ballot_item entries # Contest Office 1 ballot_item_manager = BallotItemManager() contest_office_manager = ContestOfficeManager() results = contest_office_manager.retrieve_contest_office( contest_office1_id) if results['contest_office_found']: contest_office = results['contest_office'] ballot_item_display_name = contest_office.office_name google_ballot_placement = 0 measure_subtitle = '' local_ballot_order = contest_office1_order if positive_value_exists( contest_office1_order) else 0 results = ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location.we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office.id, contest_office.we_vote_id) if results['new_ballot_item_created']: messages.add_message(request, messages.INFO, 'Office 1 added.') else: messages.add_message(request, messages.ERROR, 'Office 1 could not be added.') # Contest Measure 1 ballot_item_manager = BallotItemManager() contest_measure_manager = ContestMeasureManager() results = contest_measure_manager.retrieve_contest_measure( contest_measure1_id) if results['contest_measure_found']: contest_measure = results['contest_measure'] google_ballot_placement = 0 ballot_item_display_name = contest_measure.measure_title contest_office_id = 0 contest_office_we_vote_id = '' local_ballot_order = 0 ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location.we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, contest_measure.measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure.id) except Exception as e: messages.add_message(request, messages.ERROR, 'Could not save ballot_returned.') return HttpResponseRedirect( reverse('ballot:ballot_item_list_edit', args=(ballot_returned_id, )) + "?google_civic_election_id=" + str(google_civic_election_id) + "&polling_location_id=" + str(polling_location_id) + "&polling_location_city=" + polling_location_city + "&polling_location_zip=" + str(polling_location_zip))
def voter_supporting_save_for_api(voter_device_id, candidate_id, candidate_we_vote_id, measure_id, measure_we_vote_id): # Get voter_id from the voter_device_id so we can know who is supporting/opposing, voterSupportingSave results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } 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): json_data = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') position_entered_manager = PositionEnteredManager() if positive_value_exists(candidate_id) or positive_value_exists(candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(candidate_id): candidate_we_vote_id = candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(candidate_id) elif positive_value_exists(candidate_we_vote_id): candidate_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id(candidate_we_vote_id) results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign(voter_id, candidate_id) status = "SUPPORTING_CANDIDATE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(measure_id) or positive_value_exists(measure_we_vote_id): contest_measure_manager = ContestMeasureManager() # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(measure_id): measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(measure_id) elif positive_value_exists(measure_we_vote_id): measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id(measure_we_vote_id) results = position_entered_manager.toggle_on_voter_support_for_contest_measure(voter_id, measure_id) status = "SUPPORTING_MEASURE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_SAVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def toggle_voter_starred_item( self, voter_id, star_status, candidate_campaign_id=0, contest_office_id=0, contest_measure_id=0, contest_office_we_vote_id='', candidate_campaign_we_vote_id='', contest_measure_we_vote_id=''): # Does a star_item entry exist from this voter already exist? star_item_manager = StarItemManager() star_item_id = 0 results = star_item_manager.retrieve_star_item( star_item_id, voter_id, contest_office_id, candidate_campaign_id, contest_measure_id) star_item_on_stage_found = False star_item_on_stage_id = 0 star_item_on_stage = StarItem() if results['star_item_found']: star_item_on_stage = results['star_item'] # Update this star_item entry with new values - we do not delete because we might be able to use try: star_item_on_stage.star_status = star_status # We don't need to update date_last_changed here because set set auto_now=True in the field star_item_on_stage.save() star_item_on_stage_id = star_item_on_stage.id star_item_on_stage_found = True status = 'UPDATE ' + star_status except Exception as e: status = 'FAILED_TO_UPDATE ' + star_status handle_record_not_saved_exception(e, logger=logger, exception_message_optional=status) elif results['MultipleObjectsReturned']: logger.warn("star_item: delete all but one and take it over?") status = 'TOGGLE_ITEM_STARRED MultipleObjectsReturned ' + star_status elif results['DoesNotExist']: try: # Create new star_item entry if candidate_campaign_id and not candidate_campaign_we_vote_id: candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_we_vote_id = \ candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(candidate_campaign_id) if contest_measure_id and not contest_measure_we_vote_id: contest_measure_manager = ContestMeasureManager() contest_measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id( contest_measure_id) if contest_office_id and not contest_office_we_vote_id: contest_office_manager = ContestOfficeManager() contest_office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id( contest_office_id) # NOTE: For speed purposes, we are not validating the existence of the items being starred # although we could if the we_vote_id is not returned. star_item_on_stage = StarItem( voter_id=voter_id, candidate_campaign_id=candidate_campaign_id, candidate_campaign_we_vote_id=candidate_campaign_we_vote_id, contest_office_id=contest_office_id, contest_office_we_vote_id=contest_office_we_vote_id, contest_measure_id=contest_measure_id, contest_measure_we_vote_id=contest_measure_we_vote_id, star_status=star_status, # We don't need to update date_last_changed here because set set auto_now=True in the field ) star_item_on_stage.save() star_item_on_stage_id = star_item_on_stage.id star_item_on_stage_found = True status = 'CREATE ' + star_status except Exception as e: status = 'FAILED_TO_UPDATE ' + star_status handle_record_not_saved_exception(e, logger=logger, exception_message_optional=status) else: status = results['status'] results = { 'success': True if star_item_on_stage_found else False, 'status': status, 'star_item_found': star_item_on_stage_found, 'star_item_id': star_item_on_stage_id, 'star_item': star_item_on_stage, } return results
def voter_star_status_retrieve_for_api(voter_device_id, office_id, office_we_vote_id, candidate_id, candidate_we_vote_id, measure_id, measure_we_vote_id): # Get voter_id from the voter_device_id so we can know who is doing the starring results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'is_starred': False, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } 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): json_data = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'is_starred': False, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') star_item_manager = StarItemManager() if positive_value_exists(office_id) or positive_value_exists(office_we_vote_id): contest_office_manager = ContestOfficeManager() # Since we can take in either office_id or office_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(office_id): office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id(office_id) elif positive_value_exists(office_we_vote_id): office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id(office_we_vote_id) # Zero out the unused values star_item_id = 0 candidate_campaign_id = 0 contest_measure_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, office_id, candidate_campaign_id, contest_measure_id) status = results['status'] success = results['success'] is_starred = results['is_starred'] json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'ballot_item_id': convert_to_int(office_id), 'ballot_item_we_vote_id': office_we_vote_id, 'kind_of_ballot_item': OFFICE, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), } return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(candidate_id) or positive_value_exists(candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(candidate_id): candidate_we_vote_id = candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(candidate_id) elif positive_value_exists(candidate_we_vote_id): candidate_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id(candidate_we_vote_id) # Zero out the unused values star_item_id = 0 contest_office_id = 0 contest_measure_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_id, contest_measure_id) status = results['status'] success = results['success'] is_starred = results['is_starred'] json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), } return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(measure_id) or positive_value_exists(measure_we_vote_id): contest_measure_manager = ContestMeasureManager() # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(measure_id): measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id(measure_id) elif positive_value_exists(measure_we_vote_id): measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id(measure_we_vote_id) # Zero out the unused values star_item_id = 0 contest_office_id = 0 candidate_campaign_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_campaign_id, measure_id) status = results['status'] success = results['success'] is_starred = results['is_starred'] json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), } return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_SAVE-OFFICE_ID_AND_CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False is_starred = False json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def organization_position_new_view(request, organization_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer authority_results = retrieve_voter_authority(request) if not voter_has_authority(request, authority_required, authority_results): return redirect_to_sign_in_page(request, authority_required) google_civic_election_id = request.GET.get('google_civic_election_id', 0) candidate_we_vote_id = request.GET.get('candidate_we_vote_id', False) measure_we_vote_id = request.GET.get('measure_we_vote_id', False) # Take in some incoming values candidate_and_measure_not_found = request.GET.get('candidate_and_measure_not_found', False) stance = request.GET.get('stance', SUPPORT) # Set a default if stance comes in empty statement_text = request.GET.get('statement_text', '') # Set a default if stance comes in empty more_info_url = request.GET.get('more_info_url', '') # We pass candidate_we_vote_id to this page to pre-populate the form candidate_campaign_id = 0 if positive_value_exists(candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() results = candidate_campaign_manager.retrieve_candidate_campaign_from_we_vote_id(candidate_we_vote_id) if results['candidate_campaign_found']: candidate_campaign = results['candidate_campaign'] candidate_campaign_id = candidate_campaign.id # We pass candidate_we_vote_id to this page to pre-populate the form contest_measure_id = 0 if positive_value_exists(measure_we_vote_id): contest_measure_manager = ContestMeasureManager() 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'] contest_measure_id = contest_measure.id messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) all_is_well = True organization_on_stage_found = False organization_on_stage = Organization() try: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_found = True except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Organization.DoesNotExist: # This is fine, create new pass if not organization_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization when trying to create a new position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Prepare a drop down of candidates competing in this election candidate_campaign_list = CandidateCampaignListManager() candidate_campaigns_for_this_election_list = [] results = candidate_campaign_list.retrieve_all_candidates_for_upcoming_election(google_civic_election_id, True) if results['candidate_list_found']: candidate_campaigns_for_this_election_list = results['candidate_list_objects'] # Prepare a drop down of measures in this election contest_measure_list = ContestMeasureList() contest_measures_for_this_election_list = [] results = contest_measure_list.retrieve_all_measures_for_upcoming_election(google_civic_election_id, True) if results['measure_list_found']: contest_measures_for_this_election_list = results['measure_list_objects'] try: organization_position_list = PositionEntered.objects.order_by('stance') organization_position_list = organization_position_list.filter(organization_id=organization_id) if positive_value_exists(google_civic_election_id): organization_position_list = organization_position_list.filter( google_civic_election_id=google_civic_election_id) organization_position_list = organization_position_list.order_by( 'google_civic_election_id', '-vote_smart_time_span') if len(organization_position_list): organization_position_list_found = True except Exception as e: organization_position_list = [] if all_is_well: election_list = Election.objects.order_by('-election_day_text') template_values = { 'candidate_campaigns_for_this_election_list': candidate_campaigns_for_this_election_list, 'candidate_campaign_id': candidate_campaign_id, 'contest_measures_for_this_election_list': contest_measures_for_this_election_list, 'contest_measure_id': contest_measure_id, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_candidate_campaign_id': 0, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': stance, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, 'organization_position_list': organization_position_list, 'voter_authority': authority_results, # Incoming values from error state 'candidate_and_measure_not_found': candidate_and_measure_not_found, 'stance': stance, 'statement_text': statement_text, 'more_info_url': more_info_url, } return render(request, 'organization/organization_position_edit.html', template_values)
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 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 process_contest_referendum_from_structured_json( one_contest_referendum_structured_json, google_civic_election_id, ocd_division_id, local_ballot_order, state_code, voter_id, polling_location_we_vote_id): """ "referendumTitle": "Proposition 45", "referendumSubtitle": "Healthcare Insurance. Rate Changes. Initiative Statute.", "referendumUrl": "http://vig.cdn.sos.ca.gov/2014/general/en/pdf/proposition-45-title-summary-analysis.pdf", "district" <= this is an array """ referendum_title = one_contest_referendum_structured_json['referendumTitle'] if \ 'referendumTitle' in one_contest_referendum_structured_json else '' referendum_subtitle = one_contest_referendum_structured_json['referendumSubtitle'] if \ 'referendumSubtitle' in one_contest_referendum_structured_json else '' referendum_url = one_contest_referendum_structured_json['referendumUrl'] if \ 'referendumUrl' in one_contest_referendum_structured_json else '' referendum_text = one_contest_referendum_structured_json['referendumText'] if \ 'referendumText' in one_contest_referendum_structured_json else '' # These following fields exist for both candidates and referendum results = process_contest_common_fields_from_structured_json( one_contest_referendum_structured_json) google_ballot_placement = results[ 'ballot_placement'] # A number specifying the position of this contest # on the voter's ballot. primary_party = results[ 'primary_party'] # If this is a partisan election, the name of the party it is for. district_name = results['district_name'] # The name of the district. district_scope = results[ 'district_scope'] # The geographic scope of this district. If unspecified the # district's geography is not known. One of: national, statewide, congressional, stateUpper, stateLower, # countywide, judicial, schoolBoard, cityWide, township, countyCouncil, cityCouncil, ward, special district_id = results['district_id'] # Note that all of the information saved here is independent of a particular voter we_vote_id = '' if google_civic_election_id and (district_id or district_name) and referendum_title: update_contest_measure_values = { # Values we search against 'google_civic_election_id': google_civic_election_id, 'state_code': state_code.lower(), # Not required for cases of federal offices 'district_id': district_id, 'district_name': district_name, 'measure_title': referendum_title, # The rest of the values 'measure_subtitle': referendum_subtitle, 'measure_url': referendum_url, 'measure_text': referendum_text, 'ocd_division_id': ocd_division_id, 'primary_party': primary_party, 'district_scope': district_scope, } contest_measure_manager = ContestMeasureManager() update_or_create_contest_measure_results = contest_measure_manager.update_or_create_contest_measure( we_vote_id, google_civic_election_id, district_id, district_name, referendum_title, state_code, update_contest_measure_values) # , create_contest_measure_values else: update_or_create_contest_measure_results = { 'success': False, 'saved': 0, 'updated': 0, 'not_processed': 1, } if update_or_create_contest_measure_results['success']: contest_measure = update_or_create_contest_measure_results[ 'contest_measure'] contest_measure_id = contest_measure.id contest_measure_we_vote_id = contest_measure.we_vote_id ballot_item_display_name = contest_measure.measure_title measure_subtitle = contest_measure.measure_subtitle ballot_item_manager = BallotItemManager() # If a voter_id was passed in, save an entry for this office for the voter's ballot if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_voter( voter_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id) \ and positive_value_exists(contest_measure_id): contest_office_id = 0 contest_office_we_vote_id = '' ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location_we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure_id, contest_measure_we_vote_id) return update_or_create_contest_measure_results
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=()))
def ballot_item_list_edit_process_view(request): """ Process the new or edit ballot form :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) ballot_returned_id = convert_to_int(request.POST.get('ballot_returned_id', 0)) google_civic_election_id = request.POST.get('google_civic_election_id', 0) polling_location_id = convert_to_int(request.POST.get('polling_location_id', 0)) polling_location_city = request.POST.get('polling_location_city', '') polling_location_zip = request.POST.get('polling_location_zip', '') contest_office1_id = request.POST.get('contest_office1_id', 0) contest_office1_order = request.POST.get('contest_office1_order', 0) contest_measure1_id = request.POST.get('contest_measure1_id', 0) election_local_id = 0 # Find existing ballot_returned ballot_returned_found = False ballot_returned = BallotReturned() if positive_value_exists(ballot_returned_id): try: ballot_returned_query = BallotReturned.objects.filter(id=ballot_returned_id) if len(ballot_returned_query): ballot_returned = ballot_returned_query[0] ballot_returned_found = True except Exception as e: pass election_manager = ElectionManager() polling_location_manager = PollingLocationManager() polling_location = PollingLocation() polling_location_found = False try: if ballot_returned_found: # Update # Check to see if this is a We Vote-created election is_we_vote_google_civic_election_id = True \ if convert_to_int(ballot_returned.google_civic_election_id) >= 1000000 \ else False results = election_manager.retrieve_election(ballot_returned.google_civic_election_id) if results['election_found']: election = results['election'] election_local_id = election.id # polling_location must be found # We cannot change a polling location once saved, so we ignore the incoming polling_location_id here results = polling_location_manager.retrieve_polling_location_by_id( 0, ballot_returned.polling_location_we_vote_id) if results['polling_location_found']: polling_location = results['polling_location'] polling_location_found = True else: # Create new ballot_returned entry # election must be found election_results = election_manager.retrieve_election(google_civic_election_id) if election_results['election_found']: election = election_results['election'] election_local_id = election.id state_code = election.get_election_state() else: messages.add_message(request, messages.ERROR, 'Could not find election -- ' 'required to save ballot_returned.') return HttpResponseRedirect(reverse('ballot:ballot_item_list_edit', args=(ballot_returned_id,)) + "?google_civic_election_id=" + str(google_civic_election_id) + "&polling_location_id=" + str(polling_location_id) + "&polling_location_city=" + polling_location_city + "&polling_location_zip=" + str(polling_location_zip) ) # polling_location must be found if positive_value_exists(polling_location_id): results = polling_location_manager.retrieve_polling_location_by_id(polling_location_id) if results['polling_location_found']: polling_location = results['polling_location'] polling_location_found = True if not polling_location_found: messages.add_message(request, messages.ERROR, 'Could not find polling_location -- ' 'required to save ballot_returned.') return HttpResponseRedirect(reverse('ballot:ballot_item_list_edit', args=(ballot_returned_id,)) + "?google_civic_election_id=" + str(google_civic_election_id) + "&polling_location_id=" + str(polling_location_id) + "&polling_location_city=" + polling_location_city + "&polling_location_zip=" + str(polling_location_zip) ) ballot_returned = BallotReturned( election_date=election.election_day_text, election_description_text=election.election_name, google_civic_election_id=google_civic_election_id, polling_location_we_vote_id=polling_location.we_vote_id, normalized_city=polling_location.city, normalized_line1=polling_location.line1, normalized_line2=polling_location.line2, normalized_state=polling_location.state, normalized_zip=polling_location.get_formatted_zip(), text_for_map_search=polling_location.get_text_for_map_search(), ) ballot_returned.save() ballot_returned_id = ballot_returned.id ballot_returned_found = True messages.add_message(request, messages.INFO, 'New ballot_returned saved.') # ####################################### # Make sure we have saved a latitude and longitude for the ballot_returned entry if ballot_returned_found and positive_value_exists(ballot_returned.text_for_map_search): if not ballot_returned.latitude or not ballot_returned.longitude: google_client = get_geocoder_for_service('google')() location = google_client.geocode(ballot_returned.text_for_map_search) if location is None: status = 'Could not find location matching "{}"'.format(ballot_returned.text_for_map_search) else: ballot_returned.latitude = location.latitude ballot_returned.longitude = location.longitude ballot_returned.save() # ####################################### # Now create new ballot_item entries # Contest Office 1 ballot_item_manager = BallotItemManager() contest_office_manager = ContestOfficeManager() results = contest_office_manager.retrieve_contest_office(contest_office1_id) if results['contest_office_found']: contest_office = results['contest_office'] ballot_item_display_name = contest_office.office_name google_ballot_placement = 0 measure_subtitle = '' local_ballot_order = contest_office1_order if positive_value_exists(contest_office1_order) else 0 results = ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location.we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, measure_subtitle, local_ballot_order, contest_office.id, contest_office.we_vote_id) if results['new_ballot_item_created']: messages.add_message(request, messages.INFO, 'Office 1 added.') else: messages.add_message(request, messages.ERROR, 'Office 1 could not be added.') # Contest Measure 1 ballot_item_manager = BallotItemManager() contest_measure_manager = ContestMeasureManager() results = contest_measure_manager.retrieve_contest_measure(contest_measure1_id) if results['contest_measure_found']: contest_measure = results['contest_measure'] google_ballot_placement = 0 ballot_item_display_name = contest_measure.measure_title contest_office_id = 0 contest_office_we_vote_id = '' local_ballot_order = 0 ballot_item_manager.update_or_create_ballot_item_for_polling_location( polling_location.we_vote_id, google_civic_election_id, google_ballot_placement, ballot_item_display_name, contest_measure.measure_subtitle, local_ballot_order, contest_office_id, contest_office_we_vote_id, contest_measure.id) except Exception as e: messages.add_message(request, messages.ERROR, 'Could not save ballot_returned.') return HttpResponseRedirect(reverse('ballot:ballot_item_list_edit', args=(ballot_returned_id,)) + "?google_civic_election_id=" + str(google_civic_election_id) + "&polling_location_id=" + str(polling_location_id) + "&polling_location_city=" + polling_location_city + "&polling_location_zip=" + str(polling_location_zip) )
def voter_supporting_save_for_api(voter_device_id, candidate_id, candidate_we_vote_id, measure_id, measure_we_vote_id): # Get voter_id from the voter_device_id so we can know who is supporting/opposing, voterSupportingSave results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } 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): json_data = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') position_entered_manager = PositionEnteredManager() if positive_value_exists(candidate_id) or positive_value_exists( candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(candidate_id): candidate_we_vote_id = candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id( candidate_id) elif positive_value_exists(candidate_we_vote_id): candidate_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id( candidate_we_vote_id) results = position_entered_manager.toggle_on_voter_support_for_candidate_campaign( voter_id, candidate_id) status = "SUPPORTING_CANDIDATE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, } return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(measure_id) or positive_value_exists( measure_we_vote_id): contest_measure_manager = ContestMeasureManager() # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(measure_id): measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id( measure_id) elif positive_value_exists(measure_we_vote_id): measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id( measure_we_vote_id) results = position_entered_manager.toggle_on_voter_support_for_contest_measure( voter_id, measure_id) status = "SUPPORTING_MEASURE " + results['status'] success = results['success'] json_data = { 'status': status, 'success': success, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_SAVE-CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False json_data = { 'status': status, 'success': success, 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_star_status_retrieve_for_api(voter_device_id, office_id, office_we_vote_id, candidate_id, candidate_we_vote_id, measure_id, measure_we_vote_id): # Get voter_id from the voter_device_id so we can know who is doing the starring results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_device_id': voter_device_id, 'is_starred': False, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } 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): json_data = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'voter_device_id': voter_device_id, 'is_starred': False, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json') star_item_manager = StarItemManager() if positive_value_exists(office_id) or positive_value_exists( office_we_vote_id): contest_office_manager = ContestOfficeManager() # Since we can take in either office_id or office_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(office_id): office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id( office_id) elif positive_value_exists(office_we_vote_id): office_id = contest_office_manager.fetch_contest_office_id_from_we_vote_id( office_we_vote_id) # Zero out the unused values star_item_id = 0 candidate_campaign_id = 0 contest_measure_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, office_id, candidate_campaign_id, contest_measure_id) status = results['status'] success = results['success'] is_starred = results['is_starred'] json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'ballot_item_id': convert_to_int(office_id), 'ballot_item_we_vote_id': office_we_vote_id, 'kind_of_ballot_item': OFFICE, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), } return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(candidate_id) or positive_value_exists( candidate_we_vote_id): candidate_campaign_manager = CandidateCampaignManager() # Since we can take in either candidate_id or candidate_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(candidate_id): candidate_we_vote_id = candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id( candidate_id) elif positive_value_exists(candidate_we_vote_id): candidate_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_we_vote_id( candidate_we_vote_id) # Zero out the unused values star_item_id = 0 contest_office_id = 0 contest_measure_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_id, contest_measure_id) status = results['status'] success = results['success'] is_starred = results['is_starred'] json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'ballot_item_id': convert_to_int(candidate_id), 'ballot_item_we_vote_id': candidate_we_vote_id, 'kind_of_ballot_item': CANDIDATE, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), } return HttpResponse(json.dumps(json_data), content_type='application/json') elif positive_value_exists(measure_id) or positive_value_exists( measure_we_vote_id): contest_measure_manager = ContestMeasureManager() # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the value we don't have if positive_value_exists(measure_id): measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id( measure_id) elif positive_value_exists(measure_we_vote_id): measure_id = contest_measure_manager.fetch_contest_measure_id_from_we_vote_id( measure_we_vote_id) # Zero out the unused values star_item_id = 0 contest_office_id = 0 candidate_campaign_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_campaign_id, measure_id) status = results['status'] success = results['success'] is_starred = results['is_starred'] json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), } return HttpResponse(json.dumps(json_data), content_type='application/json') else: status = 'UNABLE_TO_SAVE-OFFICE_ID_AND_CANDIDATE_ID_AND_MEASURE_ID_MISSING' success = False is_starred = False json_data = { 'status': status, 'success': success, 'voter_device_id': voter_device_id, 'is_starred': is_starred, 'office_id': convert_to_int(office_id), 'candidate_id': convert_to_int(candidate_id), 'measure_id': convert_to_int(measure_id), 'ballot_item_id': 0, 'ballot_item_we_vote_id': '', 'kind_of_ballot_item': '', } return HttpResponse(json.dumps(json_data), content_type='application/json')
def positions_count_for_contest_measure(voter_id, measure_id, measure_we_vote_id, stance_we_are_looking_for, show_positions_this_voter_follows=True): """ We want to return a JSON file with the number of orgs, friends and public figures the voter follows who support this particular measure """ # This implementation is built to make limited database calls. We do as many calculations as we can here in the # application layer position_list_manager = PositionListManager() all_positions_list_for_contest_measure = \ position_list_manager.retrieve_all_positions_for_contest_measure( measure_id, measure_we_vote_id, stance_we_are_looking_for) follow_organization_list_manager = FollowOrganizationList() organizations_followed_by_voter = \ follow_organization_list_manager.retrieve_follow_organization_by_voter_id_simple_id_array(voter_id) # Since we can take in either measure_id or measure_we_vote_id, we need to retrieve the measure object # so we make sure we have both of these values to return if positive_value_exists(measure_id): contest_measure_manager = ContestMeasureManager() results = contest_measure_manager.retrieve_contest_measure_from_id(measure_id) if results['contest_measure_found']: contest_measure = results['contest_measure'] measure_we_vote_id = contest_measure.we_vote_id elif positive_value_exists(measure_we_vote_id): contest_measure_manager = ContestMeasureManager() 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'] measure_id = contest_measure.id if show_positions_this_voter_follows: positions_followed = position_list_manager.calculate_positions_followed_by_voter( voter_id, all_positions_list_for_contest_measure, organizations_followed_by_voter) positions_followed_count = len(positions_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITION_COUNT_FOR_CONTEST_MEASURE', 'success': True, 'count': positions_followed_count, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } results = { 'json_data': json_data, } return results else: positions_not_followed = position_list_manager.calculate_positions_not_followed_by_voter( all_positions_list_for_contest_measure, organizations_followed_by_voter) positions_not_followed_count = len(positions_not_followed) json_data = { 'status': 'SUCCESSFUL_RETRIEVE_OF_POSITIONS_NOT_FOLLOWED_COUNT_FOR_CONTEST_MEASURE', 'success': True, 'count': positions_not_followed_count, 'ballot_item_id': convert_to_int(measure_id), 'ballot_item_we_vote_id': measure_we_vote_id, 'kind_of_ballot_item': MEASURE, } results = { 'json_data': json_data, } return results
def toggle_voter_starred_item(self, voter_id, star_status, candidate_campaign_id=0, contest_office_id=0, contest_measure_id=0, contest_office_we_vote_id='', candidate_campaign_we_vote_id='', contest_measure_we_vote_id=''): # Does a star_item entry exist from this voter already exist? star_item_manager = StarItemManager() star_item_id = 0 results = star_item_manager.retrieve_star_item(star_item_id, voter_id, contest_office_id, candidate_campaign_id, contest_measure_id) star_item_on_stage_found = False star_item_on_stage_id = 0 star_item_on_stage = StarItem() if results['star_item_found']: star_item_on_stage = results['star_item'] # Update this star_item entry with new values - we do not delete because we might be able to use try: star_item_on_stage.star_status = star_status # We don't need to update date_last_changed here because set set auto_now=True in the field star_item_on_stage.save() star_item_on_stage_id = star_item_on_stage.id star_item_on_stage_found = True status = 'UPDATE ' + star_status except Exception as e: status = 'FAILED_TO_UPDATE ' + star_status handle_record_not_saved_exception( e, logger=logger, exception_message_optional=status) elif results['MultipleObjectsReturned']: logger.warn("star_item: delete all but one and take it over?") status = 'TOGGLE_ITEM_STARRED MultipleObjectsReturned ' + star_status elif results['DoesNotExist']: try: # Create new star_item entry if candidate_campaign_id and not candidate_campaign_we_vote_id: candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_we_vote_id = \ candidate_campaign_manager.fetch_candidate_campaign_we_vote_id_from_id(candidate_campaign_id) if contest_measure_id and not contest_measure_we_vote_id: contest_measure_manager = ContestMeasureManager() contest_measure_we_vote_id = contest_measure_manager.fetch_contest_measure_we_vote_id_from_id( contest_measure_id) if contest_office_id and not contest_office_we_vote_id: contest_office_manager = ContestOfficeManager() contest_office_we_vote_id = contest_office_manager.fetch_contest_office_we_vote_id_from_id( contest_office_id) # NOTE: For speed purposes, we are not validating the existence of the items being starred # although we could if the we_vote_id is not returned. star_item_on_stage = StarItem( voter_id=voter_id, candidate_campaign_id=candidate_campaign_id, candidate_campaign_we_vote_id=candidate_campaign_we_vote_id, contest_office_id=contest_office_id, contest_office_we_vote_id=contest_office_we_vote_id, contest_measure_id=contest_measure_id, contest_measure_we_vote_id=contest_measure_we_vote_id, star_status=star_status, # We don't need to update date_last_changed here because set set auto_now=True in the field ) star_item_on_stage.save() star_item_on_stage_id = star_item_on_stage.id star_item_on_stage_found = True status = 'CREATE ' + star_status except Exception as e: status = 'FAILED_TO_UPDATE ' + star_status handle_record_not_saved_exception( e, logger=logger, exception_message_optional=status) else: status = results['status'] results = { 'success': True if star_item_on_stage_found else False, 'status': status, 'star_item_found': star_item_on_stage_found, 'star_item_id': star_item_on_stage_id, 'star_item': star_item_on_stage, } return results