def organization_add_new_position_form_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) all_is_well = True organization_on_stage_found = False 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 = CandidateCampaignList() candidate_campaigns_for_this_election_list \ = candidate_campaign_list.retrieve_candidate_campaigns_for_this_election_list() if all_is_well: template_values = { 'candidate_campaigns_for_this_election_list': candidate_campaigns_for_this_election_list, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_candidate_campaign_id': 0, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': SUPPORT, # Default stance } return render(request, 'organization/organization_position_edit.html', template_values)
def measure_edit_view(request, measure_id): messages_on_stage = get_messages(request) measure_id = convert_to_int(measure_id) measure_on_stage_found = False try: measure_on_stage = ContestMeasure.objects.get(id=measure_id) measure_on_stage_found = True except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) measure_on_stage = ContestMeasure() except ContestMeasure.DoesNotExist: # This is fine, create new measure_on_stage = ContestMeasure() pass if measure_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'measure': measure_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'measure/measure_edit.html', template_values)
def retrieve_organization(self, organization_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False organization_on_stage = Organization() organization_on_stage_id = 0 try: organization_on_stage = Organization.objects.get( id=organization_id) organization_on_stage_id = organization_on_stage.id except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True print "position.organization Found multiple" except Organization.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True print "position.organization did not find" organization_on_stage_found = True if organization_on_stage_id > 0 else False results = { 'success': True if organization_on_stage_found else False, 'organization_found': organization_on_stage_found, 'organization_id': organization_on_stage_id, 'organization': organization_on_stage, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results
def retrieve_politician(self, politician_id): # , id_we_vote=None error_result = False exception_does_not_exist = False exception_multiple_object_returned = False politician_on_stage = Politician() politician_on_stage_id = 0 try: if politician_id > 0: politician_on_stage = Politician.objects.get(id=politician_id) politician_on_stage_id = politician_on_stage.id # elif len(id_we_vote) > 0: # politician_on_stage = Politician.objects.get(id_we_vote=id_we_vote) # politician_on_stage_id = politician_on_stage.id except Politician.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) error_result = True exception_multiple_object_returned = True except Politician.DoesNotExist: error_result = True exception_does_not_exist = True # politician_on_stage_found2 = politician_on_stage_id > 0 # TODO Why not this simpler case? politician_on_stage_found = True if politician_on_stage_id > 0 else False results = { "success": True if politician_on_stage_found else False, "politician_found": politician_on_stage_found, "politician_id": politician_on_stage_id, "politician": politician_on_stage, "error_result": error_result, "DoesNotExist": exception_does_not_exist, "MultipleObjectsReturned": exception_multiple_object_returned, } return results
def voter_summary_view(request, voter_id): authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) voter_id = convert_to_int(voter_id) voter_on_stage_found = False try: voter_on_stage = Voter.objects.get(id=voter_id) voter_on_stage_found = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Voter.DoesNotExist: # This is fine, create new pass if voter_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'voter': voter_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'voter/voter_summary.html', template_values)
def retrieve_address(self, voter_address_id, voter_id=0, address_type=''): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_address_on_stage = VoterAddress() voter_address_has_value = False if not positive_value_exists(address_type): # Provide a default address_type = BALLOT_ADDRESS try: if positive_value_exists(voter_address_id): voter_address_on_stage = VoterAddress.objects.get(id=voter_address_id) voter_address_id = voter_address_on_stage.id voter_address_found = True status = "VOTER_ADDRESS_FOUND_BY_ID" success = True voter_address_has_value = True if positive_value_exists(voter_address_on_stage.text_for_map_search) \ else False elif positive_value_exists(voter_id) and address_type in (BALLOT_ADDRESS, MAILING_ADDRESS, FORMER_BALLOT_ADDRESS): voter_address_on_stage = VoterAddress.objects.get(voter_id=voter_id, address_type=address_type) # If still here, we found an existing address voter_address_id = voter_address_on_stage.id voter_address_found = True status = "VOTER_ADDRESS_FOUND_BY_VOTER_ID_AND_ADDRESS_TYPE" success = True voter_address_has_value = True if positive_value_exists(voter_address_on_stage.text_for_map_search) \ else False else: voter_address_found = False status = "VOTER_ADDRESS_NOT_FOUND-MISSING_REQUIRED_VARIABLES" success = False except VoterAddress.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) error_result = True status = "VOTER_ADDRESS_MULTIPLE_OBJECTS_RETURNED" exception_multiple_object_returned = True success = False voter_address_found = False except VoterAddress.DoesNotExist: error_result = True status = "VOTER_ADDRESS_DOES_NOT_EXIST" exception_does_not_exist = True success = True voter_address_found = False results = { 'success': success, 'status': status, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'voter_address_found': voter_address_found, 'voter_address_has_value': voter_address_has_value, 'voter_address_id': voter_address_id, 'voter_address': voter_address_on_stage, } return results
def election_edit_view(request, election_local_id): authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) election_local_id = convert_to_int(election_local_id) election_on_stage_found = False election_on_stage = Election() try: election_on_stage = Election.objects.get(id=election_local_id) election_on_stage_found = True except Election.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Election.DoesNotExist: # This is fine, create new pass if election_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'election': election_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, "election/election_edit.html", template_values)
def quick_info_summary_view(request, quick_info_id): # TODO to be updated authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) quick_info_id = convert_to_int(quick_info_id) quick_info_on_stage_found = False quick_info_on_stage = QuickInfo() try: quick_info_on_stage = QuickInfo.objects.get(id=quick_info_id) quick_info_on_stage_found = True except QuickInfo.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except QuickInfo.DoesNotExist: # This is fine, create new pass if quick_info_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'quick_info': quick_info_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'quick_info/quick_info_summary.html', template_values)
def retrieve_maplight_candidate(self, candidate_id, candidate_id_maplight=None, politician_id_maplight=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False maplight_candidate_on_stage = MapLightCandidate() try: if candidate_id > 0: maplight_candidate_on_stage = MapLightCandidate.objects.get(id=candidate_id) candidate_id = maplight_candidate_on_stage.id elif len(candidate_id_maplight) > 0: maplight_candidate_on_stage = MapLightCandidate.objects.get(candidate_id=candidate_id_maplight) candidate_id = maplight_candidate_on_stage.id elif len(politician_id_maplight) > 0: maplight_candidate_on_stage = MapLightCandidate.objects.get(politician_id=politician_id_maplight) candidate_id = maplight_candidate_on_stage.id except MapLightCandidate.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True except MapLightCandidate.DoesNotExist as e: exception_does_not_exist = True results = { 'success': True if candidate_id > 0 else False, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'maplight_candidate_found': True if candidate_id > 0 else False, 'candidate_id': candidate_id, 'maplight_candidate': maplight_candidate_on_stage, } return results
def retrieve_organization(self, organization_id, id_we_vote=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False organization_on_stage = Organization() organization_on_stage_id = 0 try: if organization_id > 0: organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_id = organization_on_stage.id elif len(id_we_vote) > 0: organization_on_stage = Organization.objects.get(id_we_vote=id_we_vote) organization_on_stage_id = organization_on_stage.id except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger) error_result = True exception_multiple_object_returned = True logger.warn("Organization.MultipleObjectsReturned") except Organization.DoesNotExist: error_result = True exception_does_not_exist = True logger.warn("Organization.DoesNotExist") organization_on_stage_found = True if organization_on_stage_id > 0 else False results = { 'success': True if organization_on_stage_found else False, 'organization_found': organization_on_stage_found, 'organization_id': organization_on_stage_id, 'organization': organization_on_stage, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results
def election_summary_view(request, election_local_id): messages_on_stage = get_messages(request) election_local_id = convert_to_int(election_local_id) election_on_stage_found = False election_on_stage = Election() try: election_on_stage = Election.objects.get(id=election_local_id) election_on_stage_found = True except Election.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Election.DoesNotExist: # This is fine, create new pass if election_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'election': election_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'election/election_summary.html', template_values)
def organization_edit_view(request, organization_id): # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) organization_on_stage_found = False 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 organization_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'organization/organization_edit.html', template_values)
def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() if value_exists(voter_id) and value_exists(google_civic_election_id) and value_exists( google_civic_district_ocd_id): try: google_civic_ballot_item_on_stage = GoogleCivicBallotItem.objects.get( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except GoogleCivicBallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except GoogleCivicBallotItem.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'success': True if google_civic_ballot_item_id > 0 else False, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'google_civic_ballot_item': google_civic_ballot_item_on_stage, } return results
def polling_location_summary_by_we_vote_id_view(request, polling_location_we_vote_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) polling_location_on_stage_found = False polling_location_on_stage = PollingLocation() try: polling_location_on_stage = PollingLocation.objects.get(we_vote_id=polling_location_we_vote_id) polling_location_on_stage_found = True except PollingLocation.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except PollingLocation.DoesNotExist: # This is fine, create new pass if polling_location_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'polling_location': polling_location_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'polling_location/polling_location_summary.html', template_values)
def position_edit_view(request, position_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) position_id = convert_to_int(position_id) position_on_stage_found = False try: position_on_stage = CandidateCampaign.objects.get(id=position_id) position_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist: # This is fine, create new pass if position_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'position': position_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'position/position_edit.html', template_values)
def retrieve_politician(self, politician_id): # , we_vote_id=None error_result = False exception_does_not_exist = False exception_multiple_object_returned = False politician_on_stage = Politician() politician_on_stage_id = 0 try: if politician_id > 0: politician_on_stage = Politician.objects.get(id=politician_id) politician_on_stage_id = politician_on_stage.id # elif len(we_vote_id) > 0: # politician_on_stage = Politician.objects.get(we_vote_id=we_vote_id) # politician_on_stage_id = politician_on_stage.id except Politician.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) error_result = True exception_multiple_object_returned = True except Politician.DoesNotExist: error_result = True exception_does_not_exist = True # politician_on_stage_found2 = politician_on_stage_id > 0 # TODO Why not this simpler case? politician_on_stage_found = True if politician_on_stage_id > 0 else False results = { 'success': True if politician_on_stage_found else False, 'politician_found': politician_on_stage_found, 'politician_id': politician_on_stage_id, 'politician': politician_on_stage, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results
def measure_summary_view(request, measure_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) measure_id = convert_to_int(measure_id) measure_on_stage_found = False measure_on_stage = ContestMeasure() google_civic_election_id = convert_to_int( request.GET.get('google_civic_election_id', 0)) try: measure_on_stage = ContestMeasure.objects.get(id=measure_id) measure_on_stage_found = True except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except ContestMeasure.DoesNotExist: # This is fine, create new pass election_list = Election.objects.order_by('-election_day_text') if measure_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'measure': measure_on_stage, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'measure/measure_summary.html', template_values)
def voter_summary_view(request, voter_id): authority_required = {'admin'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) voter_id = convert_to_int(voter_id) voter_on_stage_found = False voter_on_stage = Voter() try: voter_on_stage = Voter.objects.get(id=voter_id) voter_on_stage_found = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Voter.DoesNotExist: # This is fine, create new pass if voter_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'voter': voter_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'voter/voter_summary.html', template_values)
def voter_authenticate_manually_view(request): messages_on_stage = get_messages(request) voter_api_device_id = get_voter_api_device_id( request) # We look in the cookies for voter_api_device_id store_new_voter_api_device_id_in_cookie = False if not positive_value_exists(voter_api_device_id): # Create a voter_device_id and voter in the database if one doesn't exist yet results = voter_setup(request) voter_api_device_id = results['voter_api_device_id'] store_new_voter_api_device_id_in_cookie = results[ 'store_new_voter_api_device_id_in_cookie'] voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) voter_id = convert_to_int(voter_id) voter_on_stage_found = False voter_on_stage = Voter() try: voter_on_stage = Voter.objects.get(id=voter_id) voter_on_stage_found = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except Voter.DoesNotExist: # This is fine, we will display an error pass if voter_on_stage_found: set_this_voter_as_admin = "UPDATE voter_voter SET is_admin=True WHERE id={voter_id};".format( voter_id=voter_id) unset_this_voter_as_admin = "UPDATE voter_voter SET is_admin=False WHERE id={voter_id};".format( voter_id=voter_id) set_as_verified_volunteer = "UPDATE voter_voter SET is_verified_volunteer=True WHERE id={voter_id};" \ "".format(voter_id=voter_id) unset_as_verified_volunteer = "UPDATE voter_voter SET is_verified_volunteer=False WHERE id={voter_id};" \ "".format(voter_id=voter_id) template_values = { 'messages_on_stage': messages_on_stage, 'voter': voter_on_stage, 'voter_api_device_id': voter_api_device_id, 'is_authenticated': request.user.is_authenticated(), 'set_this_voter_as_admin': set_this_voter_as_admin, 'unset_this_voter_as_admin': unset_this_voter_as_admin, 'set_as_verified_volunteer': set_as_verified_volunteer, 'unset_as_verified_volunteer': unset_as_verified_volunteer, } else: template_values = { 'messages_on_stage': messages_on_stage, } response = render(request, 'voter/voter_authenticate_manually.html', template_values) # We want to store the voter_api_device_id cookie if it is new # if positive_value_exists(voter_api_device_id) and positive_value_exists(store_new_voter_api_device_id_in_cookie): # DALE 2016-02-15 Always set if we have a voter_api_device_id if positive_value_exists(store_new_voter_api_device_id_in_cookie): set_voter_api_device_id(request, response, voter_api_device_id) return response
def measure_edit_view(request, measure_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) measure_id = convert_to_int(measure_id) measure_on_stage_found = False try: measure_on_stage = ContestMeasure.objects.get(id=measure_id) measure_on_stage_found = True except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) measure_on_stage = ContestMeasure() except ContestMeasure.DoesNotExist: # This is fine, create new measure_on_stage = ContestMeasure() pass if measure_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'measure': measure_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'measure/measure_edit.html', template_values)
def voter_authenticate_manually_process_view(request): voter_api_device_id = get_voter_api_device_id( request) # We look in the cookies for voter_api_device_id voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) voter_id = convert_to_int(voter_id) voter_signed_in = False try: voter_on_stage = Voter.objects.get(id=voter_id) # If the account associated with this voter_api_device_id is an admin, complete Django authentication if voter_on_stage.is_admin: voter_on_stage.backend = 'django.contrib.auth.backends.ModelBackend' login(request, voter_on_stage) messages.add_message(request, messages.INFO, 'Voter logged in.') voter_signed_in = True else: messages.add_message(request, messages.INFO, 'This account does not have Admin access.') except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) messages.add_message( request, messages.ERROR, 'More than one voter found. Voter not logged in.') except Voter.DoesNotExist: # This is fine, we will display an error messages.add_message(request, messages.ERROR, 'Voter not found. Voter not logged in.') if voter_signed_in: return HttpResponseRedirect(reverse('admin_tools:admin_home', args=())) else: return HttpResponseRedirect( reverse('voter:authenticate_manually', args=()))
def voter_authenticate_manually_process_view(request): voter_api_device_id = get_voter_api_device_id(request) # We look in the cookies for voter_api_device_id voter_id = fetch_voter_id_from_voter_device_link(voter_api_device_id) voter_id = convert_to_int(voter_id) voter_signed_in = False try: voter_on_stage = Voter.objects.get(id=voter_id) # If the account associated with this voter_api_device_id is an admin, complete Django authentication if voter_on_stage.is_admin: voter_on_stage.backend = 'django.contrib.auth.backends.ModelBackend' login(request, voter_on_stage) messages.add_message(request, messages.INFO, 'Voter logged in.') voter_signed_in = True else: messages.add_message(request, messages.INFO, 'This account does not have Admin access.') except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) messages.add_message(request, messages.ERROR, 'More than one voter found. Voter not logged in.') except Voter.DoesNotExist: # This is fine, we will display an error messages.add_message(request, messages.ERROR, 'Voter not found. Voter not logged in.') if voter_signed_in: return HttpResponseRedirect(reverse('admin_tools:admin_home', args=())) else: return HttpResponseRedirect(reverse('voter:authenticate_manually', args=()))
def update_or_create_polling_location(self, polling_location_id, location_name, polling_hours_text, directions_text, line1, line2, city, state, zip_long): """ Either update or create an polling_location entry. """ exception_multiple_object_returned = False new_polling_location_created = False new_polling_location = PollingLocation() if not polling_location_id: success = False status = 'MISSING_POLLING_LOCATION_ID' elif not line1: success = False status = 'MISSING_POLLING_LOCATION_LINE1' elif not city: success = False status = 'MISSING_POLLING_LOCATION_CITY' elif not state: success = False status = 'MISSING_POLLING_LOCATION_STATE' elif not zip_long: success = False status = 'MISSING_POLLING_LOCATION_ZIP' else: try: updated_values = { # Values we search against 'polling_location_id': polling_location_id, 'state': state, # The rest of the values 'location_name': location_name.strip(), 'polling_hours_text': polling_hours_text.strip(), 'directions_text': directions_text.strip(), 'line1': line1.strip(), 'line2': line2, 'city': city.strip(), 'zip_long': zip_long, } # We use polling_location_id + state to find prior entries since I am not sure polling_location_id's # are unique from state-to-state new_polling_location, new_polling_location_created = PollingLocation.objects.update_or_create( polling_location_id__exact=polling_location_id, state=state, defaults=updated_values) success = True status = 'POLLING_LOCATION_SAVED' except PollingLocation.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) success = False status = 'MULTIPLE_MATCHING_ADDRESSES_FOUND' exception_multiple_object_returned = True results = { 'success': success, 'status': status, 'MultipleObjectsReturned': exception_multiple_object_returned, 'new_polling_location': new_polling_location, 'new_polling_location_created': new_polling_location_created, } return results
def office_edit_view(request, office_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) office_id = convert_to_int(office_id) google_civic_election_id = request.GET.get('google_civic_election_id', 0) office_on_stage_found = False try: office_on_stage = ContestOffice.objects.get(id=office_id) office_on_stage_found = True except ContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except ContestOffice.DoesNotExist: # This is fine, create new pass if office_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'office': office_on_stage, 'google_civic_election_id': google_civic_election_id, } else: template_values = { 'messages_on_stage': messages_on_stage, 'google_civic_election_id': google_civic_election_id, } return render(request, 'office/office_edit.html', template_values)
def organization_add_new_position_form_view(request, organization_id): messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) all_is_well = True organization_on_stage_found = False 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) except Organization.DoesNotExist as e: # This is fine, create new handle_exception_silently(e) 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 = CandidateCampaignList() candidate_campaigns_for_this_election_list \ = candidate_campaign_list.retrieve_candidate_campaigns_for_this_election_list() if all_is_well: template_values = { 'candidate_campaigns_for_this_election_list': candidate_campaigns_for_this_election_list, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position_candidate_campaign_id': 0, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': SUPPORT, # Default stance } return render(request, 'organization/organization_position_edit.html', template_values)
def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() if ( value_exists(voter_id) and value_exists(google_civic_election_id) and value_exists(google_civic_district_ocd_id) ): try: google_civic_ballot_item_on_stage = GoogleCivicBallotItem.objects.get( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except GoogleCivicBallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True except GoogleCivicBallotItem.DoesNotExist as e: exception_does_not_exist = True results = { "success": True if google_civic_ballot_item_id > 0 else False, "DoesNotExist": exception_does_not_exist, "MultipleObjectsReturned": exception_multiple_object_returned, "google_civic_ballot_item": google_civic_ballot_item_on_stage, } return results
def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = GoogleCivicBallotItem() if value_exists(voter_id) and value_exists( google_civic_election_id) and value_exists( google_civic_district_ocd_id): try: google_civic_ballot_item_on_stage = GoogleCivicBallotItem.objects.get( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except GoogleCivicBallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except GoogleCivicBallotItem.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'success': True if google_civic_ballot_item_id > 0 else False, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'google_civic_ballot_item': google_civic_ballot_item_on_stage, } return results
def retrieve_address(self, voter_address_id, voter_id, address_type): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_address_on_stage = VoterAddress() try: if voter_address_id > 0: voter_address_on_stage = VoterAddress.objects.get( id=voter_address_id) voter_address_id = voter_address_on_stage.id elif voter_id > 0 and address_type in (BALLOT_ADDRESS, MAILING_ADDRESS, FORMER_BALLOT_ADDRESS): voter_address_on_stage = VoterAddress.objects.get( voter_id=voter_id, address_type=address_type) # If still here, we found an existing address voter_address_id = voter_address_on_stage.id except VoterAddress.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) error_result = True exception_multiple_object_returned = True except VoterAddress.DoesNotExist: error_result = True exception_does_not_exist = True results = { 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'voter_address_found': True if voter_address_id > 0 else False, 'voter_address_id': voter_address_id, 'voter_address': voter_address_on_stage, } return results
def retrieve_politician(self, politician_id): # , id_we_vote=None error_result = False exception_does_not_exist = False exception_multiple_object_returned = False politician_on_stage = Politician() politician_on_stage_id = 0 try: if politician_id > 0: politician_on_stage = Politician.objects.get(id=politician_id) politician_on_stage_id = politician_on_stage.id # elif len(id_we_vote) > 0: # politician_on_stage = Politician.objects.get(id_we_vote=id_we_vote) # politician_on_stage_id = politician_on_stage.id except Politician.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except Politician.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True politician_on_stage_found = True if politician_on_stage_id > 0 else False results = { 'success': True if politician_on_stage_found else False, 'politician_found': politician_on_stage_found, 'politician_id': politician_on_stage_id, 'politician': politician_on_stage, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results
def retrieve_address(self, voter_address_id, voter_id, address_type): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_address_on_stage = VoterAddress() try: if voter_address_id > 0: voter_address_on_stage = VoterAddress.objects.get(id=voter_address_id) voter_address_id = voter_address_on_stage.id elif voter_id > 0 and address_type in (BALLOT_ADDRESS, MAILING_ADDRESS, FORMER_BALLOT_ADDRESS): voter_address_on_stage = VoterAddress.objects.get(voter_id=voter_id, address_type=address_type) # If still here, we found an existing address voter_address_id = voter_address_on_stage.id except VoterAddress.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) error_result = True exception_multiple_object_returned = True except VoterAddress.DoesNotExist: error_result = True exception_does_not_exist = True results = { 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'voter_address_found': True if voter_address_id > 0 else False, 'voter_address_id': voter_address_id, 'voter_address': voter_address_on_stage, } return results
def polling_location_summary_by_we_vote_id_view(request, polling_location_we_vote_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) polling_location_on_stage_found = False polling_location_on_stage = PollingLocation() try: polling_location_on_stage = PollingLocation.objects.get( we_vote_id=polling_location_we_vote_id) polling_location_on_stage_found = True except PollingLocation.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except PollingLocation.DoesNotExist: # This is fine, create new pass if polling_location_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'polling_location': polling_location_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'polling_location/polling_location_summary.html', template_values)
def organization_edit_view(request, organization_id): # If person isn't signed in, we don't want to let them visit this page yet if not request.user.is_authenticated(): return redirect('/admin') messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) organization_on_stage_found = False 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) except Organization.DoesNotExist as e: # This is fine, create new handle_exception_silently(e) if organization_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'organization/organization_edit.html', template_values)
def retrieve_contest_office(self, contest_office_id, id_maplight=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False contest_office_on_stage = ContestOffice() try: if contest_office_id > 0: contest_office_on_stage = ContestOffice.objects.get( id=contest_office_id) contest_office_id = contest_office_on_stage.id elif len(id_maplight) > 0: contest_office_on_stage = ContestOffice.objects.get( id_maplight=id_maplight) contest_office_id = contest_office_on_stage.id except ContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except ContestOffice.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'success': True if contest_office_id > 0 else False, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'contest_office_found': True if contest_office_id > 0 else False, 'contest_office_id': contest_office_id, 'contest_office': contest_office_on_stage, } return results
def retrieve_candidate_campaign(self, candidate_campaign_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False candidate_campaign_on_stage = CandidateCampaign() try: if candidate_campaign_id > 0: candidate_campaign_on_stage = CandidateCampaign.objects.get( id=candidate_campaign_id) candidate_campaign_id = candidate_campaign_on_stage.id except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except CandidateCampaign.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'candidate_campaign_found': True if candidate_campaign_id > 0 else False, 'candidate_campaign_id': candidate_campaign_id, 'candidate_campaign': candidate_campaign_on_stage, } return results
def organization_edit_existing_position_form_view(request, organization_id, position_id): """ In edit, you can only change your stance and comments, not who or what the position is about :param request: :param organization_id: :param position_id: :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) messages_on_stage = get_messages(request) organization_id = convert_to_int(organization_id) position_id = convert_to_int(position_id) organization_on_stage_found = False 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 edit a position.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Get the existing position organization_position_on_stage = PositionEntered() organization_position_on_stage_found = False position_entered_manager = PositionEnteredManager() results = position_entered_manager.retrieve_position_from_id(position_id) if results['position_found']: organization_position_on_stage_found = True organization_position_on_stage = results['position'] if not organization_position_on_stage_found: messages.add_message(request, messages.INFO, 'Could not find organization position when trying to edit.') return HttpResponseRedirect(reverse('organization:organization_position_list', args=([organization_id]))) # Note: We have access to the candidate campaign through organization_position_on_stage.candidate_campaign election_list = Election.objects.all() if organization_position_on_stage_found: template_values = { 'is_in_edit_mode': True, 'messages_on_stage': messages_on_stage, 'organization': organization_on_stage, 'organization_position': organization_position_on_stage, 'possible_stances_list': ORGANIZATION_STANCE_CHOICES, 'stance_selected': organization_position_on_stage.stance, 'election_list': election_list, } return render(request, 'organization/organization_position_edit.html', template_values)
def retrieve_maplight_contest_office(self, contest_office_id, id_maplight=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False maplight_contest_office_on_stage = MapLightContestOffice() try: if contest_office_id > 0: maplight_contest_office_on_stage = MapLightContestOffice.objects.get(id=contest_office_id) contest_office_id = maplight_contest_office_on_stage.id elif len(id_maplight) > 0: maplight_contest_office_on_stage = MapLightContestOffice.objects.get(contest_id=id_maplight) contest_office_id = maplight_contest_office_on_stage.id except MapLightContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) exception_multiple_object_returned = True except MapLightContestOffice.DoesNotExist as e: handle_exception_silently(e) exception_does_not_exist = True results = { 'success': True if contest_office_id > 0 else False, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'maplight_contest_office_found': True if contest_office_id > 0 else False, 'contest_office_id': contest_office_id, 'maplight_contest_office': maplight_contest_office_on_stage, } return results
def retrieve_contest_measure(self, contest_measure_id, contest_measure_we_vote_id='', maplight_id=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False contest_measure_on_stage = ContestMeasure() try: if positive_value_exists(contest_measure_id): contest_measure_on_stage = ContestMeasure.objects.get(id=contest_measure_id) contest_measure_id = contest_measure_on_stage.id elif positive_value_exists(contest_measure_we_vote_id): contest_measure_on_stage = ContestMeasure.objects.get(we_vote_id=contest_measure_we_vote_id) contest_measure_id = contest_measure_on_stage.id elif positive_value_exists(maplight_id): contest_measure_on_stage = ContestMeasure.objects.get(maplight_id=maplight_id) contest_measure_id = contest_measure_on_stage.id except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True except ContestMeasure.DoesNotExist: exception_does_not_exist = True results = { 'success': True if contest_measure_id > 0 else False, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'contest_measure_found': True if contest_measure_id > 0 else False, 'contest_measure_id': contest_measure_id, 'contest_measure_we_vote_id': contest_measure_we_vote_id, 'contest_measure': contest_measure_on_stage, } return results
def measure_summary_view(request, measure_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) measure_id = convert_to_int(measure_id) measure_on_stage_found = False measure_on_stage = ContestMeasure() google_civic_election_id = convert_to_int(request.GET.get('google_civic_election_id', 0)) try: measure_on_stage = ContestMeasure.objects.get(id=measure_id) measure_on_stage_found = True except ContestMeasure.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except ContestMeasure.DoesNotExist: # This is fine, create new pass election_list = Election.objects.order_by('-election_day_text') if measure_on_stage_found: template_values = { 'messages_on_stage': messages_on_stage, 'measure': measure_on_stage, 'election_list': election_list, 'google_civic_election_id': google_civic_election_id, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'measure/measure_summary.html', template_values)
def retrieve_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id): exception_does_not_exist = False exception_multiple_object_returned = False google_civic_ballot_item_on_stage = BallotItem() google_civic_ballot_item_id = 0 if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id) and \ positive_value_exists(google_civic_district_ocd_id): try: google_civic_ballot_item_on_stage = BallotItem.objects.get( voter_id__exact=voter_id, google_civic_election_id__exact=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, # TODO This needs to be rethunk ) google_civic_ballot_item_id = google_civic_ballot_item_on_stage.id except BallotItem.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True except BallotItem.DoesNotExist: exception_does_not_exist = True results = { 'success': True if google_civic_ballot_item_id > 0 else False, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'google_civic_ballot_item': google_civic_ballot_item_on_stage, } return results
def retrieve_contest_office(self, contest_office_id, contest_office_we_vote_id='', maplight_id=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False contest_office_on_stage = ContestOffice() try: if positive_value_exists(contest_office_id): contest_office_on_stage = ContestOffice.objects.get( id=contest_office_id) contest_office_id = contest_office_on_stage.id contest_office_we_vote_id = contest_office_on_stage.we_vote_id status = "RETRIEVE_OFFICE_FOUND_BY_ID" elif positive_value_exists(contest_office_we_vote_id): contest_office_on_stage = ContestOffice.objects.get( we_vote_id=contest_office_we_vote_id) contest_office_id = contest_office_on_stage.id contest_office_we_vote_id = contest_office_on_stage.we_vote_id status = "RETRIEVE_OFFICE_FOUND_BY_WE_VOTE_ID" elif positive_value_exists(maplight_id): contest_office_on_stage = ContestOffice.objects.get( maplight_id=maplight_id) contest_office_id = contest_office_on_stage.id contest_office_we_vote_id = contest_office_on_stage.we_vote_id status = "RETRIEVE_OFFICE_FOUND_BY_MAPLIGHT_ID" else: status = "RETRIEVE_OFFICE_SEARCH_INDEX_MISSING" except ContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True status = "RETRIEVE_OFFICE_MULTIPLE_OBJECTS_RETURNED" except ContestOffice.DoesNotExist: exception_does_not_exist = True status = "RETRIEVE_OFFICE_NOT_FOUND" results = { 'success': True if convert_to_int(contest_office_id) > 0 else False, 'status': status, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'contest_office_found': True if convert_to_int(contest_office_id) > 0 else False, 'contest_office_id': convert_to_int(contest_office_id), 'contest_office_we_vote_id': contest_office_we_vote_id, 'contest_office': contest_office_on_stage, } return results
def save_setting(self, setting_name, setting_value, value_type=None): accepted_value_types = ["bool", "int", "str"] if value_type is None: if type(setting_value).__name__ == "bool": value_type = WeVoteSetting.BOOLEAN elif type(setting_value).__name__ == "int": value_type = WeVoteSetting.INTEGER elif type(setting_value).__name__ == "str": value_type = WeVoteSetting.STRING elif type(setting_value).__name__ == "list": print "setting is a list. To be developed" value_type = WeVoteSetting.STRING else: value_type = WeVoteSetting.STRING elif value_type not in accepted_value_types: # Not a recognized value_type, save as a str value_type = WeVoteSetting.STRING # Does this setting already exist? we_vote_setting = WeVoteSetting() we_vote_setting_id = 0 we_vote_setting_exists = False we_vote_setting_does_not_exist = False try: we_vote_setting = WeVoteSetting.objects.get(name=setting_name) we_vote_setting_exists = True except WeVoteSetting.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) except WeVoteSetting.DoesNotExist as e: handle_exception_silently(e) we_vote_setting_does_not_exist = True we_vote_setting_manager = WeVoteSettingsManager() if we_vote_setting_exists: # Update this position with new values try: we_vote_setting.value_type = value_type we_vote_setting = we_vote_setting_manager.set_setting_value_by_type( we_vote_setting, setting_value, value_type ) we_vote_setting.save() we_vote_setting_id = we_vote_setting.id except Exception as e: handle_record_not_saved_exception(e) elif we_vote_setting_does_not_exist: try: # Create new we_vote_setting = WeVoteSetting(value_type=value_type, name=setting_name) we_vote_setting = we_vote_setting_manager.set_setting_value_by_type( we_vote_setting, setting_value, value_type ) we_vote_setting.save() we_vote_setting_id = we_vote_setting.id except Exception as e: handle_record_not_saved_exception(e) results = {"success": True if we_vote_setting_id else False, "we_vote_setting": we_vote_setting} return results
def update_or_create_voter_address(self, voter_id, address_type, raw_address_text): """ NOTE: This approach won't support multiple FORMER_BALLOT_ADDRESS :param voter_id: :param address_type: :param raw_address_text: :return: """ status = '' exception_multiple_object_returned = False new_address_created = False voter_address_on_stage = None voter_address_on_stage_found = False if positive_value_exists(voter_id) and address_type in (BALLOT_ADDRESS, MAILING_ADDRESS, FORMER_BALLOT_ADDRESS): try: updated_values = { # Values we search against 'voter_id': voter_id, 'address_type': address_type, # The rest of the values are to be saved 'text_for_map_search': raw_address_text, 'latitude': None, 'longitude': None, 'normalized_line1': None, 'normalized_line2': None, 'normalized_city': None, 'normalized_state': None, 'normalized_zip': None, # We clear out former values for these so voter_ballot_items_retrieve_for_api resets them 'refreshed_from_google': False, 'google_civic_election_id': 0, 'election_day_text': '', } voter_address_on_stage, new_address_created = VoterAddress.objects.update_or_create( voter_id__exact=voter_id, address_type=address_type, defaults=updated_values) voter_address_on_stage_found = voter_address_on_stage.id success = True except VoterAddress.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) success = False status = 'MULTIPLE_MATCHING_ADDRESSES_FOUND' exception_multiple_object_returned = True else: success = False status = 'MISSING_VOTER_ID_OR_ADDRESS_TYPE' results = { 'success': success, 'status': status, 'MultipleObjectsReturned': exception_multiple_object_returned, 'voter_address_saved': success, 'address_type': address_type, 'new_address_created': new_address_created, 'voter_address_found': voter_address_on_stage_found, 'voter_address': voter_address_on_stage, } return results
def update_or_create_contest_office(self, we_vote_id, maplight_id, google_civic_election_id, office_name, updated_contest_office_values): """ Either update or create an office entry. """ exception_multiple_object_returned = False new_office_created = False contest_office_on_stage = ContestOffice() if not google_civic_election_id: success = False status = 'MISSING_GOOGLE_CIVIC_ELECTION_ID' # DALE 2016-05-10 Since we are allowing offices to be created prior to Google Civic data # being available, we need to remove our reliance on district_id or district_name # elif not (district_id or district_name): # success = False # status = 'MISSING_DISTRICT_ID' elif not office_name: success = False status = 'MISSING_OFFICE' else: # state_code not required due to some federal offices try: if positive_value_exists(we_vote_id): contest_office_on_stage, new_office_created = ContestOffice.objects.update_or_create( google_civic_election_id__exact=google_civic_election_id, we_vote_id__iexact=we_vote_id, defaults=updated_contest_office_values) elif positive_value_exists(maplight_id): contest_office_on_stage, new_office_created = ContestOffice.objects.update_or_create( google_civic_election_id__exact=google_civic_election_id, maplight_id__exact=maplight_id, defaults=updated_contest_office_values) else: contest_office_on_stage, new_office_created = ContestOffice.objects.update_or_create( google_civic_election_id__exact=google_civic_election_id, # district_id__exact=district_id, # district_name__iexact=district_name, # Case doesn't matter office_name__iexact=office_name, # Case doesn't matter # state_code__iexact=state_code, # Case doesn't matter defaults=updated_contest_office_values) success = True status = 'CONTEST_OFFICE_SAVED' except ContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) success = False status = 'MULTIPLE_MATCHING_CONTEST_OFFICES_FOUND' exception_multiple_object_returned = True results = { 'success': success, 'status': status, 'MultipleObjectsReturned': exception_multiple_object_returned, 'new_office_created': new_office_created, 'contest_office': contest_office_on_stage, 'saved': new_office_created, 'updated': True if success and not new_office_created else False, 'not_processed': True if not success else False, } return results
def candidate_edit_view(request, candidate_id): authority_required = {'verified_volunteer'} # admin, verified_volunteer if not voter_has_authority(request, authority_required): return redirect_to_sign_in_page(request, authority_required) messages_on_stage = get_messages(request) candidate_id = convert_to_int(candidate_id) candidate_on_stage_found = False candidate_on_stage = CandidateCampaign() try: candidate_on_stage = CandidateCampaign.objects.get(id=candidate_id) candidate_on_stage_found = True except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) except CandidateCampaign.DoesNotExist: # This is fine, create new pass if candidate_on_stage_found: # Working with Vote Smart data try: vote_smart_candidate_id = candidate_on_stage.vote_smart_id rating_list_query = VoteSmartRatingOneCandidate.objects.order_by( '-timeSpan') # Desc order rating_list = rating_list_query.filter( candidateId=vote_smart_candidate_id) except VotesmartApiError as error_instance: # Catch the error message coming back from Vote Smart and pass it in the status error_message = error_instance.args status = "EXCEPTION_RAISED: {error_message}".format( error_message=error_message) print_to_log(logger=logger, exception_message_optional=status) rating_list = [] # Working with We Vote Positions try: candidate_position_list = PositionEntered.objects.order_by( 'stance') candidate_position_list = candidate_position_list.filter( candidate_campaign_id=candidate_id) # if positive_value_exists(google_civic_election_id): # organization_position_list = candidate_position_list.filter( # google_civic_election_id=google_civic_election_id) except Exception as e: handle_record_not_found_exception(e, logger=logger) candidate_position_list = [] template_values = { 'messages_on_stage': messages_on_stage, 'candidate': candidate_on_stage, 'rating_list': rating_list, 'candidate_position_list': candidate_position_list, } else: template_values = { 'messages_on_stage': messages_on_stage, } return render(request, 'candidate/candidate_edit.html', template_values)
def update_or_create_contest_office(self, we_vote_id, google_civic_election_id, district_id, district_name, office_name, state_code, updated_contest_office_values): """ Either update or create an office entry. """ exception_multiple_object_returned = False new_office_created = False contest_office_on_stage = ContestOffice() if not google_civic_election_id: success = False status = 'MISSING_GOOGLE_CIVIC_ELECTION_ID' elif not (district_id or district_name): success = False status = 'MISSING_DISTRICT_ID' elif not office_name: success = False status = 'MISSING_OFFICE' else: # state_code not required due to some federal offices try: if positive_value_exists(we_vote_id): contest_office_on_stage, new_office_created = ContestOffice.objects.update_or_create( google_civic_election_id__exact= google_civic_election_id, we_vote_id__exact=we_vote_id, defaults=updated_contest_office_values) else: contest_office_on_stage, new_office_created = ContestOffice.objects.update_or_create( google_civic_election_id__exact= google_civic_election_id, district_id__exact=district_id, district_name__iexact= district_name, # Case doesn't matter office_name__iexact=office_name, # Case doesn't matter state_code__iexact=state_code, # Case doesn't matter defaults=updated_contest_office_values) success = True status = 'CONTEST_OFFICE_SAVED' except ContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) success = False status = 'MULTIPLE_MATCHING_CONTEST_OFFICES_FOUND' exception_multiple_object_returned = True results = { 'success': success, 'status': status, 'MultipleObjectsReturned': exception_multiple_object_returned, 'new_office_created': new_office_created, 'contest_office': contest_office_on_stage, 'saved': new_office_created, 'updated': True if success and not new_office_created else False, 'not_processed': True if not success else False, } return results
def retrieve_candidate_campaign( self, candidate_campaign_id, candidate_campaign_we_vote_id=None, candidate_maplight_id=None, candidate_name=None, candidate_vote_smart_id=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False candidate_campaign_on_stage = CandidateCampaign() try: if positive_value_exists(candidate_campaign_id): candidate_campaign_on_stage = CandidateCampaign.objects.get(id=candidate_campaign_id) candidate_campaign_id = candidate_campaign_on_stage.id candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id status = "RETRIEVE_CANDIDATE_FOUND_BY_ID" elif positive_value_exists(candidate_campaign_we_vote_id): candidate_campaign_on_stage = CandidateCampaign.objects.get(we_vote_id=candidate_campaign_we_vote_id) candidate_campaign_id = candidate_campaign_on_stage.id candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id status = "RETRIEVE_CANDIDATE_FOUND_BY_WE_VOTE_ID" elif positive_value_exists(candidate_maplight_id): candidate_campaign_on_stage = CandidateCampaign.objects.get(maplight_id=candidate_maplight_id) candidate_campaign_id = candidate_campaign_on_stage.id candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id status = "RETRIEVE_CANDIDATE_FOUND_BY_MAPLIGHT_ID" elif positive_value_exists(candidate_vote_smart_id): candidate_campaign_on_stage = CandidateCampaign.objects.get(vote_smart_id=candidate_vote_smart_id) candidate_campaign_id = candidate_campaign_on_stage.id candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id status = "RETRIEVE_CANDIDATE_FOUND_BY_VOTE_SMART_ID" elif positive_value_exists(candidate_name): candidate_campaign_on_stage = CandidateCampaign.objects.get(candidate_name=candidate_name) candidate_campaign_id = candidate_campaign_on_stage.id candidate_campaign_we_vote_id = candidate_campaign_on_stage.we_vote_id status = "RETRIEVE_CANDIDATE_FOUND_BY_NAME" else: status = "RETRIEVE_CANDIDATE_SEARCH_INDEX_MISSING" except CandidateCampaign.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) exception_multiple_object_returned = True status = "RETRIEVE_CANDIDATE_MULTIPLE_OBJECTS_RETURNED" except CandidateCampaign.DoesNotExist: exception_does_not_exist = True status = "RETRIEVE_CANDIDATE_NOT_FOUND" results = { 'success': True if convert_to_int(candidate_campaign_id) > 0 else False, 'status': status, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'candidate_campaign_found': True if convert_to_int(candidate_campaign_id) else False, 'candidate_campaign_id': convert_to_int(candidate_campaign_id), 'candidate_campaign_we_vote_id': candidate_campaign_we_vote_id, 'candidate_campaign': candidate_campaign_on_stage, } return results
def office(self): try: office = ContestOffice.objects.get(id=self.contest_office_id) except ContestOffice.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) logger.error("candidate.election Found multiple") return except ContestOffice.DoesNotExist: logger.error("candidate.election did not find") return return office
def organization(self): try: organization = Organization.objects.get(id=self.organization_id) except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) logger.error("position.candidate_campaign Found multiple") return except Organization.DoesNotExist: logger.error("position.candidate_campaign did not find") return return organization
def retrieve_position(self, position_id, organization_id, voter_id, candidate_campaign_id, measure_campaign_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False position_on_stage = PositionEntered() try: if position_id > 0: position_on_stage = PositionEntered.objects.get(id=position_id) position_id = position_on_stage.id elif organization_id > 0 and candidate_campaign_id > 0: position_on_stage = PositionEntered.objects.get( organization_id=organization_id, candidate_campaign_id=candidate_campaign_id) # If still here, we found an existing position position_id = position_on_stage.id elif organization_id > 0 and measure_campaign_id > 0: position_on_stage = PositionEntered.objects.get( organization_id=organization_id, measure_campaign_id=measure_campaign_id) position_id = position_on_stage.id elif voter_id > 0 and candidate_campaign_id > 0: position_on_stage = PositionEntered.objects.get( voter_id=voter_id, candidate_campaign_id=candidate_campaign_id) position_id = position_on_stage.id elif voter_id > 0 and measure_campaign_id > 0: position_on_stage = PositionEntered.objects.get( voter_id=voter_id, measure_campaign_id=measure_campaign_id) position_id = position_on_stage.id except PositionEntered.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except PositionEntered.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True results = { 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, 'position_found': True if position_id > 0 else False, 'position_id': position_id, 'position': position_on_stage, 'is_support': position_on_stage.is_support(), 'is_oppose': position_on_stage.is_oppose(), 'is_no_stance': position_on_stage.is_no_stance(), 'is_information_only': position_on_stage.is_information_only(), 'is_still_deciding': position_on_stage.is_still_deciding(), } return results
def election(self): try: election = Election.objects.get(google_civic_election_id=self.google_civic_election_id) except Election.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger=logger) logger.error("candidate.election Found multiple") return except Election.DoesNotExist: logger.error("candidate.election did not find") return return election
def organization(self): try: organization = Organization.objects.get(id=self.organization_id) except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) print "position.candidate_campaign Found multiple" return None except Organization.DoesNotExist as e: handle_exception_silently(e) print "position.candidate_campaign did not find" return None return organization
def retrieve_organization(self, organization_id, we_vote_id=None, vote_smart_id=None): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False organization_on_stage = Organization() organization_on_stage_id = 0 status = "ERROR_ENTERING_RETRIEVE_ORGANIZATION" try: if positive_value_exists(organization_id): status = "ERROR_RETRIEVING_ORGANIZATION_WITH_ID" organization_on_stage = Organization.objects.get(id=organization_id) organization_on_stage_id = organization_on_stage.id status = "ORGANIZATION_FOUND_WITH_ID" elif positive_value_exists(we_vote_id): status = "ERROR_RETRIEVING_ORGANIZATION_WITH_WE_VOTE_ID" organization_on_stage = Organization.objects.get(we_vote_id=we_vote_id) organization_on_stage_id = organization_on_stage.id status = "ORGANIZATION_FOUND_WITH_WE_VOTE_ID" elif positive_value_exists(vote_smart_id): status = "ERROR_RETRIEVING_ORGANIZATION_WITH_VOTE_SMART_ID" organization_on_stage = Organization.objects.get(vote_smart_id=vote_smart_id) organization_on_stage_id = organization_on_stage.id status = "ORGANIZATION_FOUND_WITH_VOTE_SMART_ID" except Organization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e, logger) error_result = True exception_multiple_object_returned = True status = "ERROR_MORE_THAN_ONE_ORGANIZATION_FOUND" # logger.warn("Organization.MultipleObjectsReturned") except Organization.DoesNotExist: error_result = True exception_does_not_exist = True status += ", ORGANIZATION_NOT_FOUND" # logger.warn("Organization.DoesNotExist") organization_on_stage_found = True if organization_on_stage_id > 0 else False results = { 'success': True if organization_on_stage_found else False, 'status': status, 'organization_found': organization_on_stage_found, 'organization_id': organization_on_stage.id if organization_on_stage.id else organization_on_stage_id, 'we_vote_id': organization_on_stage.we_vote_id if organization_on_stage.we_vote_id else we_vote_id, 'organization': organization_on_stage, 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results