Beispiel #1
0
 def setUp(self):
     BallotReturned.objects.create(
         **{
             'google_civic_election_id': 4184,
             'latitude': 34.6604854,
             'longitude': -90.184124,
             'normalized_city': 'coldwater',
             'normalized_line1': '13247 arkabutla rd',
             'normalized_state': 'MS',
             'normalized_zip': '38618',
             'polling_location_we_vote_id': 'wv01ploc43132',
         })
     self.ballot_manager = BallotReturnedManager()
Beispiel #2
0
 def setUp(self):
     BallotReturned.objects.create(**{'google_civic_election_id': 4184,
                                      'latitude': 34.6604854,
                                      'longitude': -90.184124,
                                      'normalized_city': 'coldwater',
                                      'normalized_line1': '13247 arkabutla rd',
                                      'normalized_state': 'MS',
                                      'normalized_zip': '38618',
                                      'polling_location_we_vote_id': 'wv01ploc43132',
                                      })
     self.ballot_manager = BallotReturnedManager()
Beispiel #3
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
Beispiel #4
0
class BallotTestCase(TestCase):
    def setUp(self):
        BallotReturned.objects.create(
            **{
                'google_civic_election_id': 4184,
                'latitude': 34.6604854,
                'longitude': -90.184124,
                'normalized_city': 'coldwater',
                'normalized_line1': '13247 arkabutla rd',
                'normalized_state': 'MS',
                'normalized_zip': '38618',
                'polling_location_we_vote_id': 'wv01ploc43132',
            })
        self.ballot_manager = BallotReturnedManager()

    def test_do_not_return_ballot_in_different_state(self):
        with mock.patch(
                'ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = Location(
                address='1200 Broadway Avenue, Oakland, CA 94720, USA',
                latitude=37.8030442,
                longitude=-122.2739699)

            result = self.ballot_manager.find_closest_ballot_returned(
                'Oakland, CA')
            self.assertEqual(google_client.geocode.call_count, 1)
            self.assertEqual(
                result, {
                    'status': 'No stored ballot matches the state CA.',
                    'ballot_returned_found': False,
                    'ballot_returned': None
                })

    def test_address_not_found(self):
        with mock.patch(
                'ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = None
            result = self.ballot_manager.find_closest_ballot_returned(
                'blah bal blh, OK')
            self.assertEqual(google_client.geocode.call_count, 1)
            self.assertEqual(
                result, {
                    'status':
                    'Could not find location matching "blah bal blh, OK"',
                    'ballot_returned_found': False,
                    'ballot_returned': None
                })

    def test_ballot_found(self):
        ballot_in_ms = BallotReturned.objects.get()
        self.assertEqual(ballot_in_ms.normalized_state, 'MS')
        with mock.patch(
                'ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = Location(
                address='Jackson, MS, USA',
                latitude=32.310251,
                longitude=-90.3289724)

            result = self.ballot_manager.find_closest_ballot_returned(
                'Jackson, MS')
            self.assertEqual(
                result, {
                    'status': 'Ballot returned found.',
                    'ballot_returned_found': True,
                    'ballot_returned': ballot_in_ms
                })

    def test_return_closest_ballot(self):
        """ When several ballots match the queried state, return the closest one. """
        ballot_in_jackson = BallotReturned.objects.create(
            **{
                'google_civic_election_id': 4184,
                'google_civic_election_id': 4184,
                'latitude': 32.269163,
                'longitude': -90.234566,
                'normalized_city': 'jackson',
                'normalized_line1': '1020 w mcdowell rd',
                'normalized_state': 'MS',
                'normalized_zip': '39204',
                'polling_location_we_vote_id': 'wv01ploc42284',
            })
        self.assertEqual(
            BallotReturned.objects.filter(normalized_state='MS').count(), 2)
        with mock.patch(
                'ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = Location(
                address='Jackson, MS, USA',
                latitude=32.310251,
                longitude=-90.3289724)

            result = self.ballot_manager.find_closest_ballot_returned(
                'Jackson, MS')

            self.assertEqual(
                result, {
                    'status': 'Ballot returned found.',
                    'ballot_returned_found': True,
                    'ballot_returned': ballot_in_jackson
                })
Beispiel #5
0
class BallotTestCase(TestCase):

    def setUp(self):
        BallotReturned.objects.create(**{'google_civic_election_id': 4184,
                                         'latitude': 34.6604854,
                                         'longitude': -90.184124,
                                         'normalized_city': 'coldwater',
                                         'normalized_line1': '13247 arkabutla rd',
                                         'normalized_state': 'MS',
                                         'normalized_zip': '38618',
                                         'polling_location_we_vote_id': 'wv01ploc43132',
                                         })
        self.ballot_manager = BallotReturnedManager()

    def test_do_not_return_ballot_in_different_state(self):
        with mock.patch('ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = Location(address='1200 Broadway Avenue, Oakland, CA 94720, USA',
                                                          latitude=37.8030442, longitude=-122.2739699)

            result = self.ballot_manager.find_closest_ballot_returned('Oakland, CA')
            self.assertEqual(google_client.geocode.call_count, 1)
            self.assertEqual(result, {'status': 'No stored ballot matches the state CA.',
                                      'ballot_returned_found': False,
                                      'ballot_returned': None})

    def test_address_not_found(self):
        with mock.patch('ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = None
            result = self.ballot_manager.find_closest_ballot_returned('blah bal blh, OK')
            self.assertEqual(google_client.geocode.call_count, 1)
            self.assertEqual(result, {'status': 'Could not find location matching "blah bal blh, OK"',
                                      'ballot_returned_found': False,
                                      'ballot_returned': None})

    def test_ballot_found(self):
        ballot_in_ms = BallotReturned.objects.get()
        self.assertEqual(ballot_in_ms.normalized_state, 'MS')
        with mock.patch('ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = Location(address='Jackson, MS, USA',
                                                          latitude=32.310251, longitude=-90.3289724)

            result = self.ballot_manager.find_closest_ballot_returned('Jackson, MS')
            self.assertEqual(result, {'status': 'Ballot returned found.',
                                      'ballot_returned_found': True,
                                      'ballot_returned': ballot_in_ms})

    def test_return_closest_ballot(self):
        """ When several ballots match the queried state, return the closest one. """
        ballot_in_jackson = BallotReturned.objects.create(**{'google_civic_election_id': 4184,
                                                             'google_civic_election_id': 4184,
                                                             'latitude': 32.269163,
                                                             'longitude': -90.234566,
                                                             'normalized_city': 'jackson',
                                                             'normalized_line1': '1020 w mcdowell rd',
                                                             'normalized_state': 'MS',
                                                             'normalized_zip': '39204',
                                                             'polling_location_we_vote_id': 'wv01ploc42284',
                                                             })
        self.assertEqual(BallotReturned.objects.filter(normalized_state='MS').count(), 2)
        with mock.patch('ballot.models.get_geocoder_for_service') as mock_geopy:
            google_client = mock_geopy('google')()
            google_client.geocode.return_value = Location(address='Jackson, MS, USA',
                                                          latitude=32.310251, longitude=-90.3289724)

            result = self.ballot_manager.find_closest_ballot_returned('Jackson, MS')

            self.assertEqual(result, {'status': 'Ballot returned found.',
                                      'ballot_returned_found': True,
                                      'ballot_returned': ballot_in_jackson})
Beispiel #6
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