Ejemplo n.º 1
0
 def get_candidate_state(self):
     if self.state_code:
         return self.state_code
     else:
         # Pull this from ocdDivisionId
         ocd_division_id = self.ocd_division_id
         return extract_state_from_ocd_division_id(ocd_division_id)
 def get_office_state(self):
     if positive_value_exists(self.state_code):
         return self.state_code
     else:
         # Pull this from ocdDivisionId
         if positive_value_exists(self.ocd_division_id):
             ocd_division_id = self.ocd_division_id
             return extract_state_from_ocd_division_id(ocd_division_id)
         else:
             return ''
Ejemplo n.º 3
0
 def get_candidate_state(self):
     if positive_value_exists(self.state_code):
         return self.state_code
     else:
         # Pull this from ocdDivisionId
         if positive_value_exists(self.ocd_division_id):
             ocd_division_id = self.ocd_division_id
             return extract_state_from_ocd_division_id(ocd_division_id)
         else:
             return ''
Ejemplo n.º 4
0
 def get_measure_state(self):
     if positive_value_exists(self.state_code):
         return self.state_code
     # Pull this from ocdDivisionId
     ocd_division_id = self.ocd_division_id
     return extract_state_from_ocd_division_id(ocd_division_id)
Ejemplo n.º 5
0
def store_one_ballot_from_google_civic_api(one_ballot_json, voter_id=0, polling_location_we_vote_id=''):
    """
    When we pass in a voter_id, we want to save this ballot related to the voter.
    When we pass in polling_location_we_vote_id, we want to save a ballot for that area, which is useful for
    getting new voters started by showing them a ballot roughly near them.
    """
    #     "election": {
    #     "electionDay": "2015-11-03",
    #     "id": "4162",
    #     "name": "Virginia General Election",
    #     "ocdDivisionId": "ocd-division/country:us/state:va"
    # },
    if 'election' not in one_ballot_json:
        results = {
            'status': 'BALLOT_JSON_MISSING_ELECTION',
            'success': False,
            'google_civic_election_id': 0,
        }
        return results

    election_date_text = ''
    election_description_text = ''
    if 'electionDay' in one_ballot_json['election']:
        election_date_text = one_ballot_json['election']['electionDay']
    if 'name' in one_ballot_json['election']:
        election_description_text = one_ballot_json['election']['name']

    if 'id' not in one_ballot_json['election']:
        results = {
            'status': 'BALLOT_JSON_MISSING_ELECTION_ID',
            'success': False,
            'google_civic_election_id': 0,
        }
        return results

    voter_address_dict = one_ballot_json['normalizedInput'] if 'normalizedInput' in one_ballot_json else {}
    if positive_value_exists(voter_id):
        if positive_value_exists(voter_address_dict):
            # When saving a ballot for an individual voter, use this data to update voter address with the
            #  normalized address information returned from Google Civic
            # "normalizedInput": {
            #   "line1": "254 hartford st",
            #   "city": "san francisco",
            #   "state": "CA",
            #   "zip": "94114"
            #  },
            voter_address_manager = VoterAddressManager()
            voter_address_manager.update_voter_address_with_normalized_values(
                voter_id, voter_address_dict)
            # Note that neither 'success' nor 'status' are set here because updating the voter_address with normalized
            # values isn't critical to the success of storing the ballot for a voter
    # We don't store the normalized address information when we capture a ballot for a polling location

    google_civic_election_id = one_ballot_json['election']['id']
    ocd_division_id = one_ballot_json['election']['ocdDivisionId']
    state_code = extract_state_from_ocd_division_id(ocd_division_id)
    if not positive_value_exists(state_code):
        # We have a backup method of looking up state from one_ballot_json['state']['name']
        # in case the ocd state fails
        state_name = ''
        if 'state' in one_ballot_json:
            if 'name' in one_ballot_json['state']:
                state_name = one_ballot_json['state']['name']
            elif len(one_ballot_json['state']) > 0:
                # In some cases, like test elections 2000 a list is returned in one_ballot_json['state']
                for one_state_entry in one_ballot_json['state']:
                    if 'name' in one_state_entry:
                        state_name = one_state_entry['name']
        state_code = convert_state_text_to_state_code(state_name)

    # Loop through all contests and store in local db cache
    if 'contests' in one_ballot_json:
        results = process_contests_from_structured_json(one_ballot_json['contests'], google_civic_election_id,
                                                        ocd_division_id, state_code, voter_id,
                                                        polling_location_we_vote_id)

        status = results['status']
        success = results['success']
    else:
        status = "STORE_ONE_BALLOT_NO_CONTESTS_FOUND"
        success = False

    # When saving a ballot for individual voter, loop through all pollingLocations and store in local db
    # process_polling_locations_from_structured_json(one_ballot_json['pollingLocations'])

    # If we successfully save a ballot, create/update a BallotReturned entry
    is_test_election = True if positive_value_exists(google_civic_election_id) \
        and convert_to_int(google_civic_election_id) == 2000 else False
    if success and positive_value_exists(voter_address_dict) and not is_test_election:
        ballot_returned_manager = BallotReturnedManager()
        if positive_value_exists(voter_id) and positive_value_exists(google_civic_election_id):
            results = ballot_returned_manager.retrieve_ballot_returned_from_voter_id(voter_id, google_civic_election_id)
            if results['ballot_returned_found']:
                update_results = ballot_returned_manager.update_ballot_returned_with_normalized_values(
                    voter_address_dict, results['ballot_returned'])
            else:
                create_results = ballot_returned_manager.create_ballot_returned_with_normalized_values(
                    voter_address_dict,
                    election_date_text, election_description_text,
                    google_civic_election_id, voter_id, '')
        if positive_value_exists(polling_location_we_vote_id) and positive_value_exists(google_civic_election_id):
            results = ballot_returned_manager.retrieve_ballot_returned_from_polling_location_we_vote_id(
                polling_location_we_vote_id, google_civic_election_id)
            if results['ballot_returned_found']:
                update_results = ballot_returned_manager.update_ballot_returned_with_normalized_values(
                    voter_address_dict, results['ballot_returned'])
            else:
                create_results = ballot_returned_manager.create_ballot_returned_with_normalized_values(
                    voter_address_dict,
                    election_date_text, election_description_text,
                    google_civic_election_id, 0, polling_location_we_vote_id)
        # Currently we don't report the success or failure of storing ballot_returned

    results = {
        'status': status,
        'success': success,
        'google_civic_election_id': google_civic_election_id,
    }
    return results
Ejemplo n.º 6
0
 def get_election_state(self):
     # Pull this from ocdDivisionId
     ocd_division_id = self.ocd_division_id
     return extract_state_from_ocd_division_id(ocd_division_id)
Ejemplo n.º 7
0
def store_one_ballot_from_google_civic_api(one_ballot_json,
                                           voter_id=0,
                                           polling_location_we_vote_id=''):
    """
    When we pass in a voter_id, we want to save this ballot related to the voter.
    When we pass in polling_location_we_vote_id, we want to save a ballot for that area, which is useful for
    getting new voters started by showing them a ballot roughly near them.
    """
    #     "election": {
    #     "electionDay": "2015-11-03",
    #     "id": "4162",
    #     "name": "Virginia General Election",
    #     "ocdDivisionId": "ocd-division/country:us/state:va"
    # },
    if 'election' not in one_ballot_json:
        results = {
            'status': 'BALLOT_JSON_MISSING_ELECTION',
            'success': False,
            'google_civic_election_id': 0,
        }
        return results

    election_date_text = ''
    election_description_text = ''
    if 'electionDay' in one_ballot_json['election']:
        election_date_text = one_ballot_json['election']['electionDay']
    if 'name' in one_ballot_json['election']:
        election_description_text = one_ballot_json['election']['name']

    if 'id' not in one_ballot_json['election']:
        results = {
            'status': 'BALLOT_JSON_MISSING_ELECTION_ID',
            'success': False,
            'google_civic_election_id': 0,
        }
        return results

    voter_address_dict = one_ballot_json[
        'normalizedInput'] if 'normalizedInput' in one_ballot_json else {}
    if positive_value_exists(voter_id):
        if positive_value_exists(voter_address_dict):
            # When saving a ballot for an individual voter, use this data to update voter address with the
            #  normalized address information returned from Google Civic
            # "normalizedInput": {
            #   "line1": "254 hartford st",
            #   "city": "san francisco",
            #   "state": "CA",
            #   "zip": "94114"
            #  },
            voter_address_manager = VoterAddressManager()
            voter_address_manager.update_voter_address_with_normalized_values(
                voter_id, voter_address_dict)
            # Note that neither 'success' nor 'status' are set here because updating the voter_address with normalized
            # values isn't critical to the success of storing the ballot for a voter
    # We don't store the normalized address information when we capture a ballot for a polling location

    google_civic_election_id = one_ballot_json['election']['id']
    ocd_division_id = one_ballot_json['election']['ocdDivisionId']
    state_code = extract_state_from_ocd_division_id(ocd_division_id)
    if not positive_value_exists(state_code):
        # We have a backup method of looking up state from one_ballot_json['state']['name']
        # in case the ocd state fails
        state_name = ''
        if 'state' in one_ballot_json:
            if 'name' in one_ballot_json['state']:
                state_name = one_ballot_json['state']['name']
            elif len(one_ballot_json['state']) > 0:
                # In some cases, like test elections 2000 a list is returned in one_ballot_json['state']
                for one_state_entry in one_ballot_json['state']:
                    if 'name' in one_state_entry:
                        state_name = one_state_entry['name']
        state_code = convert_state_text_to_state_code(state_name)

    # Loop through all contests and store in local db cache
    if 'contests' in one_ballot_json:
        results = process_contests_from_structured_json(
            one_ballot_json['contests'], google_civic_election_id,
            ocd_division_id, state_code, voter_id, polling_location_we_vote_id)

        status = results['status']
        success = results['success']
    else:
        status = "STORE_ONE_BALLOT_NO_CONTESTS_FOUND"
        success = False

    # When saving a ballot for individual voter, loop through all pollingLocations and store in local db
    # process_polling_locations_from_structured_json(one_ballot_json['pollingLocations'])

    # If we successfully save a ballot, create/update a BallotReturned entry
    is_test_election = True if positive_value_exists(google_civic_election_id) \
        and convert_to_int(google_civic_election_id) == 2000 else False
    if success and positive_value_exists(
            voter_address_dict) and not is_test_election:
        ballot_returned_manager = BallotReturnedManager()
        if positive_value_exists(voter_id) and positive_value_exists(
                google_civic_election_id):
            results = ballot_returned_manager.retrieve_ballot_returned_from_voter_id(
                voter_id, google_civic_election_id)
            if results['ballot_returned_found']:
                update_results = ballot_returned_manager.update_ballot_returned_with_normalized_values(
                    voter_address_dict, results['ballot_returned'])
            else:
                create_results = ballot_returned_manager.create_ballot_returned_with_normalized_values(
                    voter_address_dict, election_date_text,
                    election_description_text, google_civic_election_id,
                    voter_id, '')
        if positive_value_exists(
                polling_location_we_vote_id) and positive_value_exists(
                    google_civic_election_id):
            results = ballot_returned_manager.retrieve_ballot_returned_from_polling_location_we_vote_id(
                polling_location_we_vote_id, google_civic_election_id)
            if results['ballot_returned_found']:
                update_results = ballot_returned_manager.update_ballot_returned_with_normalized_values(
                    voter_address_dict, results['ballot_returned'])
            else:
                create_results = ballot_returned_manager.create_ballot_returned_with_normalized_values(
                    voter_address_dict, election_date_text,
                    election_description_text, google_civic_election_id, 0,
                    polling_location_we_vote_id)
        # Currently we don't report the success or failure of storing ballot_returned

    results = {
        'status': status,
        'success': success,
        'google_civic_election_id': google_civic_election_id,
    }
    return results
Ejemplo n.º 8
0
def store_one_ballot_from_google_civic_api(one_ballot_json, voter_id=0):

    #     "election": {
    #     "electionDay": "2015-11-03",
    #     "id": "4162",
    #     "name": "Virginia General Election",
    #     "ocdDivisionId": "ocd-division/country:us/state:va"
    # },
    if 'election' not in one_ballot_json:
        results = {
            'status': 'BALLOT_JSON_MISSING_ELECTION',
            'success': False,
            'google_civic_election_id': 0,
        }
        return results

    if 'id' not in one_ballot_json['election']:
        results = {
            'status': 'BALLOT_JSON_MISSING_ELECTION_ID',
            'success': False,
            'google_civic_election_id': 0,
        }
        return results

    if positive_value_exists(voter_id):
        voter_address_dict = one_ballot_json['normalizedInput'] if 'normalizedInput' in one_ballot_json else {}
        if positive_value_exists(voter_address_dict):
            # When saving a ballot for an individual voter, use this data to update voter address with the
            #  normalized address information returned from Google Civic
            # "normalizedInput": {
            #   "line1": "254 hartford st",
            #   "city": "san francisco",
            #   "state": "CA",
            #   "zip": "94114"
            #  },
            voter_address_manager = VoterAddressManager()
            voter_address_manager.update_voter_address_with_normalized_values(
                voter_id, voter_address_dict)
            # Note that neither 'success' nor 'status' are set here because updating the voter_address with normalized
            # values isn't critical to the success of storing the ballot for a voter

    google_civic_election_id = one_ballot_json['election']['id']
    ocd_division_id = one_ballot_json['election']['ocdDivisionId']
    state_code = extract_state_from_ocd_division_id(ocd_division_id)
    if not positive_value_exists(state_code):
        # We have a backup method of looking up state from one_ballot_json['state']['name']
        # in case the ocd state fails
        state_name = ''
        if 'state' in one_ballot_json:
            if 'name' in one_ballot_json['state']:
                state_name = one_ballot_json['state']['name']
            elif len(one_ballot_json['state']) > 0:
                # In some cases, like test elections 2000 a list is returned in one_ballot_json['state']
                for one_state_entry in one_ballot_json['state']:
                    if 'name' in one_state_entry:
                        state_name = one_state_entry['name']
        state_code = convert_state_text_to_state_code(state_name)

    # Loop through all contests and store in local db cache
    if 'contests' in one_ballot_json:
        results = process_contests_from_structured_json(one_ballot_json['contests'], google_civic_election_id,
                                                        ocd_division_id, state_code, voter_id)

        status = results['status']
        success = results['success']
    else:
        status = "STORE_ONE_BALLOT_NO_CONTESTS_FOUND"
        success = False

    # When saving a ballot for individual voter, loop through all pollingLocations and store in local db
    # process_polling_locations_from_structured_json(one_ballot_json['pollingLocations'])

    results = {
        'status': status,
        'success': success,
        'google_civic_election_id': google_civic_election_id,
    }
    return results
 def get_measure_state(self):
     if positive_value_exists(self.state_code):
         return self.state_code
     # Pull this from ocdDivisionId
     ocd_division_id = self.ocd_division_id
     return extract_state_from_ocd_division_id(ocd_division_id)
Ejemplo n.º 10
0
 def get_election_state(self):
     # Pull this from ocdDivisionId
     ocd_division_id = self.ocd_division_id
     return extract_state_from_ocd_division_id(ocd_division_id)