def retrieve_all_ballot_items_for_voter(self, voter_id, google_civic_election_id): ballot_item_list = [] ballot_item_list_found = False try: ballot_item_queryset = BallotItem.objects.order_by('local_ballot_order') ballot_item_queryset = ballot_item_queryset.filter(voter_id=voter_id) if positive_value_exists(google_civic_election_id): ballot_item_queryset = ballot_item_queryset.filter(google_civic_election_id=google_civic_election_id) ballot_item_list = ballot_item_queryset if len(ballot_item_list): ballot_item_list_found = True status = 'BALLOT_ITEMS_FOUND, retrieve_all_ballot_items_for_voter' else: status = 'NO_BALLOT_ITEMS_FOUND_0' except BallotItem.DoesNotExist: # No ballot items found. Not a problem. status = 'NO_BALLOT_ITEMS_FOUND_DoesNotExist' ballot_item_list = [] except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_ballot_items_for_voter ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) results = { 'success': True if ballot_item_list_found else False, 'status': status, 'google_civic_election_id': google_civic_election_id, 'voter_id': voter_id, 'ballot_item_list_found': ballot_item_list_found, 'ballot_item_list': ballot_item_list, } return results
def delete_all_ballot_items_for_voter(self, voter_id, google_civic_election_id): ballot_item_list_deleted = False try: ballot_item_queryset = BallotItem.objects.filter(voter_id=voter_id) if positive_value_exists(google_civic_election_id): ballot_item_queryset = ballot_item_queryset.filter(google_civic_election_id=google_civic_election_id) ballot_item_queryset.delete() ballot_item_list_deleted = True status = 'BALLOT_ITEMS_DELETED' except BallotItem.DoesNotExist: # No ballot items found. Not a problem. status = 'NO_BALLOT_ITEMS_DELETED_DoesNotExist' except Exception as e: handle_exception(e, logger=logger) status = 'FAILED delete_all_ballot_items_for_voter ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) results = { 'success': True if ballot_item_list_deleted else False, 'status': status, 'google_civic_election_id': google_civic_election_id, 'voter_id': voter_id, 'ballot_item_list_deleted': ballot_item_list_deleted, } return results
def delete_voter(self, email): email = self.normalize_email(email) voter_id = 0 voter_we_vote_id = "" voter_deleted = False if positive_value_exists(email) and validate_email(email): email_valid = True else: email_valid = False try: if email_valid: results = self.retrieve_voter(voter_id, email, voter_we_vote_id) if results["voter_found"]: voter = results["voter"] voter_id = voter.id voter.delete() voter_deleted = True except Exception as e: handle_exception(e, logger=logger) results = { "email_not_valid": True if not email_valid else False, "voter_deleted": voter_deleted, "voter_id": voter_id, } return results
def retrieve_all_measures_for_upcoming_election( self, google_civic_election_id=0, return_list_of_objects=False): measure_list_objects = [] measure_list_light = [] measure_list_found = False try: measure_queryset = ContestMeasure.objects.all() if positive_value_exists(google_civic_election_id): measure_queryset = measure_queryset.filter( google_civic_election_id=google_civic_election_id) else: # TODO Limit this search to upcoming_elections only pass measure_list_objects = measure_queryset if len(measure_list_objects): measure_list_found = True status = 'MEASURES_RETRIEVED' success = True else: status = 'NO_MEASURES_RETRIEVED' success = True except ContestMeasure.DoesNotExist: # No measures found. Not a problem. status = 'NO_MEASURES_FOUND_DoesNotExist' measure_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_measures_for_upcoming_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if measure_list_found: for measure in measure_list_objects: one_measure = { 'ballot_item_display_name': measure.measure_title, 'measure_we_vote_id': measure.we_vote_id, 'office_we_vote_id': '', 'candidate_we_vote_id': '', } measure_list_light.append(one_measure.copy()) results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'measure_list_found': measure_list_found, 'measure_list_objects': measure_list_objects if return_list_of_objects else [], 'measure_list_light': measure_list_light, } return results
def retrieve_possible_duplicate_offices(self, google_civic_election_id, office_name, state_code, we_vote_id_from_master=''): """ Find offices that match another office in all critical fields other than we_vote_id_from_master :param google_civic_election_id: :return: """ office_list_objects = [] office_list_found = False try: office_queryset = ContestOffice.objects.all() office_queryset = office_queryset.filter( google_civic_election_id=google_civic_election_id) office_queryset = office_queryset.filter( office_name__iexact=office_name) # Case doesn't matter office_queryset = office_queryset.filter( state_code__iexact=state_code) # Case doesn't matter # office_queryset = office_queryset.filter(district_id__exact=district_id) # office_queryset = office_queryset.filter(district_name__iexact=district_name) # Case doesn't matter # Ignore we_vote_id coming in from master server if positive_value_exists(we_vote_id_from_master): office_queryset = office_queryset.filter(~Q( we_vote_id__iexact=we_vote_id_from_master)) office_list_objects = office_queryset if len(office_list_objects): office_list_found = True status = 'OFFICES_RETRIEVED' success = True else: status = 'NO_OFFICES_RETRIEVED' success = True except ContestOffice.DoesNotExist: # No offices found. Not a problem. status = 'NO_OFFICES_FOUND_DoesNotExist' office_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_possible_duplicate_offices ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'office_list_found': office_list_found, 'office_list': office_list_objects, } return results
def retrieve_all_offices_for_upcoming_election(self, google_civic_election_id=0, return_list_of_objects=False): office_list_objects = [] office_list_light = [] office_list_found = False try: office_queryset = ContestOffice.objects.all() if positive_value_exists(google_civic_election_id): office_queryset = office_queryset.filter(google_civic_election_id=google_civic_election_id) else: # TODO Limit this search to upcoming_elections only pass office_queryset = office_queryset.order_by("office_name") office_list_objects = office_queryset if len(office_list_objects): office_list_found = True status = 'OFFICES_RETRIEVED' success = True else: status = 'NO_OFFICES_RETRIEVED' success = True except ContestOffice.DoesNotExist: # No offices found. Not a problem. status = 'NO_OFFICES_FOUND_DoesNotExist' office_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_offices_for_upcoming_election ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False if office_list_found: for office in office_list_objects: one_office = { 'ballot_item_display_name': office.office_name, 'measure_we_vote_id': '', 'office_we_vote_id': office.we_vote_id, 'candidate_we_vote_id': '', } office_list_light.append(one_office.copy()) results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'office_list_found': office_list_found, 'office_list_objects': office_list_objects if return_list_of_objects else [], 'office_list_light': office_list_light, } return results
def retrieve_all_candidates_for_upcoming_election(self, google_civic_election_id=0, return_list_of_objects=False): candidate_list_objects = [] candidate_list_light = [] candidate_list_found = False try: candidate_queryset = CandidateCampaign.objects.all() if positive_value_exists(google_civic_election_id): candidate_queryset = candidate_queryset.filter(google_civic_election_id=google_civic_election_id) else: # TODO Limit this search to upcoming_elections only pass candidate_queryset = candidate_queryset.order_by("candidate_name") candidate_list_objects = candidate_queryset if len(candidate_list_objects): candidate_list_found = True status = 'CANDIDATES_RETRIEVED' success = True else: status = 'NO_CANDIDATES_RETRIEVED' success = True except CandidateCampaign.DoesNotExist: # No candidates found. Not a problem. status = 'NO_CANDIDATES_FOUND_DoesNotExist' candidate_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_candidates_for_office ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) success = False if candidate_list_found: for candidate in candidate_list_objects: one_candidate = { 'ballot_item_display_name': candidate.display_candidate_name(), 'candidate_we_vote_id': candidate.we_vote_id, 'office_we_vote_id': candidate.contest_office_we_vote_id, 'measure_we_vote_id': '', } candidate_list_light.append(one_candidate.copy()) results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'candidate_list_found': candidate_list_found, 'candidate_list_objects': candidate_list_objects if return_list_of_objects else [], 'candidate_list_light': candidate_list_light, } return results
def retrieve_all_measures_for_upcoming_election(self, google_civic_election_id=0, return_list_of_objects=False): measure_list_objects = [] measure_list_light = [] measure_list_found = False try: measure_queryset = ContestMeasure.objects.all() if positive_value_exists(google_civic_election_id): measure_queryset = measure_queryset.filter(google_civic_election_id=google_civic_election_id) else: # TODO Limit this search to upcoming_elections only pass measure_list_objects = measure_queryset if len(measure_list_objects): measure_list_found = True status = "MEASURES_RETRIEVED" success = True else: status = "NO_MEASURES_RETRIEVED" success = True except ContestMeasure.DoesNotExist: # No measures found. Not a problem. status = "NO_MEASURES_FOUND_DoesNotExist" measure_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = "FAILED retrieve_all_measures_for_upcoming_election " "{error} [type: {error_type}]".format( error=e, error_type=type(e) ) success = False if measure_list_found: for measure in measure_list_objects: one_measure = { "ballot_item_display_name": measure.measure_title, "measure_we_vote_id": measure.we_vote_id, "office_we_vote_id": "", "candidate_we_vote_id": "", } measure_list_light.append(one_measure.copy()) results = { "success": success, "status": status, "google_civic_election_id": google_civic_election_id, "measure_list_found": measure_list_found, "measure_list_objects": measure_list_objects if return_list_of_objects else [], "measure_list_light": measure_list_light, } return results
def retrieve_possible_duplicate_offices(self, google_civic_election_id, office_name, state_code, we_vote_id_from_master=''): """ Find offices that match another office in all critical fields other than we_vote_id_from_master :param google_civic_election_id: :return: """ office_list_objects = [] office_list_found = False try: office_queryset = ContestOffice.objects.all() office_queryset = office_queryset.filter(google_civic_election_id=google_civic_election_id) office_queryset = office_queryset.filter(office_name__iexact=office_name) # Case doesn't matter office_queryset = office_queryset.filter(state_code__iexact=state_code) # Case doesn't matter # office_queryset = office_queryset.filter(district_id__exact=district_id) # office_queryset = office_queryset.filter(district_name__iexact=district_name) # Case doesn't matter # Ignore we_vote_id coming in from master server if positive_value_exists(we_vote_id_from_master): office_queryset = office_queryset.filter(~Q(we_vote_id__iexact=we_vote_id_from_master)) office_list_objects = office_queryset if len(office_list_objects): office_list_found = True status = 'OFFICES_RETRIEVED' success = True else: status = 'NO_OFFICES_RETRIEVED' success = True except ContestOffice.DoesNotExist: # No offices found. Not a problem. status = 'NO_OFFICES_FOUND_DoesNotExist' office_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_possible_duplicate_offices ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'office_list_found': office_list_found, 'office_list': office_list_objects, } return results
def retrieve_all_candidates_for_office(self, office_id, office_we_vote_id): candidate_list = [] candidate_list_found = False if not positive_value_exists(office_id) and not positive_value_exists(office_we_vote_id): status = 'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING' results = { 'success': True if candidate_list_found else False, 'status': status, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'candidate_list_found': candidate_list_found, 'candidate_list': candidate_list, } return results try: candidate_queryset = CandidateCampaign.objects.all() if positive_value_exists(office_id): candidate_queryset = candidate_queryset.filter(contest_office_id=office_id) elif positive_value_exists(office_we_vote_id): candidate_queryset = candidate_queryset.filter(contest_office_we_vote_id=office_we_vote_id) candidate_queryset = candidate_queryset.order_by('-twitter_followers_count') candidate_list = candidate_queryset if len(candidate_list): candidate_list_found = True status = 'CANDIDATES_RETRIEVED' else: status = 'NO_CANDIDATES_RETRIEVED' except CandidateCampaign.DoesNotExist: # No candidates found. Not a problem. status = 'NO_CANDIDATES_FOUND_DoesNotExist' candidate_list = [] except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_candidates_for_office ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) results = { 'success': True if candidate_list_found else False, 'status': status, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'candidate_list_found': candidate_list_found, 'candidate_list': candidate_list, } return results
def retrieve_all_candidates_for_office(self, office_id, office_we_vote_id): candidate_list = [] candidate_list_found = False if not positive_value_exists(office_id) and not positive_value_exists(office_we_vote_id): status = 'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING' results = { 'success': True if candidate_list_found else False, 'status': status, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'candidate_list_found': candidate_list_found, 'candidate_list': candidate_list, } return results try: candidate_queryset = CandidateCampaign.objects.all() if positive_value_exists(office_id): candidate_queryset = candidate_queryset.filter(contest_office_id=office_id) elif positive_value_exists(office_we_vote_id): candidate_queryset = candidate_queryset.filter(contest_office_we_vote_id=office_we_vote_id) candidate_queryset = candidate_queryset.order_by('candidate_name') candidate_list = candidate_queryset if len(candidate_list): candidate_list_found = True status = 'CANDIDATES_RETRIEVED' else: status = 'NO_CANDIDATES_RETRIEVED' except CandidateCampaign.DoesNotExist: # No candidates found. Not a problem. status = 'NO_CANDIDATES_FOUND_DoesNotExist' candidate_list = [] except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_candidates_for_office ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) results = { 'success': True if candidate_list_found else False, 'status': status, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'candidate_list_found': candidate_list_found, 'candidate_list': candidate_list, } return results
def organization_count(): organization_count_all = 0 try: organization_list_all = Organization.objects.all() organization_count_all = organization_list_all.count() success = True # We will want to cache a json file and only refresh it every couple of seconds (so it doesn't become # a bottle neck as we grow) except Exception as e: exception_message = "organizationCount: Unable to count list of Organizations from db." handle_exception(e, logger=logger, exception_message=exception_message) success = False json_data = {"success": success, "organization_count": organization_count_all} return HttpResponse(json.dumps(json_data), content_type="application/json")
def voter_count(): voter_count_all = 0 try: voter_list_all = Voter.objects.all() # In future, add a filter to only show voters who have actually done something # voter_list = voter_list.filter(id=voter_id) voter_count_all = voter_list_all.count() success = True # We will want to cache a json file and only refresh it every couple of seconds (so it doesn't become # a bottle neck as we grow) except Exception as e: exception_message = "voterCount: Unable to count list of Voters from db." handle_exception(e, logger=logger, exception_message=exception_message) success = False json_data = {"success": success, "voter_count": voter_count_all} return HttpResponse(json.dumps(json_data), content_type="application/json")
def retrieve_candidate_count_for_office(self, office_id, office_we_vote_id): if not positive_value_exists(office_id) and not positive_value_exists(office_we_vote_id): status = 'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING' results = { 'success': False, 'status': status, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'candidate_count': 0, } return results try: candidate_queryset = CandidateCampaign.objects.all() if positive_value_exists(office_id): candidate_queryset = candidate_queryset.filter(contest_office_id=office_id) elif positive_value_exists(office_we_vote_id): candidate_queryset = candidate_queryset.filter(contest_office_we_vote_id=office_we_vote_id) candidate_list = candidate_queryset candidate_count = candidate_list.count() success = True status = "CANDIDATE_COUNT_FOUND" except CandidateCampaign.DoesNotExist: # No candidates found. Not a problem. status = 'NO_CANDIDATES_FOUND_DoesNotExist' candidate_count = 0 success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_all_candidates_for_office ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False candidate_count = 0 results = { 'success': success, 'status': status, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'candidate_count': candidate_count, } return results
def organization_count(): organization_count_all = 0 try: organization_list_all = Organization.objects.all() organization_count_all = organization_list_all.count() success = True # We will want to cache a json file and only refresh it every couple of seconds (so it doesn't become # a bottle neck as we grow) except Exception as e: exception_message = "organizationCount: Unable to count list of Organizations from db." handle_exception(e, logger=logger, exception_message=exception_message) success = False json_data = { 'success': success, 'organization_count': organization_count_all, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def delete_voter_guide_possibility(self, voter_guide_possibility_id): voter_guide_possibility_id = convert_to_int(voter_guide_possibility_id) voter_guide_deleted = False try: if voter_guide_possibility_id: results = self.retrieve_voter_guide_possibility(voter_guide_possibility_id) if results["voter_guide_found"]: voter_guide = results["voter_guide"] voter_guide_possibility_id = voter_guide.id voter_guide.delete() voter_guide_deleted = True except Exception as e: handle_exception(e, logger=logger) results = { "success": voter_guide_deleted, "voter_guide_deleted": voter_guide_deleted, "voter_guide_possibility_id": voter_guide_possibility_id, } return results
def delete_quick_info_master(self, quick_info_master_id): quick_info_master_id = convert_to_int(quick_info_master_id) quick_info_master_deleted = False try: if quick_info_master_id: results = self.retrieve_quick_info_master(quick_info_master_id) if results['quick_info_master_found']: quick_info_master = results['quick_info_master'] quick_info_master_id = quick_info_master.id quick_info_master.delete() quick_info_master_deleted = True except Exception as e: handle_exception(e, logger=logger) results = { 'success': quick_info_master_deleted, 'quick_info_master_deleted': quick_info_master_deleted, 'quick_info_master_id': quick_info_master_id, } return results
def delete_voter_guide(self, voter_guide_id): voter_guide_id = convert_to_int(voter_guide_id) voter_guide_deleted = False try: if voter_guide_id: results = self.retrieve_voter_guide(voter_guide_id) if results['voter_guide_found']: voter_guide = results['voter_guide'] voter_guide_id = voter_guide.id voter_guide.delete() voter_guide_deleted = True except Exception as e: handle_exception(e, logger=logger) results = { 'success': voter_guide_deleted, 'voter_guide_deleted': voter_guide_deleted, 'voter_guide_id': voter_guide_id, } return results
def delete_organization(self, organization_id): organization_id = convert_to_int(organization_id) organization_deleted = False try: if organization_id: results = self.retrieve_organization(organization_id) if results['organization_found']: organization = results['organization'] organization_id = organization.id organization.delete() organization_deleted = True except Exception as e: handle_exception(e, logger=logger) results = { 'success': organization_deleted, 'organization_deleted': organization_deleted, 'organization_id': organization_id, } return results
def voter_count(): voter_count_all = 0 try: voter_list_all = Voter.objects.all() # In future, add a filter to only show voters who have actually done something # voter_list = voter_list.filter(id=voter_id) voter_count_all = voter_list_all.count() success = True # We will want to cache a json file and only refresh it every couple of seconds (so it doesn't become # a bottle neck as we grow) except Exception as e: exception_message = "voterCount: Unable to count list of Voters from db." handle_exception(e, logger=logger, exception_message=exception_message) success = False json_data = { 'success': success, 'voter_count': voter_count_all, } return HttpResponse(json.dumps(json_data), content_type='application/json')
def retrieve_candidates_from_non_unique_identifiers(self, twitter_handle, google_civic_election_id=0): candidate_list_objects = [] candidate_list_found = False twitter_handle_filtered = extract_twitter_handle_from_text_string(twitter_handle) try: candidate_queryset = CandidateCampaign.objects.all() candidate_queryset = candidate_queryset.filter(candidate_twitter_handle__iexact=twitter_handle_filtered) if positive_value_exists(google_civic_election_id): candidate_queryset = candidate_queryset.filter(google_civic_election_id=google_civic_election_id) candidate_queryset = candidate_queryset.order_by('-id') candidate_list_objects = candidate_queryset if len(candidate_list_objects): candidate_list_found = True status = 'CANDIDATES_RETRIEVED_FROM_TWITTER_HANDLE' success = True else: status = 'NO_CANDIDATES_RETRIEVED_FROM_TWITTER_HANDLE' success = True except CandidateCampaign.DoesNotExist: # No candidates found. Not a problem. status = 'NO_CANDIDATES_FOUND_FROM_TWITTER_HANDLE_DoesNotExist' candidate_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_candidates_from_non_unique_identifiers ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'candidate_list_found': candidate_list_found, 'candidate_list': candidate_list_objects, } return results
def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): """ :param voter_device_id: :param google_civic_election_id: This variable either was stored in a cookie, or passed in explicitly so we can get the ballot items related to that election. :return: """ # Get voter_id from the voter_device_id so we can figure out which ballot_items to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_id': 0, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } results = { 'success': False, 'json_data': json_data, 'google_civic_election_id': 0, # Force the clearing of google_civic_election_id } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'voter_id': voter_id, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } results = { 'success': False, 'json_data': json_data, 'google_civic_election_id': 0, # Force the clearing of google_civic_election_id } return results ballot_item_list_manager = BallotItemListManager() # If we get here without a google_civic_election_id, we need to choose one from the ballot items that are already # stored. If we proceed to retrieve_all_ballot_items_for_voter without a google_civic_election_id, we will get # ballot items from a variety of elections. # This logic looks for all of the elections we have ballot information for, and displays the most recent election # (not counting the test election) if not positive_value_exists(google_civic_election_id): google_civic_election_id = ballot_item_list_manager.fetch_most_recent_google_civic_election_id() # If an election id STILL wasn't found, then we probably don't have any ballot items stored locally, so we # need to go out to google civic. BUT we will proceed and attempt to retrieve ballot items without an election_id ballot_item_list = [] ballot_items_to_display = [] try: results = ballot_item_list_manager.retrieve_all_ballot_items_for_voter(voter_id, google_civic_election_id) success = results['success'] status = results['status'] ballot_item_list = results['ballot_item_list'] except Exception as e: status = 'FAILED voter_ballot_items_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for ballot_item in ballot_item_list: if ballot_item.contest_office_we_vote_id: kind_of_ballot_item = OFFICE ballot_item_id = ballot_item.contest_office_id we_vote_id = ballot_item.contest_office_we_vote_id elif ballot_item.contest_measure_we_vote_id: kind_of_ballot_item = MEASURE ballot_item_id = ballot_item.contest_measure_id we_vote_id = ballot_item.contest_measure_we_vote_id else: kind_of_ballot_item = '' if positive_value_exists(kind_of_ballot_item): one_ballot_item = { 'ballot_item_display_name': ballot_item.ballot_item_display_name, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'kind_of_ballot_item': kind_of_ballot_item, 'id': ballot_item_id, 'we_vote_id': we_vote_id, } ballot_items_to_display.append(one_ballot_item.copy()) json_data = { 'status': 'VOTER_BALLOT_ITEMS_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'ballot_item_list': ballot_items_to_display, 'google_civic_election_id': google_civic_election_id, } else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } results = { 'success': success, 'google_civic_election_id': google_civic_election_id, # We want to save google_civic_election_id in cookie 'json_data': json_data, } return results
def voter_guides_to_follow_retrieve(voter_device_id, google_civic_election_id=0): # Get voter_id from the voter_device_id so we can figure out which voter_guides to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'ERROR_GUIDES_TO_FOLLOW_NO_VOTER_DEVICE_ID', 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "ERROR_GUIDES_TO_FOLLOW_VOTER_NOT_FOUND_FROM_VOTER_DEVICE_ID", 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') # If the google_civic_election_id was found cached in a cookie and passed in, use that # If not, fetch it for this voter if not positive_value_exists(google_civic_election_id): google_civic_election_id = fetch_google_civic_election_id_for_voter_id(voter_id) voter_guide_list = [] voter_guides = [] try: voter_guide_list_object = VoterGuideList() results = voter_guide_list_object.retrieve_voter_guides_for_election(google_civic_election_id) success = results['success'] status = results['status'] voter_guide_list = results['voter_guide_list'] except Exception as e: status = 'FAILED voter_guides_to_follow_retrieve, retrieve_voter_guides_for_election ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for voter_guide in voter_guide_list: one_voter_guide = { 'google_civic_election_id': voter_guide.google_civic_election_id, 'voter_guide_owner_type': voter_guide.voter_guide_owner_type, 'organization_we_vote_id': voter_guide.organization_we_vote_id, 'public_figure_we_vote_id': voter_guide.public_figure_we_vote_id, 'owner_voter_id': voter_guide.owner_voter_id, 'last_updated': voter_guide.last_updated.strftime('%Y-%m-%d %H:%M'), } voter_guides.append(one_voter_guide.copy()) json_data = { 'status': 'VOTER_GUIDES_TO_FOLLOW_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_guides': voter_guides, } else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_guides': [], } return HttpResponse(json.dumps(json_data), content_type='application/json')
def retrieve_possible_duplicate_measures(self, measure_title, google_civic_election_id, measure_url, maplight_id, vote_smart_id, we_vote_id_from_master=''): measure_list_objects = [] filters = [] measure_list_found = False try: measure_queryset = ContestMeasure.objects.all() measure_queryset = measure_queryset.filter(google_civic_election_id=google_civic_election_id) # We don't look for office_we_vote_id because of the chance that locally we are using a # different we_vote_id # measure_queryset = measure_queryset.filter(contest_office_we_vote_id__iexact=office_we_vote_id) # Ignore entries with we_vote_id coming in from master server if positive_value_exists(we_vote_id_from_master): measure_queryset = measure_queryset.filter(~Q(we_vote_id__iexact=we_vote_id_from_master)) # We want to find candidates with *any* of these values if positive_value_exists(measure_title): new_filter = Q(measure_title__iexact=measure_title) filters.append(new_filter) if positive_value_exists(measure_url): new_filter = Q(measure_url__iexact=measure_url) filters.append(new_filter) if positive_value_exists(maplight_id): new_filter = Q(maplight_id=maplight_id) filters.append(new_filter) if positive_value_exists(vote_smart_id): new_filter = Q(vote_smart_id=vote_smart_id) filters.append(new_filter) # Add the first query if len(filters): final_filters = filters.pop() # ...and "OR" the remaining items in the list for item in filters: final_filters |= item measure_queryset = measure_queryset.filter(final_filters) measure_list_objects = measure_queryset if len(measure_list_objects): measure_list_found = True status = 'DUPLICATE_MEASURES_RETRIEVED' success = True else: status = 'NO_DUPLICATE_MEASURES_RETRIEVED' success = True except ContestMeasure.DoesNotExist: # No candidates found. Not a problem. status = 'NO_DUPLICATE_MEASURES_FOUND_DoesNotExist' measure_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_possible_duplicate_measures ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'measure_list_found': measure_list_found, 'measure_list': measure_list_objects, } return results
def import_legislators_current_csv(): """ This is a very simple method that is hard coded to the UnitedStates.io CSV Field names. We are saving the contents of this file locally so we can 1) share the data on ElectionDataSummary.org dynamically 2) save it/merge it into the correct We Vote tables later :return: """ with open(LEGISLATORS_CURRENT_CSV_FILE, 'rU') as legislators_current_data: legislators_current_data.readline() # Skip the header reader = csv.reader(legislators_current_data) # Create a regular tuple reader for index, legislator_row in enumerate(reader): # if index > 7: # break logger.debug("import_legislators_current_csv: " + legislator_row[0]) # For debugging legislator_entry_found = False # Do we have a record of this legislator based on bioguide_id? if legislator_row[18] != "": try: query1 = TheUnitedStatesIoLegislatorCurrent.objects.all() query1 = query1.filter(bioguide_id__exact=legislator_row[18]) # Was at least one existing entry found based on the above criteria? if len(query1): legislator_entry = query1[0] legislator_entry_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) if not legislator_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on bioguide id # ...so check to see if we have a record of this legislator based on govtrack id? if legislator_row[23] != "": try: query2 = TheUnitedStatesIoLegislatorCurrent.objects.all() query2 = query2.filter(govtrack_id__exact=legislator_row[23]) # Was at least one existing entry found based on the above criteria? if len(query2): legislator_entry = query2[0] legislator_entry_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) if not legislator_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on govtrack id # ...so create a new entry legislator_entry = TheUnitedStatesIoLegislatorCurrent( last_name=legislator_row[0], # "last_name", # row[0] first_name=legislator_row[1], # "first_name", # row[1] ) legislator_entry.last_name = legislator_row[0] # "last_name", # row[0] legislator_entry.first_name = legislator_row[1] # "first_name", # row[1] legislator_entry.birth_date = legislator_row[2] # "birthday", # row[2] legislator_entry.gender = legislator_row[3] # "gender", # row[3] # "type", # row[4] legislator_entry.state = legislator_row[5] # "state", # row[5] # "district", # row[6] # Convert this to ocd district legislator_entry.party = legislator_row[7] # "party", # row[7] # "url", # row[8] # "address", # row[9] # "phone", # row[10] # "contact_form", # row[11] # "rss_url", # row[12] # "twitter", # row[13] # "facebook", # row[14] # "facebook_id", # row[15] # "youtube", # row[16] # "youtube_id", # row[17] legislator_entry.bioguide_id = legislator_row[18] # "bioguide_id", # row[18] legislator_entry.thomas_id = legislator_row[19] # "thomas_id", # row[19] legislator_entry.opensecrets_id = legislator_row[20] # "opensecrets_id", # row[20] legislator_entry.lis_id = legislator_row[21] # "lis_id", # row[21] legislator_entry.cspan_id = legislator_row[22] # "cspan_id", # row[22] legislator_entry.govtrack_id = legislator_row[23] # "govtrack_id", # row[23] legislator_entry.votesmart_id = legislator_row[24] # "votesmart_id", # row[24] legislator_entry.ballotpedia_id = legislator_row[25] # "ballotpedia_id", # row[25] legislator_entry.washington_post_id = legislator_row[26] # "washington_post_id", # row[26] legislator_entry.icpsr_id = legislator_row[27] # "icpsr_id", # row[27] legislator_entry.wikipedia_id = legislator_row[28] # "wikipedia_id" # row[28] # Add "try/exception" so we know when entry isn't saved due to unique requirement try: legislator_entry.save() except Exception as e: handle_exception(e, logger=logger)
def transfer_theunitedstatesio_cached_data_to_wevote_tables(): """ In this method, we take the cached theunitedstatesio data and move it into the core We Vote data :return: """ print "Running transfer_theunitedstatesio_cached_data_to_wevote_tables()" legislators_current_query = TheUnitedStatesIoLegislatorCurrent.objects.all() # Only retrieve entries that haven't been processed yet # legislators_current_query = legislators_current_query.filter(was_processed=False) for legislator_current_entry in legislators_current_query: print 'Transferring: ' + str(legislator_current_entry.id) + ':' \ + legislator_current_entry.first_name + ' ' + legislator_current_entry.last_name politician_entry_found = False ######################### # Search the Politician's table to see if we already have an entry for this person # Do we have a record of this politician based on id_bioguide? if legislator_current_entry.bioguide_id != "": try: # Try to find earlier version based on the bioguide identifier query1 = Politician.objects.all() query1 = query1.filter(id_bioguide__exact=legislator_current_entry.bioguide_id) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] politician_entry_found = True except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on bioguide id # ...so check to see if we have a record of this legislator based on govtrack id? if legislator_current_entry.govtrack_id != "": try: query1 = Politician.objects.all() query1 = query1.filter(id_govtrack__exact=legislator_current_entry.govtrack_id) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] print "FOUND" politician_entry_found = True else: print "NOT FOUND" except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on id_govtrack # ...so check to see if we have a record of this legislator based on full_name_google_civic if legislator_current_entry.first_name and legislator_current_entry.last_name: try: full_name_assembled_guess = \ legislator_current_entry.first_name+" "+legislator_current_entry.last_name print "Searching for existing full_name_google_civic: {full_name_assembled}".format( full_name_assembled=full_name_assembled_guess) query1 = Politician.objects.all() query1 = query1.filter(full_name_google_civic=full_name_assembled_guess) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] print "FOUND" politician_entry_found = True else: print "NOT FOUND" except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on full_name_google_civic # ...so check to see if we have a record of this legislator based on full_name_assembled if legislator_current_entry.first_name and legislator_current_entry.last_name: try: full_name_assembled_guess = \ legislator_current_entry.first_name+" "+legislator_current_entry.last_name print "Searching for existing full_name_assembled: {full_name_assembled}".format( full_name_assembled=full_name_assembled_guess) query1 = Politician.objects.all() query1 = query1.filter(full_name_assembled=full_name_assembled_guess) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] print "FOUND" politician_entry_found = True else: print "NOT FOUND" except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found # ...so create a new entry politician_entry = Politician( last_name=legislator_current_entry.last_name, first_name=legislator_current_entry.first_name, full_name_assembled=legislator_current_entry.first_name+" "+legislator_current_entry.last_name, ) politician_entry.last_name = legislator_current_entry.last_name politician_entry.first_name = legislator_current_entry.first_name politician_entry.full_name_assembled = \ legislator_current_entry.first_name+" "+legislator_current_entry.last_name politician_entry.birth_date = legislator_current_entry.birthday politician_entry.gender = legislator_current_entry.gender politician_entry.id_bioguide = legislator_current_entry.bioguide_id politician_entry.id_thomas = legislator_current_entry.thomas_id politician_entry.id_opensecrets = legislator_current_entry.opensecrets_id politician_entry.id_lis = legislator_current_entry.lis_id politician_entry.id_cspan = legislator_current_entry.cspan_id politician_entry.id_govtrack = legislator_current_entry.govtrack_id politician_entry.id_votesmart = legislator_current_entry.votesmart_id politician_entry.id_ballotpedia = legislator_current_entry.ballotpedia_id politician_entry.id_washington_post = legislator_current_entry.washington_post_id politician_entry.id_icpsr = legislator_current_entry.icpsr_id politician_entry.id_wikipedia = legislator_current_entry.wikipedia_id # OTHER FIELDS # "type", # row[4] politician_entry.state_code = legislator_current_entry.state # "state", # row[5] # "district", # row[6] politician_entry.party = legislator_current_entry.party # "party", # row[7] # "url", # row[8] # "address", # row[9] # "phone", # row[10] # "contact_form", # row[11] # "rss_url", # row[12] # "twitter", # row[13] # "facebook", # row[14] # "facebook_id", # row[15] # "youtube", # row[16] # "youtube_id", # row[17] # We use "try/exception" so we know when entry isn't saved due to unique requirement # This works! Bigger question -- how to deal with exceptions in general? try: politician_entry.save() # Mark the source entry as was_processed so we don't try to import the same data again # legislator_current_entry.save() except Exception as e: handle_exception(e)
def import_legislators_current_csv(): """ This is a very simple method that is hard coded to the UnitedStates.io CSV Field names. We are saving the contents of this file locally so we can 1) share the data on ElectionDataSummary.org dynamically 2) save it/merge it into the correct We Vote tables later :return: """ with open(LEGISLATORS_CURRENT_CSV_FILE, 'rU') as legislators_current_data: legislators_current_data.readline() # Skip the header reader = csv.reader( legislators_current_data) # Create a regular tuple reader for index, legislator_row in enumerate(reader): # if index > 7: # break logger.debug("import_legislators_current_csv: " + legislator_row[0]) # For debugging legislator_entry_found = False # Do we have a record of this legislator based on bioguide_id? if legislator_row[18] != "": try: query1 = TheUnitedStatesIoLegislatorCurrent.objects.all() query1 = query1.filter( bioguide_id__exact=legislator_row[18]) # Was at least one existing entry found based on the above criteria? if len(query1): legislator_entry = query1[0] legislator_entry_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) if not legislator_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on bioguide id # ...so check to see if we have a record of this legislator based on govtrack id? if legislator_row[23] != "": try: query2 = TheUnitedStatesIoLegislatorCurrent.objects.all( ) query2 = query2.filter( govtrack_id__exact=legislator_row[23]) # Was at least one existing entry found based on the above criteria? if len(query2): legislator_entry = query2[0] legislator_entry_found = True except Exception as e: handle_record_not_found_exception(e, logger=logger) if not legislator_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on govtrack id # ...so create a new entry legislator_entry = TheUnitedStatesIoLegislatorCurrent( last_name=legislator_row[ 0], # "last_name", # row[0] first_name=legislator_row[ 1], # "first_name", # row[1] ) legislator_entry.last_name = legislator_row[ 0] # "last_name", # row[0] legislator_entry.first_name = legislator_row[ 1] # "first_name", # row[1] legislator_entry.birth_date = legislator_row[ 2] # "birthday", # row[2] legislator_entry.gender = legislator_row[ 3] # "gender", # row[3] # "type", # row[4] legislator_entry.state = legislator_row[ 5] # "state", # row[5] # "district", # row[6] # Convert this to ocd district legislator_entry.party = legislator_row[ 7] # "party", # row[7] # "url", # row[8] # "address", # row[9] # "phone", # row[10] # "contact_form", # row[11] # "rss_url", # row[12] # "twitter", # row[13] # "facebook", # row[14] # "facebook_id", # row[15] # "youtube", # row[16] # "youtube_id", # row[17] legislator_entry.bioguide_id = legislator_row[ 18] # "bioguide_id", # row[18] legislator_entry.thomas_id = legislator_row[ 19] # "thomas_id", # row[19] legislator_entry.opensecrets_id = legislator_row[ 20] # "opensecrets_id", # row[20] legislator_entry.lis_id = legislator_row[ 21] # "lis_id", # row[21] legislator_entry.cspan_id = legislator_row[ 22] # "cspan_id", # row[22] legislator_entry.govtrack_id = legislator_row[ 23] # "govtrack_id", # row[23] legislator_entry.votesmart_id = legislator_row[ 24] # "votesmart_id", # row[24] legislator_entry.ballotpedia_id = legislator_row[ 25] # "ballotpedia_id", # row[25] legislator_entry.washington_post_id = legislator_row[ 26] # "washington_post_id", # row[26] legislator_entry.icpsr_id = legislator_row[ 27] # "icpsr_id", # row[27] legislator_entry.wikipedia_id = legislator_row[ 28] # "wikipedia_id" # row[28] # Add "try/exception" so we know when entry isn't saved due to unique requirement try: legislator_entry.save() except Exception as e: handle_exception(e, logger=logger)
def voter_ballot_items_retrieve_for_api(voter_device_id, google_civic_election_id): """ :param voter_device_id: :param google_civic_election_id: This variable either was stored in a cookie, or passed in explicitly so we can get the ballot items related to that election. :return: """ # Get voter_id from the voter_device_id so we can figure out which ballot_items to offer results = is_voter_device_id_valid(voter_device_id) if not results["success"]: json_data = { "status": "VALID_VOTER_DEVICE_ID_MISSING", "success": False, "voter_id": 0, "voter_device_id": voter_device_id, "ballot_item_list": [], "google_civic_election_id": google_civic_election_id, } results = { "success": False, "json_data": json_data, "google_civic_election_id": 0, # Force the clearing of google_civic_election_id } return results voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { "status": "VALID_VOTER_ID_MISSING", "success": False, "voter_id": voter_id, "voter_device_id": voter_device_id, "ballot_item_list": [], "google_civic_election_id": google_civic_election_id, } results = { "success": False, "json_data": json_data, "google_civic_election_id": 0, # Force the clearing of google_civic_election_id } return results ballot_item_list_manager = BallotItemListManager() # If we get here without a google_civic_election_id, we need to choose one from the ballot items that are already # stored. If we proceed to retrieve_all_ballot_items_for_voter without a google_civic_election_id, we will get # ballot items from a variety of elections. # This logic looks for all of the elections we have ballot information for, and displays the most recent election # (not counting the test election) if not positive_value_exists(google_civic_election_id): google_civic_election_id = ballot_item_list_manager.fetch_most_recent_google_civic_election_id() # If an election id STILL wasn't found, then we probably don't have any ballot items stored locally, so we # need to go out to google civic. BUT we will proceed and attempt to retrieve ballot items without an election_id ballot_item_list = [] ballot_items_to_display = [] try: results = ballot_item_list_manager.retrieve_all_ballot_items_for_voter(voter_id, google_civic_election_id) success = results["success"] status = results["status"] ballot_item_list = results["ballot_item_list"] except Exception as e: status = "FAILED voter_ballot_items_retrieve. " "{error} [type: {error_type}]".format( error=e, error_type=type(e) ) handle_exception(e, logger=logger, exception_message=status) success = False if success: for ballot_item in ballot_item_list: if ballot_item.contest_office_we_vote_id: kind_of_ballot_item = OFFICE ballot_item_id = ballot_item.contest_office_id we_vote_id = ballot_item.contest_office_we_vote_id try: candidate_list_object = CandidateCampaignList() results = candidate_list_object.retrieve_all_candidates_for_office(ballot_item_id, we_vote_id) candidates_to_display = [] if results["candidate_list_found"]: candidate_list = results["candidate_list"] for candidate in candidate_list: # This should match values returned in candidates_retrieve_for_api one_candidate = { "id": candidate.id, "we_vote_id": candidate.we_vote_id, "ballot_item_display_name": candidate.candidate_name, "candidate_photo_url": candidate.fetch_photo_url(), "order_on_ballot": candidate.order_on_ballot, "kind_of_ballot_item": CANDIDATE, } candidates_to_display.append(one_candidate.copy()) except Exception as e: # status = 'FAILED candidates_retrieve. ' \ # '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) candidates_to_display = [] one_ballot_item = { "ballot_item_display_name": ballot_item.ballot_item_display_name, "google_civic_election_id": ballot_item.google_civic_election_id, "google_ballot_placement": ballot_item.google_ballot_placement, "local_ballot_order": ballot_item.local_ballot_order, "kind_of_ballot_item": kind_of_ballot_item, "id": ballot_item_id, "we_vote_id": we_vote_id, "candidate_list": candidates_to_display, } ballot_items_to_display.append(one_ballot_item.copy()) elif ballot_item.contest_measure_we_vote_id: kind_of_ballot_item = MEASURE ballot_item_id = ballot_item.contest_measure_id we_vote_id = ballot_item.contest_measure_we_vote_id one_ballot_item = { "ballot_item_display_name": ballot_item.ballot_item_display_name, "google_civic_election_id": ballot_item.google_civic_election_id, "google_ballot_placement": ballot_item.google_ballot_placement, "local_ballot_order": ballot_item.local_ballot_order, "kind_of_ballot_item": kind_of_ballot_item, "id": ballot_item_id, "we_vote_id": we_vote_id, } ballot_items_to_display.append(one_ballot_item.copy()) json_data = { "status": "VOTER_BALLOT_ITEMS_RETRIEVED", "success": True, "voter_device_id": voter_device_id, "ballot_item_list": ballot_items_to_display, "google_civic_election_id": google_civic_election_id, } else: json_data = { "status": status, "success": False, "voter_device_id": voter_device_id, "ballot_item_list": [], "google_civic_election_id": google_civic_election_id, } results = { "success": success, "google_civic_election_id": google_civic_election_id, # We want to save google_civic_election_id in cookie "json_data": json_data, } return results
def ballot_item_options_retrieve_for_api(google_civic_election_id=0): """ This function returns a normalized list of candidates and measures so we can pre-populate form fields. Not specific to one voter. :param google_civic_election_id: :return: """ status = "" try: candidate_list_object = CandidateCampaignListManager() results = candidate_list_object.retrieve_all_candidates_for_upcoming_election( google_civic_election_id) candidate_success = results['success'] status += results['status'] candidate_list = results['candidate_list_light'] except Exception as e: status += 'FAILED ballot_item_options_retrieve_for_api, candidate_list. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) candidate_list = [] candidate_success = False try: office_list_object = ContestOfficeListManager() results = office_list_object.retrieve_all_offices_for_upcoming_election( google_civic_election_id) office_success = results['success'] status += ' ' + results['status'] office_list = results['office_list_light'] except Exception as e: status += 'FAILED ballot_item_options_retrieve_for_api, office_list. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) office_list = [] office_success = False try: measure_list_object = ContestMeasureList() results = measure_list_object.retrieve_all_measures_for_upcoming_election( google_civic_election_id) measure_success = results['success'] status += ' ' + results['status'] measure_list = results['measure_list_light'] except Exception as e: status += 'FAILED ballot_item_options_retrieve_for_api, measure_list. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) measure_list = [] measure_success = False ballot_items_to_display = [] if candidate_success and len(candidate_list): for candidate in candidate_list: ballot_items_to_display.append(candidate.copy()) if office_success and len(office_list): for office in office_list: ballot_items_to_display.append(office.copy()) if measure_success and len(measure_list): for measure in measure_list: ballot_items_to_display.append(measure.copy()) json_data = { 'status': status, 'success': candidate_success or measure_success, 'ballot_item_list': ballot_items_to_display, 'google_civic_election_id': google_civic_election_id, } results = { 'status': status, 'success': candidate_success or measure_success, 'google_civic_election_id': google_civic_election_id, # We want to save google_civic_election_id in cookie 'json_data': json_data, } return results
def voter_ballot_items_retrieve_for_one_election_for_api( voter_device_id, voter_id, google_civic_election_id): """ :param voter_device_id: :param voter_id: :param google_civic_election_id: This variable was passed in explicitly so we can get the ballot items related to that election. :return: """ ballot_item_list_manager = BallotItemListManager() ballot_item_list = [] ballot_items_to_display = [] try: results = ballot_item_list_manager.retrieve_all_ballot_items_for_voter( voter_id, google_civic_election_id) success = results['success'] status = results['status'] ballot_item_list = results['ballot_item_list'] except Exception as e: status = 'FAILED voter_ballot_items_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for ballot_item in ballot_item_list: if ballot_item.contest_office_we_vote_id: kind_of_ballot_item = OFFICE ballot_item_id = ballot_item.contest_office_id we_vote_id = ballot_item.contest_office_we_vote_id try: candidate_list_object = CandidateCampaignListManager() results = candidate_list_object.retrieve_all_candidates_for_office( ballot_item_id, we_vote_id) candidates_to_display = [] if results['candidate_list_found']: candidate_list = results['candidate_list'] for candidate in candidate_list: # This should match values returned in candidates_retrieve_for_api one_candidate = { 'id': candidate.id, 'we_vote_id': candidate.we_vote_id, 'ballot_item_display_name': candidate.display_candidate_name(), 'candidate_photo_url': candidate.candidate_photo_url(), 'party': candidate.political_party_display(), 'order_on_ballot': candidate.order_on_ballot, 'kind_of_ballot_item': CANDIDATE, 'twitter_handle': candidate.candidate_twitter_handle, 'twitter_description': candidate.twitter_description, 'twitter_followers_count': candidate.twitter_followers_count, } candidates_to_display.append(one_candidate.copy()) except Exception as e: # status = 'FAILED candidates_retrieve. ' \ # '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) candidates_to_display = [] one_ballot_item = { 'ballot_item_display_name': ballot_item.ballot_item_display_name, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'kind_of_ballot_item': kind_of_ballot_item, 'id': ballot_item_id, 'we_vote_id': we_vote_id, 'candidate_list': candidates_to_display, } ballot_items_to_display.append(one_ballot_item.copy()) elif ballot_item.contest_measure_we_vote_id: kind_of_ballot_item = MEASURE ballot_item_id = ballot_item.contest_measure_id we_vote_id = ballot_item.contest_measure_we_vote_id one_ballot_item = { 'ballot_item_display_name': ballot_item.ballot_item_display_name, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'measure_subtitle': ballot_item.measure_subtitle, 'kind_of_ballot_item': kind_of_ballot_item, 'id': ballot_item_id, 'we_vote_id': we_vote_id, } ballot_items_to_display.append(one_ballot_item.copy()) results = { 'status': 'VOTER_BALLOT_ITEMS_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'ballot_item_list': ballot_items_to_display, 'google_civic_election_id': google_civic_election_id, } else: results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } return results
def organization_search_find_any_possibilities(self, organization_name, organization_twitter_handle, organization_website, organization_email): """ We want to find *any* possible organization that includes any of the search terms :param organization_name: :param organization_twitter_handle: :param organization_website: :param organization_email: :return: """ organization_list_for_json = {} try: filters = [] organization_list_for_json = [] organization_objects_list = [] if positive_value_exists(organization_name): new_filter = Q(organization_name__icontains=organization_name) filters.append(new_filter) if positive_value_exists(organization_twitter_handle): new_filter = Q(organization_twitter_handle__icontains=organization_twitter_handle) filters.append(new_filter) if positive_value_exists(organization_website): new_filter = Q(organization_website__icontains=organization_website) filters.append(new_filter) if positive_value_exists(organization_email): new_filter = Q(organization_email__icontains=organization_email) filters.append(new_filter) # Add the first query if len(filters): final_filters = filters.pop() # ...and "OR" the remaining items in the list for item in filters: final_filters |= item organization_objects_list = Organization.objects.filter(final_filters) if len(organization_objects_list): organizations_found = True status = 'ORGANIZATIONS_RETRIEVED' for organization in organization_objects_list: one_organization_json = { 'organization_id': organization.id, 'organization_we_vote_id': organization.we_vote_id, 'organization_name': organization.organization_name if positive_value_exists( organization.organization_name) else '', 'organization_website': organization.organization_website if positive_value_exists( organization.organization_website) else '', 'organization_twitter_handle': organization.organization_twitter_handle if positive_value_exists( organization.organization_twitter_handle) else '', 'organization_email': organization.organization_email if positive_value_exists( organization.organization_email) else '', 'organization_facebook': organization.organization_facebook if positive_value_exists( organization.organization_facebook) else '', } organization_list_for_json.append(one_organization_json) else: organizations_found = False status = 'NO_ORGANIZATIONS_RETRIEVED' success = True except Organization.DoesNotExist: # No organizations found. Not a problem. organizations_found = False status = 'NO_ORGANIZATIONS_FOUND_DoesNotExist' success = True # We are still successful if no organizations are found except Exception as e: organizations_found = False handle_exception(e, logger=logger) status = 'FAILED organization_search_find_any_possibilities ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) success = False results = { 'status': status, 'success': success, 'organizations_found': organizations_found, 'organizations_list': organization_list_for_json, } return results
def candidates_retrieve(office_id, office_we_vote_id): """ Used by the api :param office_id: :param office_we_vote_id: :return: """ # NOTE: Candidates retrieve is independent of *who* wants to see the data. Candidates retrieve never triggers # a ballot data lookup from Google Civic, like voterBallotItems does if not positive_value_exists(office_id) and not positive_value_exists(office_we_vote_id): # At this point if we don't have a google_civic_election_id, then we don't have an upcoming election status = 'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING' json_data = { 'status': status, 'success': False, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'google_civic_election_id': 0, 'candidate_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') candidate_list = [] candidates_to_display = [] google_civic_election_id = 0 try: candidate_list_object = CandidateCampaignList() results = candidate_list_object.retrieve_all_candidates_for_office(office_id, office_we_vote_id) success = results['success'] status = results['status'] candidate_list = results['candidate_list'] except Exception as e: status = 'FAILED candidates_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: # Reset office_we_vote_id and office_id so we are sure that it matches what we pull from the database office_id = 0 office_we_vote_id = '' for candidate in candidate_list: one_candidate = { 'candidate_id': candidate.id, 'candidate_we_vote_id': candidate.we_vote_id, 'candidate_display_name': candidate.candidate_name, 'candidate_photo_url': candidate.fetch_photo_url(), 'order_on_ballot': candidate.order_on_ballot, } candidates_to_display.append(one_candidate.copy()) # Capture the office_we_vote_id and google_civic_election_id so we can return if not positive_value_exists(office_id) and candidate.contest_office_id: office_id = candidate.contest_office_id if not positive_value_exists(office_we_vote_id) and candidate.contest_office_we_vote_id: office_we_vote_id = candidate.contest_office_we_vote_id if not positive_value_exists(google_civic_election_id) and candidate.google_civic_election_id: google_civic_election_id = candidate.google_civic_election_id if len(candidates_to_display): status = 'CANDIDATES_RETRIEVED' else: status = 'NO_CANDIDATES_RETRIEVED' json_data = { 'status': status, 'success': True, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'google_civic_election_id': google_civic_election_id, 'candidate_list': candidates_to_display, } else: json_data = { 'status': status, 'success': False, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'google_civic_election_id': google_civic_election_id, 'candidate_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json')
def voter_ballot_items_retrieve(voter_device_id, google_civic_election_id): # Get voter_id from the voter_device_id so we can figure out which ballot_items to offer results = is_voter_device_id_valid(voter_device_id) if not results['success']: json_data = { 'status': 'VALID_VOTER_DEVICE_ID_MISSING', 'success': False, 'voter_id': 0, 'voter_device_id': voter_device_id, 'ballot_item_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') voter_id = fetch_voter_id_from_voter_device_link(voter_device_id) if not positive_value_exists(voter_id): json_data = { 'status': "VALID_VOTER_ID_MISSING", 'success': False, 'voter_id': voter_id, 'voter_device_id': voter_device_id, 'ballot_item_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') # If the google_civic_election_id was found cached in a cookie and passed in, use that # If not, fetch it for this voter if not positive_value_exists(google_civic_election_id): google_civic_election_id = fetch_google_civic_election_id_for_voter_id(voter_id) if not positive_value_exists(google_civic_election_id): # We need to reach out to Google Civic to get this voter's ballot results = retrieve_and_store_ballot_for_voter(voter_id) # We come back from retrieving the ballot with a google_civic_election_id and the data stored in the BallotItem # table google_civic_election_id = results['google_civic_election_id'] if not positive_value_exists(google_civic_election_id): # At this point if we don't have a google_civic_election_id, then we don't have an upcoming election status = 'MISSING_GOOGLE_CIVIC_ELECTION_ID' json_data = { 'status': status, 'success': False, 'voter_id': voter_id, 'voter_device_id': voter_device_id, 'ballot_item_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') ballot_item_list = [] ballot_items_to_display = [] try: ballot_item_list_object = BallotItemList() results = ballot_item_list_object.retrieve_all_ballot_items_for_voter(voter_id, google_civic_election_id) success = results['success'] status = results['status'] ballot_item_list = results['ballot_item_list'] except Exception as e: status = 'FAILED voter_ballot_items_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for ballot_item in ballot_item_list: one_ballot_item = { 'ballot_item_label': ballot_item.ballot_item_label, 'voter_id': ballot_item.voter_id, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'contest_office_id': ballot_item.contest_office_id, 'contest_office_we_vote_id': ballot_item.contest_office_we_vote_id, 'contest_measure_id': ballot_item.contest_measure_id, 'contest_measure_we_vote_id': ballot_item.contest_measure_we_vote_id, } ballot_items_to_display.append(one_ballot_item.copy()) json_data = { 'status': 'VOTER_BALLOT_ITEMS_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'voter_id': voter_id, 'ballot_item_list': ballot_items_to_display, } else: json_data = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'voter_id': voter_id, 'ballot_item_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json')
def candidates_retrieve_for_api(office_id, office_we_vote_id): """ Used by the api :param office_id: :param office_we_vote_id: :return: """ # NOTE: Candidates retrieve is independent of *who* wants to see the data. Candidates retrieve never triggers # a ballot data lookup from Google Civic, like voterBallotItems does if not positive_value_exists(office_id) and not positive_value_exists( office_we_vote_id): status = 'VALID_OFFICE_ID_AND_OFFICE_WE_VOTE_ID_MISSING' json_data = { 'status': status, 'success': False, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'google_civic_election_id': 0, 'candidate_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json') candidate_list = [] candidates_to_display = [] google_civic_election_id = 0 try: candidate_list_object = CandidateCampaignListManager() results = candidate_list_object.retrieve_all_candidates_for_office( office_id, office_we_vote_id) success = results['success'] status = results['status'] candidate_list = results['candidate_list'] except Exception as e: status = 'FAILED candidates_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: # Reset office_we_vote_id and office_id so we are sure that it matches what we pull from the database office_id = 0 office_we_vote_id = '' for candidate in candidate_list: one_candidate = { 'id': candidate.id, 'we_vote_id': candidate.we_vote_id, 'ballot_item_display_name': candidate.display_candidate_name(), 'candidate_photo_url': candidate.candidate_photo_url(), 'party': candidate.political_party_display(), 'order_on_ballot': candidate.order_on_ballot, 'kind_of_ballot_item': CANDIDATE, } candidates_to_display.append(one_candidate.copy()) # Capture the office_we_vote_id and google_civic_election_id so we can return if not positive_value_exists( office_id) and candidate.contest_office_id: office_id = candidate.contest_office_id if not positive_value_exists( office_we_vote_id) and candidate.contest_office_we_vote_id: office_we_vote_id = candidate.contest_office_we_vote_id if not positive_value_exists( google_civic_election_id ) and candidate.google_civic_election_id: google_civic_election_id = candidate.google_civic_election_id if len(candidates_to_display): status = 'CANDIDATES_RETRIEVED' else: status = 'NO_CANDIDATES_RETRIEVED' json_data = { 'status': status, 'success': True, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'google_civic_election_id': google_civic_election_id, 'candidate_list': candidates_to_display, } else: json_data = { 'status': status, 'success': False, 'office_id': office_id, 'office_we_vote_id': office_we_vote_id, 'google_civic_election_id': google_civic_election_id, 'candidate_list': [], } return HttpResponse(json.dumps(json_data), content_type='application/json')
def ballot_item_options_retrieve_for_api(google_civic_election_id=0): """ This function returns a normalized list of candidates and measures so we can pre-populate form fields :param google_civic_election_id: :return: """ status = "" try: candidate_list_object = CandidateCampaignList() results = candidate_list_object.retrieve_all_candidates_for_upcoming_election(google_civic_election_id) candidate_success = results["success"] status += results["status"] candidate_list = results["candidate_list_light"] except Exception as e: status += "FAILED ballot_item_options_retrieve_for_api, candidate_list. " "{error} [type: {error_type}]".format( error=e, error_type=type(e) ) handle_exception(e, logger=logger, exception_message=status) candidate_list = [] candidate_success = False try: office_list_object = ContestOfficeList() results = office_list_object.retrieve_all_offices_for_upcoming_election(google_civic_election_id) office_success = results["success"] status += " " + results["status"] office_list = results["office_list_light"] except Exception as e: status += "FAILED ballot_item_options_retrieve_for_api, office_list. " "{error} [type: {error_type}]".format( error=e, error_type=type(e) ) handle_exception(e, logger=logger, exception_message=status) office_list = [] office_success = False try: measure_list_object = ContestMeasureList() results = measure_list_object.retrieve_all_measures_for_upcoming_election(google_civic_election_id) measure_success = results["success"] status += " " + results["status"] measure_list = results["measure_list_light"] except Exception as e: status += "FAILED ballot_item_options_retrieve_for_api, measure_list. " "{error} [type: {error_type}]".format( error=e, error_type=type(e) ) handle_exception(e, logger=logger, exception_message=status) measure_list = [] measure_success = False ballot_items_to_display = [] if candidate_success and len(candidate_list): for candidate in candidate_list: ballot_items_to_display.append(candidate.copy()) if office_success and len(office_list): for office in office_list: ballot_items_to_display.append(office.copy()) if measure_success and len(measure_list): for measure in measure_list: ballot_items_to_display.append(measure.copy()) json_data = { "status": status, "success": candidate_success or measure_success, "ballot_item_list": ballot_items_to_display, "google_civic_election_id": google_civic_election_id, } results = { "status": status, "success": candidate_success or measure_success, "google_civic_election_id": google_civic_election_id, # We want to save google_civic_election_id in cookie "json_data": json_data, } return results
def retrieve_possible_duplicate_measures(self, measure_title, google_civic_election_id, measure_url, maplight_id, we_vote_id_from_master=''): measure_list_objects = [] filters = [] measure_list_found = False try: measure_queryset = ContestMeasure.objects.all() measure_queryset = measure_queryset.filter(google_civic_election_id=google_civic_election_id) # We don't look for office_we_vote_id because of the chance that locally we are using a # different we_vote_id # measure_queryset = measure_queryset.filter(contest_office_we_vote_id__iexact=office_we_vote_id) # Ignore entries with we_vote_id coming in from master server if positive_value_exists(we_vote_id_from_master): measure_queryset = measure_queryset.filter(~Q(we_vote_id__iexact=we_vote_id_from_master)) # We want to find candidates with *any* of these values if positive_value_exists(measure_title): new_filter = Q(measure_title__iexact=measure_title) filters.append(new_filter) if positive_value_exists(measure_url): new_filter = Q(measure_url__iexact=measure_url) filters.append(new_filter) if positive_value_exists(maplight_id): new_filter = Q(maplight_id=maplight_id) filters.append(new_filter) # Add the first query if len(filters): final_filters = filters.pop() # ...and "OR" the remaining items in the list for item in filters: final_filters |= item measure_queryset = measure_queryset.filter(final_filters) measure_list_objects = measure_queryset if len(measure_list_objects): measure_list_found = True status = 'DUPLICATE_MEASURES_RETRIEVED' success = True else: status = 'NO_DUPLICATE_MEASURES_RETRIEVED' success = True except ContestMeasure.DoesNotExist: # No candidates found. Not a problem. status = 'NO_DUPLICATE_MEASURES_FOUND_DoesNotExist' measure_list_objects = [] success = True except Exception as e: handle_exception(e, logger=logger) status = 'FAILED retrieve_possible_duplicate_measures ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) success = False results = { 'success': success, 'status': status, 'google_civic_election_id': google_civic_election_id, 'measure_list_found': measure_list_found, 'measure_list': measure_list_objects, } return results
def transfer_theunitedstatesio_cached_data_to_wevote_tables(): """ In this method, we take the cached theunitedstatesio data and move it into the core We Vote data :return: """ print "Running transfer_theunitedstatesio_cached_data_to_wevote_tables()" legislators_current_query = TheUnitedStatesIoLegislatorCurrent.objects.all( ) # Only retrieve entries that haven't been processed yet # legislators_current_query = legislators_current_query.filter(was_processed=False) for legislator_current_entry in legislators_current_query: print 'Transferring: ' + str(legislator_current_entry.id) + ':' \ + legislator_current_entry.first_name + ' ' + legislator_current_entry.last_name politician_entry_found = False ######################### # Search the Politician's table to see if we already have an entry for this person # Do we have a record of this politician based on id_bioguide? if legislator_current_entry.bioguide_id != "": try: # Try to find earlier version based on the bioguide identifier query1 = Politician.objects.all() query1 = query1.filter( id_bioguide__exact=legislator_current_entry.bioguide_id) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] politician_entry_found = True except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on bioguide id # ...so check to see if we have a record of this legislator based on govtrack id? if legislator_current_entry.govtrack_id != "": try: query1 = Politician.objects.all() query1 = query1.filter( id_govtrack__exact=legislator_current_entry.govtrack_id ) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] print "FOUND" politician_entry_found = True else: print "NOT FOUND" except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on id_govtrack # ...so check to see if we have a record of this legislator based on full_name_google_civic if legislator_current_entry.first_name and legislator_current_entry.last_name: try: full_name_assembled_guess = legislator_current_entry.first_name + " " + legislator_current_entry.last_name print "Searching for existing full_name_google_civic: {full_name_assembled}".format( full_name_assembled=full_name_assembled_guess) query1 = Politician.objects.all() query1 = query1.filter( full_name_google_civic=full_name_assembled_guess) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] print "FOUND" politician_entry_found = True else: print "NOT FOUND" except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found based on full_name_google_civic # ...so check to see if we have a record of this legislator based on full_name_assembled if legislator_current_entry.first_name and legislator_current_entry.last_name: try: full_name_assembled_guess = legislator_current_entry.first_name + " " + legislator_current_entry.last_name print "Searching for existing full_name_assembled: {full_name_assembled}".format( full_name_assembled=full_name_assembled_guess) query1 = Politician.objects.all() query1 = query1.filter( full_name_assembled=full_name_assembled_guess) # Was at least one existing entry found based on the above criteria? if len(query1): politician_entry = query1[0] print "FOUND" politician_entry_found = True else: print "NOT FOUND" except Exception as e: handle_record_not_found_exception(e) if not politician_entry_found: # TheUnitedStatesIoLegislatorCurrent was not found # ...so create a new entry politician_entry = Politician( last_name=legislator_current_entry.last_name, first_name=legislator_current_entry.first_name, full_name_assembled=legislator_current_entry.first_name + " " + legislator_current_entry.last_name, ) politician_entry.last_name = legislator_current_entry.last_name politician_entry.first_name = legislator_current_entry.first_name politician_entry.full_name_assembled = \ legislator_current_entry.first_name+" "+legislator_current_entry.last_name politician_entry.birth_date = legislator_current_entry.birthday politician_entry.gender = legislator_current_entry.gender politician_entry.id_bioguide = legislator_current_entry.bioguide_id politician_entry.id_thomas = legislator_current_entry.thomas_id politician_entry.id_opensecrets = legislator_current_entry.opensecrets_id politician_entry.id_lis = legislator_current_entry.lis_id politician_entry.id_cspan = legislator_current_entry.cspan_id politician_entry.id_govtrack = legislator_current_entry.govtrack_id politician_entry.id_votesmart = legislator_current_entry.votesmart_id politician_entry.id_ballotpedia = legislator_current_entry.ballotpedia_id politician_entry.id_washington_post = legislator_current_entry.washington_post_id politician_entry.id_icpsr = legislator_current_entry.icpsr_id politician_entry.id_wikipedia = legislator_current_entry.wikipedia_id # OTHER FIELDS # "type", # row[4] politician_entry.state_code = legislator_current_entry.state # "state", # row[5] # "district", # row[6] politician_entry.party = legislator_current_entry.party # "party", # row[7] # "url", # row[8] # "address", # row[9] # "phone", # row[10] # "contact_form", # row[11] # "rss_url", # row[12] # "twitter", # row[13] # "facebook", # row[14] # "facebook_id", # row[15] # "youtube", # row[16] # "youtube_id", # row[17] # We use "try/exception" so we know when entry isn't saved due to unique requirement # This works! Bigger question -- how to deal with exceptions in general? try: politician_entry.save() # Mark the source entry as was_processed so we don't try to import the same data again # legislator_current_entry.save() except Exception as e: handle_exception(e)
def voter_ballot_items_retrieve_for_one_election_for_api(voter_device_id, voter_id, google_civic_election_id): """ :param voter_device_id: :param google_civic_election_id: This variable either was stored in a cookie, or passed in explicitly so we can get the ballot items related to that election. :return: """ ballot_item_list_manager = BallotItemListManager() ballot_item_list = [] ballot_items_to_display = [] try: results = ballot_item_list_manager.retrieve_all_ballot_items_for_voter(voter_id, google_civic_election_id) success = results['success'] status = results['status'] ballot_item_list = results['ballot_item_list'] except Exception as e: status = 'FAILED voter_ballot_items_retrieve. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) success = False if success: for ballot_item in ballot_item_list: if ballot_item.contest_office_we_vote_id: kind_of_ballot_item = OFFICE ballot_item_id = ballot_item.contest_office_id we_vote_id = ballot_item.contest_office_we_vote_id try: candidate_list_object = CandidateCampaignList() results = candidate_list_object.retrieve_all_candidates_for_office(ballot_item_id, we_vote_id) candidates_to_display = [] if results['candidate_list_found']: candidate_list = results['candidate_list'] for candidate in candidate_list: # This should match values returned in candidates_retrieve_for_api one_candidate = { 'id': candidate.id, 'we_vote_id': candidate.we_vote_id, 'ballot_item_display_name': candidate.candidate_name, 'candidate_photo_url': candidate.candidate_photo_url(), 'party': candidate.party_display(), 'order_on_ballot': candidate.order_on_ballot, 'kind_of_ballot_item': CANDIDATE, 'twitter_description': candidate.twitter_description, 'twitter_followers_count': candidate.twitter_followers_count, } candidates_to_display.append(one_candidate.copy()) except Exception as e: # status = 'FAILED candidates_retrieve. ' \ # '{error} [type: {error_type}]'.format(error=e.message, error_type=type(e)) candidates_to_display = [] one_ballot_item = { 'ballot_item_display_name': ballot_item.ballot_item_display_name, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'kind_of_ballot_item': kind_of_ballot_item, 'id': ballot_item_id, 'we_vote_id': we_vote_id, 'candidate_list': candidates_to_display, } ballot_items_to_display.append(one_ballot_item.copy()) elif ballot_item.contest_measure_we_vote_id: kind_of_ballot_item = MEASURE ballot_item_id = ballot_item.contest_measure_id we_vote_id = ballot_item.contest_measure_we_vote_id one_ballot_item = { 'ballot_item_display_name': ballot_item.ballot_item_display_name, 'google_civic_election_id': ballot_item.google_civic_election_id, 'google_ballot_placement': ballot_item.google_ballot_placement, 'local_ballot_order': ballot_item.local_ballot_order, 'measure_subtitle': ballot_item.measure_subtitle, 'kind_of_ballot_item': kind_of_ballot_item, 'id': ballot_item_id, 'we_vote_id': we_vote_id, } ballot_items_to_display.append(one_ballot_item.copy()) results = { 'status': 'VOTER_BALLOT_ITEMS_RETRIEVED', 'success': True, 'voter_device_id': voter_device_id, 'ballot_item_list': ballot_items_to_display, 'google_civic_election_id': google_civic_election_id, } else: results = { 'status': status, 'success': False, 'voter_device_id': voter_device_id, 'ballot_item_list': [], 'google_civic_election_id': google_civic_election_id, } return results
def ballot_item_options_retrieve_for_api(google_civic_election_id=0): """ This function returns a normalized list of candidates and measures so we can pre-populate form fields. Not specific to one voter. :param google_civic_election_id: :return: """ status = "" try: candidate_list_object = CandidateCampaignList() results = candidate_list_object.retrieve_all_candidates_for_upcoming_election(google_civic_election_id) candidate_success = results['success'] status += results['status'] candidate_list = results['candidate_list_light'] except Exception as e: status += 'FAILED ballot_item_options_retrieve_for_api, candidate_list. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) candidate_list = [] candidate_success = False try: office_list_object = ContestOfficeList() results = office_list_object.retrieve_all_offices_for_upcoming_election(google_civic_election_id) office_success = results['success'] status += ' ' + results['status'] office_list = results['office_list_light'] except Exception as e: status += 'FAILED ballot_item_options_retrieve_for_api, office_list. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) office_list = [] office_success = False try: measure_list_object = ContestMeasureList() results = measure_list_object.retrieve_all_measures_for_upcoming_election(google_civic_election_id) measure_success = results['success'] status += ' ' + results['status'] measure_list = results['measure_list_light'] except Exception as e: status += 'FAILED ballot_item_options_retrieve_for_api, measure_list. ' \ '{error} [type: {error_type}]'.format(error=e, error_type=type(e)) handle_exception(e, logger=logger, exception_message=status) measure_list = [] measure_success = False ballot_items_to_display = [] if candidate_success and len(candidate_list): for candidate in candidate_list: ballot_items_to_display.append(candidate.copy()) if office_success and len(office_list): for office in office_list: ballot_items_to_display.append(office.copy()) if measure_success and len(measure_list): for measure in measure_list: ballot_items_to_display.append(measure.copy()) json_data = { 'status': status, 'success': candidate_success or measure_success, 'ballot_item_list': ballot_items_to_display, 'google_civic_election_id': google_civic_election_id, } results = { 'status': status, 'success': candidate_success or measure_success, 'google_civic_election_id': google_civic_election_id, # We want to save google_civic_election_id in cookie 'json_data': json_data, } return results
def organization_search_find_any_possibilities( self, organization_name, organization_twitter_handle, organization_website, organization_email ): """ We want to find *any* possible organization that includes any of the search terms :param organization_name: :param organization_twitter_handle: :param organization_website: :param organization_email: :return: """ organization_list_for_json = {} try: filters = [] organization_list_for_json = [] organization_objects_list = [] if positive_value_exists(organization_name): new_filter = Q(organization_name__icontains=organization_name) filters.append(new_filter) if positive_value_exists(organization_twitter_handle): new_filter = Q(organization_twitter_handle__icontains=organization_twitter_handle) filters.append(new_filter) if positive_value_exists(organization_website): new_filter = Q(organization_website__icontains=organization_website) filters.append(new_filter) if positive_value_exists(organization_email): new_filter = Q(organization_email__icontains=organization_email) filters.append(new_filter) # Add the first query if len(filters): final_filters = filters.pop() # ...and "OR" the remaining items in the list for item in filters: final_filters |= item organization_objects_list = Organization.objects.filter(final_filters) if len(organization_objects_list): organizations_found = True status = "ORGANIZATIONS_RETRIEVED" for organization in organization_objects_list: one_organization_json = { "organization_id": organization.id, "organization_we_vote_id": organization.we_vote_id, "organization_name": organization.organization_name if positive_value_exists(organization.organization_name) else "", "organization_website": organization.organization_website if positive_value_exists(organization.organization_website) else "", "organization_twitter_handle": organization.organization_twitter_handle if positive_value_exists(organization.organization_twitter_handle) else "", "organization_email": organization.organization_email if positive_value_exists(organization.organization_email) else "", "organization_facebook": organization.organization_facebook if positive_value_exists(organization.organization_facebook) else "", } organization_list_for_json.append(one_organization_json) else: organizations_found = False status = "NO_ORGANIZATIONS_RETRIEVED" success = True except Organization.DoesNotExist: # No organizations found. Not a problem. organizations_found = False status = "NO_ORGANIZATIONS_FOUND_DoesNotExist" success = True # We are still successful if no organizations are found except Exception as e: organizations_found = False handle_exception(e, logger=logger) status = "FAILED organization_search_find_any_possibilities " "{error} [type: {error_type}]".format( error=e.message, error_type=type(e) ) success = False results = { "status": status, "success": success, "organizations_found": organizations_found, "organizations_list": organization_list_for_json, } return results