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 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_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_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_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_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_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_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_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) error_result = True exception_multiple_object_returned = True print "Organization.MultipleObjectsReturned" except Organization.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True print "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 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 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 measure_campaign_id_we_vote(self): try: measure_campaign_on_stage = Organization.objects.get(id=self.measure_campaign_id) if measure_campaign_on_stage.id_we_vote: return measure_campaign_on_stage.id_we_vote except Exception as e: handle_exception_silently(e) return ''
def candidate_campaign_id_we_vote(self): try: candidate_campaign_on_stage = CandidateCampaign.objects.get(id=self.candidate_campaign_id) if candidate_campaign_on_stage.id_we_vote: return candidate_campaign_on_stage.id_we_vote except Exception as e: handle_exception_silently(e) return ''
def organization_id_we_vote(self): try: organization_on_stage = Organization.objects.get(id=self.organization_id) if organization_on_stage.id_we_vote: return organization_on_stage.id_we_vote except Exception as e: handle_exception_silently(e) return ''
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: """ # 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) 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) 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 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 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, } return render(request, 'organization/organization_position_edit.html', template_values)
def organization_id_we_vote(self): try: organization_on_stage = Organization.objects.get( id=self.organization_id) if organization_on_stage.id_we_vote: return organization_on_stage.id_we_vote except Exception as e: handle_exception_silently(e) return ''
def candidate_campaign_id_we_vote(self): try: candidate_campaign_on_stage = CandidateCampaign.objects.get( id=self.candidate_campaign_id) if candidate_campaign_on_stage.id_we_vote: return candidate_campaign_on_stage.id_we_vote except Exception as e: handle_exception_silently(e) return ''
def measure_campaign_id_we_vote(self): try: measure_campaign_on_stage = Organization.objects.get( id=self.measure_campaign_id) if measure_campaign_on_stage.id_we_vote: return measure_campaign_on_stage.id_we_vote except Exception as e: handle_exception_silently(e) return ''
def import_maplight_from_json(request): load_from_url = False ballot_for_one_voter_array = [] if load_from_url: # Request json file from Maplight servers print "TO BE IMPLEMENTED: Load Maplight JSON from url" # request = requests.get(VOTER_INFO_URL, params={ # "key": GOOGLE_CIVIC_API_KEY, # This comes from an environment variable # "address": "254 Hartford Street San Francisco CA", # "electionId": "2000", # }) # structured_json = json.loads(request.text) else: # Load saved json from local file print "Loading Maplight sample JSON from local file" with open( MAPLIGHT_SAMPLE_BALLOT_JSON_FILE) as ballot_for_one_voter_json: ballot_for_one_voter_array = json.load(ballot_for_one_voter_json) # A MapLight ballot query is essentially an array of contests with the key as the contest_id if ballot_for_one_voter_array and len(ballot_for_one_voter_array): # Parse the JSON here. This JSON is a list of contests on the ballot for one voter. for contest_id in ballot_for_one_voter_array: # Get a description of the contest. Office? Measure? contest_overview_array = ballot_for_one_voter_array[contest_id] if contest_overview_array['type'] == "office": # Get a description of the office the candidates are competing for # contest_office_description_json = contest_overview_array['office'] # With the contest_id, we can look up who is running politicians_running_for_one_contest_array = [] if load_from_url: print "TO BE IMPLEMENTED: Load MapLight JSON for a contest from URL" else: json_file_with_the_data_from_this_contest = MAPLIGHT_SAMPLE_CONTEST_JSON_FILE.format( contest_id=contest_id) try: with open(json_file_with_the_data_from_this_contest ) as json_data: politicians_running_for_one_contest_array = json.load( json_data) except Exception as e: handle_exception_silently(e) print "File {file_path} not found.".format( file_path=json_file_with_the_data_from_this_contest ) # Don't try to process the file if it doesn't exist, but go to the next entry continue import_maplight_contest_office_candidates_from_array( politicians_running_for_one_contest_array) # Also add measure return
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 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 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: """ 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) 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 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 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, } return render(request, 'organization/organization_position_edit.html', template_values)
def save_ballot_item_for_voter( self, voter_id, google_civic_election_id, google_civic_district_ocd_id, google_ballot_order, local_ballot_order): try: # Just try to save. If it is a duplicate entry, the save will fail due to unique requirements google_civic_ballot_item = GoogleCivicBallotItem(voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, google_ballot_order=google_ballot_order, ballot_order=local_ballot_order,) google_civic_ballot_item.save() except GoogleCivicBallotItem.DoesNotExist as e: handle_exception_silently(e)
def import_maplight_from_json(request): load_from_url = False ballot_for_one_voter_array = [] if load_from_url: # Request json file from Maplight servers print "TO BE IMPLEMENTED: Load Maplight JSON from url" # request = requests.get(VOTER_INFO_URL, params={ # "key": GOOGLE_CIVIC_API_KEY, # This comes from an environment variable # "address": "254 Hartford Street San Francisco CA", # "electionId": "2000", # }) # structured_json = json.loads(request.text) else: # Load saved json from local file print "Loading Maplight sample JSON from local file" with open(MAPLIGHT_SAMPLE_BALLOT_JSON_FILE) as ballot_for_one_voter_json: ballot_for_one_voter_array = json.load(ballot_for_one_voter_json) # A MapLight ballot query is essentially an array of contests with the key as the contest_id if ballot_for_one_voter_array and len(ballot_for_one_voter_array): # Parse the JSON here. This JSON is a list of contests on the ballot for one voter. for contest_id in ballot_for_one_voter_array: # Get a description of the contest. Office? Measure? contest_overview_array = ballot_for_one_voter_array[contest_id] if contest_overview_array['type'] == "office": # Get a description of the office the candidates are competing for # contest_office_description_json = contest_overview_array['office'] # With the contest_id, we can look up who is running politicians_running_for_one_contest_array = [] if load_from_url: print "TO BE IMPLEMENTED: Load MapLight JSON for a contest from URL" else: json_file_with_the_data_from_this_contest = MAPLIGHT_SAMPLE_CONTEST_JSON_FILE.format( contest_id=contest_id) try: with open(json_file_with_the_data_from_this_contest) as json_data: politicians_running_for_one_contest_array = json.load(json_data) except Exception as e: handle_exception_silently(e) print "File {file_path} not found.".format(file_path=json_file_with_the_data_from_this_contest) # Don't try to process the file if it doesn't exist, but go to the next entry continue import_maplight_contest_office_candidates_from_array(politicians_running_for_one_contest_array) # Also add measure return
def save_ballot_item_for_voter(self, voter_id, google_civic_election_id, google_civic_district_ocd_id, google_ballot_order, local_ballot_order): try: # Just try to save. If it is a duplicate entry, the save will fail due to unique requirements google_civic_ballot_item = GoogleCivicBallotItem( voter_id=voter_id, google_civic_election_id=google_civic_election_id, district_ocd_id=google_civic_district_ocd_id, google_ballot_order=google_ballot_order, ballot_order=local_ballot_order, ) google_civic_ballot_item.save() except GoogleCivicBallotItem.DoesNotExist as e: handle_exception_silently(e)
def retrieve_candidate_campaign(self, candidate_campaign_id, id_we_vote=None, candidate_id_maplight=None, candidate_name=None): 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 elif len(id_we_vote) > 0: candidate_campaign_on_stage = CandidateCampaign.objects.get( id_we_vote=id_we_vote) candidate_campaign_id = candidate_campaign_on_stage.id elif candidate_id_maplight > 0 and candidate_id_maplight != "": candidate_campaign_on_stage = CandidateCampaign.objects.get( id_maplight=candidate_id_maplight) candidate_campaign_id = candidate_campaign_on_stage.id elif len(candidate_name) > 0: candidate_campaign_on_stage = CandidateCampaign.objects.get( candidate_name=candidate_name) 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 = { 'success': True if candidate_campaign_id > 0 else False, '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 fetch_setting(self, setting_name): setting_name = setting_name.strip() try: if setting_name != '': we_vote_setting = WeVoteSetting.objects.get(name=setting_name) if we_vote_setting.value_type == WeVoteSetting.BOOLEAN: return we_vote_setting.boolean_value elif we_vote_setting.value_type == WeVoteSetting.INTEGER: return we_vote_setting.integer_value elif we_vote_setting.value_type == WeVoteSetting.STRING: return we_vote_setting.string_value except WeVoteSetting.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) return '' except WeVoteSetting.DoesNotExist as e: handle_exception_silently(e) return '' return ''
def fetch_setting(self, setting_name): setting_name = setting_name.strip() try: if setting_name != "": we_vote_setting = WeVoteSetting.objects.get(name=setting_name) if we_vote_setting.value_type == WeVoteSetting.BOOLEAN: return we_vote_setting.boolean_value elif we_vote_setting.value_type == WeVoteSetting.INTEGER: return we_vote_setting.integer_value elif we_vote_setting.value_type == WeVoteSetting.STRING: return we_vote_setting.string_value except WeVoteSetting.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) return "" except WeVoteSetting.DoesNotExist as e: handle_exception_silently(e) return "" return ""
def retrieve_voter_device_link(self, voter_device_id, voter_id, voter_device_link_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_device_link_on_stage = VoterDeviceLink() try: if voter_device_id is not '': voter_device_link_on_stage = VoterDeviceLink.objects.get( voter_device_id=voter_device_id) voter_device_link_id = voter_device_link_on_stage.id elif voter_id > 0: voter_device_link_on_stage = VoterDeviceLink.objects.get( voter_id=voter_id) # If still here, we found an existing position voter_device_link_id = voter_device_link_on_stage.id elif voter_device_link_id > 0: voter_device_link_on_stage = VoterDeviceLink.objects.get( id=voter_device_link_id) # If still here, we found an existing position voter_device_link_id = voter_device_link_on_stage.id else: voter_device_link_id = 0 except VoterDeviceLink.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except VoterDeviceLink.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, 'voter_device_link_found': True if voter_device_link_id > 0 else False, 'voter_device_link': voter_device_link_on_stage, } return results
def retrieve_follow_organization(self, follow_organization_id, voter_id, organization_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False follow_organization_on_stage = FollowOrganization() follow_organization_on_stage_id = 0 try: if follow_organization_id > 0: follow_organization_on_stage = FollowOrganization.objects.get( id=follow_organization_id) follow_organization_on_stage_id = organization_id.id elif voter_id > 0 and organization_id > 0: follow_organization_on_stage = FollowOrganization.objects.get( voter_id=voter_id, organization_id=organization_id) follow_organization_on_stage_id = follow_organization_on_stage.id except FollowOrganization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except FollowOrganization.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True follow_organization_on_stage_found = True if follow_organization_on_stage_id > 0 else False results = { 'success': True if follow_organization_on_stage_found else False, 'follow_organization_found': follow_organization_on_stage_found, 'follow_organization_id': follow_organization_on_stage_id, 'follow_organization': follow_organization_on_stage, 'is_following': follow_organization_on_stage.is_following(), 'is_not_following': follow_organization_on_stage.is_not_following(), 'is_ignoring': follow_organization_on_stage.is_ignoring(), 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results
def retrieve_voter(self, voter_id, email): voter_id = convert_to_int(voter_id) if not validate_email(email): # We do not want to search for an invalid email email = None error_result = False exception_does_not_exist = False exception_multiple_object_returned = False voter_on_stage = Voter() try: if voter_id > 0: voter_on_stage = Voter.objects.get(id=voter_id) # If still here, we found an existing voter voter_id = voter_on_stage.id elif email is not '' and email is not None: voter_on_stage = Voter.objects.get(email=email) # If still here, we found an existing voter voter_id = voter_on_stage.id else: voter_id = 0 error_result = True except Voter.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except Voter.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, 'voter_found': True if voter_id > 0 else False, 'voter_id': voter_id, 'voter': voter_on_stage, } return results
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) exception_multiple_object_returned = True except MapLightCandidate.DoesNotExist as e: handle_exception_silently(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_follow_organization(self, follow_organization_id, voter_id, organization_id): error_result = False exception_does_not_exist = False exception_multiple_object_returned = False follow_organization_on_stage = FollowOrganization() follow_organization_on_stage_id = 0 try: if follow_organization_id > 0: follow_organization_on_stage = FollowOrganization.objects.get(id=follow_organization_id) follow_organization_on_stage_id = organization_id.id elif voter_id > 0 and organization_id > 0: follow_organization_on_stage = FollowOrganization.objects.get( voter_id=voter_id, organization_id=organization_id) follow_organization_on_stage_id = follow_organization_on_stage.id except FollowOrganization.MultipleObjectsReturned as e: handle_record_found_more_than_one_exception(e) error_result = True exception_multiple_object_returned = True except FollowOrganization.DoesNotExist as e: handle_exception_silently(e) error_result = True exception_does_not_exist = True follow_organization_on_stage_found = True if follow_organization_on_stage_id > 0 else False results = { 'success': True if follow_organization_on_stage_found else False, 'follow_organization_found': follow_organization_on_stage_found, 'follow_organization_id': follow_organization_on_stage_id, 'follow_organization': follow_organization_on_stage, 'is_following': follow_organization_on_stage.is_following(), 'is_not_following': follow_organization_on_stage.is_not_following(), 'is_ignoring': follow_organization_on_stage.is_ignoring(), 'error_result': error_result, 'DoesNotExist': exception_does_not_exist, 'MultipleObjectsReturned': exception_multiple_object_returned, } return results
def organization_add_new_position_form_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) 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 organization_edit_view(request, organization_id): 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_candidate_campaign( self, candidate_campaign_id, id_we_vote=None, candidate_id_maplight=None, candidate_name=None): 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 elif len(id_we_vote) > 0: candidate_campaign_on_stage = CandidateCampaign.objects.get(id_we_vote=id_we_vote) candidate_campaign_id = candidate_campaign_on_stage.id elif candidate_id_maplight > 0 and candidate_id_maplight != "": candidate_campaign_on_stage = CandidateCampaign.objects.get(id_maplight=candidate_id_maplight) candidate_campaign_id = candidate_campaign_on_stage.id elif len(candidate_name) > 0: candidate_campaign_on_stage = CandidateCampaign.objects.get(candidate_name=candidate_name) 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 = { 'success': True if candidate_campaign_id > 0 else False, '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 import_we_vote_positions_from_json(request, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ if load_from_uri: # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers") request = requests.get(POSITIONS_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable }) structured_json = json.loads(request.text) else: # Load saved json from local file messages.add_message(request, messages.INFO, "Loading positions from local file") with open(POSITIONS_JSON_FILE) as json_data: structured_json = json.load(json_data) for one_position in structured_json: # Make sure we have the minimum required variables if len(one_position["id_we_vote"]) == 0 \ or len(one_position["organization_id_we_vote"]) == 0\ or len(one_position["candidate_campaign_id_we_vote"]) == 0: continue # Check to see if this position is already being used anywhere position_on_stage_found = False try: if len(one_position["id_we_vote"]) > 0: position_query = PositionEntered.objects.filter(id_we_vote=one_position["id_we_vote"]) if len(position_query): position_on_stage = position_query[0] position_on_stage_found = True except PositionEntered.DoesNotExist as e: handle_exception_silently(e) except Exception as e: handle_record_not_found_exception(e) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id(one_position["organization_id_we_vote"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_id_we_vote( one_position["candidate_campaign_id_we_vote"]) # TODO We need to look up measure_campaign_id measure_campaign_id = 0 try: if position_on_stage_found: # Update position_on_stage.id_we_vote = one_position["id_we_vote"] position_on_stage.organization_id = organization_id position_on_stage.candidate_campaign_id = candidate_campaign_id position_on_stage.measure_campaign_id = measure_campaign_id position_on_stage.date_entered = one_position["date_entered"] position_on_stage.election_id = one_position["election_id"] position_on_stage.stance = one_position["stance"] position_on_stage.more_info_url = one_position["more_info_url"] position_on_stage.statement_text = one_position["statement_text"] position_on_stage.statement_html = one_position["statement_html"] position_on_stage.save() messages.add_message(request, messages.INFO, "Position updated: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) else: # Create new position_on_stage = PositionEntered( id_we_vote=one_position["id_we_vote"], organization_id=organization_id, candidate_campaign_id=candidate_campaign_id, measure_campaign_id=measure_campaign_id, date_entered=one_position["date_entered"], election_id=one_position["election_id"], stance=one_position["stance"], more_info_url=one_position["more_info_url"], statement_text=one_position["statement_text"], statement_html=one_position["statement_html"], ) position_on_stage.save() messages.add_message(request, messages.INFO, "New position imported: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) except Exception as e: handle_record_not_saved_exception(e) messages.add_message(request, messages.ERROR, "Could not save position, id_we_vote: {id_we_vote}, " "organization_id_we_vote: {organization_id_we_vote}, " "candidate_campaign_id_we_vote: {candidate_campaign_id_we_vote}".format( id_we_vote=one_position["id_we_vote"], organization_id_we_vote=one_position["organization_id_we_vote"], candidate_campaign_id_we_vote=one_position["candidate_campaign_id_we_vote"], ))
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 import_we_vote_positions_from_json(request, load_from_uri=False): """ Get the json data, and either create new entries or update existing :return: """ if load_from_uri: # Request json file from We Vote servers messages.add_message(request, messages.INFO, "Loading positions from We Vote Master servers") request = requests.get( POSITIONS_URL, params={ "key": WE_VOTE_API_KEY, # This comes from an environment variable }) structured_json = json.loads(request.text) else: # Load saved json from local file messages.add_message(request, messages.INFO, "Loading positions from local file") with open(POSITIONS_JSON_FILE) as json_data: structured_json = json.load(json_data) for one_position in structured_json: # Make sure we have the minimum required variables if len(one_position["id_we_vote"]) == 0 \ or len(one_position["organization_id_we_vote"]) == 0\ or len(one_position["candidate_campaign_id_we_vote"]) == 0: continue # Check to see if this position is already being used anywhere position_on_stage_found = False try: if len(one_position["id_we_vote"]) > 0: position_query = PositionEntered.objects.filter( id_we_vote=one_position["id_we_vote"]) if len(position_query): position_on_stage = position_query[0] position_on_stage_found = True except PositionEntered.DoesNotExist as e: handle_exception_silently(e) except Exception as e: handle_record_not_found_exception(e) # We need to look up the local organization_id based on the newly saved we_vote_id organization_manager = OrganizationManager() organization_id = organization_manager.fetch_organization_id( one_position["organization_id_we_vote"]) # We need to look up the local candidate_campaign_id candidate_campaign_manager = CandidateCampaignManager() candidate_campaign_id = candidate_campaign_manager.fetch_candidate_campaign_id_from_id_we_vote( one_position["candidate_campaign_id_we_vote"]) # TODO We need to look up measure_campaign_id measure_campaign_id = 0 try: if position_on_stage_found: # Update position_on_stage.id_we_vote = one_position["id_we_vote"] position_on_stage.organization_id = organization_id position_on_stage.candidate_campaign_id = candidate_campaign_id position_on_stage.measure_campaign_id = measure_campaign_id position_on_stage.date_entered = one_position["date_entered"] position_on_stage.election_id = one_position["election_id"] position_on_stage.stance = one_position["stance"] position_on_stage.more_info_url = one_position["more_info_url"] position_on_stage.statement_text = one_position[ "statement_text"] position_on_stage.statement_html = one_position[ "statement_html"] position_on_stage.save() messages.add_message( request, messages.INFO, "Position updated: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) else: # Create new position_on_stage = PositionEntered( id_we_vote=one_position["id_we_vote"], organization_id=organization_id, candidate_campaign_id=candidate_campaign_id, measure_campaign_id=measure_campaign_id, date_entered=one_position["date_entered"], election_id=one_position["election_id"], stance=one_position["stance"], more_info_url=one_position["more_info_url"], statement_text=one_position["statement_text"], statement_html=one_position["statement_html"], ) position_on_stage.save() messages.add_message( request, messages.INFO, "New position imported: {id_we_vote}".format( id_we_vote=one_position["id_we_vote"])) except Exception as e: handle_record_not_saved_exception(e) messages.add_message( request, messages.ERROR, "Could not save position, id_we_vote: {id_we_vote}, " "organization_id_we_vote: {organization_id_we_vote}, " "candidate_campaign_id_we_vote: {candidate_campaign_id_we_vote}" .format( id_we_vote=one_position["id_we_vote"], organization_id_we_vote=one_position[ "organization_id_we_vote"], candidate_campaign_id_we_vote=one_position[ "candidate_campaign_id_we_vote"], ))