示例#1
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)
示例#2
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
示例#3
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)
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 set here because updating the voter_address with normalized values
            #  isn't critical to the success of storing the ballot for a voter
            # if update_address_results['success']:
            #     status = 'VOTER_ADDRESS_UPDATED_WITH_NORMALIZED_VALUES'
            # else:
            #     status = update_address_results['status']

    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
        if 'state' in one_ballot_json:
            if 'name' in one_ballot_json['state']:
                state_code = one_ballot_json['state']['name']

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

    status = results['status']

    # 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': results['success'],
        'google_civic_election_id': google_civic_election_id,
    }
    return results