Ejemplo n.º 1
0
def geocode_zip(zipcode):
    """ Get zipcode geocoding information """
    try:
        geocode = GoogleGeocode(zipcode)
        return geocode.google_maps_api()
    except:
        return {'error': 'Error while getting geocoding information for ' + zipcode}
Ejemplo n.º 2
0
    def insert_counselor(self, counselor):
        """ Save a counseling agency to local database """
        if not counselor or counselor == {}:
            return
        self.sanitize_values(counselor)

        obj = CounselingAgency()
        obj.agcid = counselor.get('agcid', '')
        obj.adr1 = counselor.get('adr1', '')
        obj.adr2 = counselor.get('adr2', '')
        obj.city = counselor.get('city', '')
        obj.email = counselor.get('email', '')
        obj.fax = counselor.get('fax', '')
        obj.nme = counselor.get('nme', '')
        obj.phone1 = counselor.get('phone1', '')
        obj.statecd = counselor.get('statecd', '')
        obj.weburl = counselor.get('weburl', '')
        obj.zipcd = counselor.get('zipcd', '')
        obj.agc_ADDR_LATITUDE = counselor.get('agc_ADDR_LATITUDE', '')
        obj.agc_ADDR_LONGITUDE = counselor.get('agc_ADDR_LONGITUDE', '')
        obj.languages = counselor.get('languages', '')
        obj.services = counselor.get('services', '')
        obj.parentid = counselor.get('parentid', '')
        obj.county_NME = counselor.get('county_NME', '')
        obj.phone2 = counselor.get('phone2', '')
        obj.mailingadr1 = counselor.get('mailingadr1', '')
        obj.mailingadr2 = counselor.get('mailingadr2', '')
        obj.mailingcity = counselor.get('mailingcity', '')
        obj.mailingzipcd = counselor.get('mailingzipcd', '')
        obj.mailingstatecd = counselor.get('mailingstatecd', '')
        obj.state_NME = counselor.get('state_NME', '')
        obj.state_FIPS_CODE = counselor.get('state_FIPS_CODE', '')
        obj.faithbased = counselor.get('faithbased', '')
        obj.colonias_IND = counselor.get('colonias_IND', '')
        obj.migrantwkrs_IND = counselor.get('migrantwkrs_IND', '')
        obj.agc_STATUS = counselor.get('agc_STATUS', '')
        obj.agc_SRC_CD = counselor.get('agc_SRC_CD', '')
        obj.counslg_METHOD = counselor.get('counslg_METHOD', '')

        try:
            if obj.agc_ADDR_LATITUDE == '0' or obj.agc_ADDR_LONGITUDE == '0':
                geocode = GoogleGeocode(obj.zipcd[:5])
                geocode_data = geocode.google_maps_api()
                if 'zip' in geocode_data:
                    obj.agc_ADDR_LATITUDE = geocode_data['zip']['lat']
                    obj.agc_ADDR_LONGITUDE = geocode_data['zip']['lng']
                else:
                    raise Exception('Could not obtain geocoding information for zipcode [%s]' % obj.zipcd)
            obj.save()
        except Exception as e:
            self.errors += 'Error while saving agency [%s]: %s\n' % (obj.nme, e)
Ejemplo n.º 3
0
 def setUp(self):
     self.gc = GoogleGeocode(20005)
Ejemplo n.º 4
0
class TestGoogleGeocode(TestCase):
    def setUp(self):
        self.gc = GoogleGeocode(20005)

    def test_init(self):
        """ Testing __init__ """
        self.assertEqual(self.gc.zipcode, 20005)
        self.assertEqual(type(self.gc.invisible_zipcodes), dict)
        self.assertTrue(len(self.gc.invisible_zipcodes) > 0)
        self.assertTrue(self.gc.privateKey is not None)
        self.assertTrue(self.gc.clientID is not None)

    def test_is_usa_or_territory__correct(self):
        """ Testing is_usa_or_territory, GUAM, USVI, ASamoa, PRico, Cnmi, Rmi, Usa """
        self.assertTrue(
            self.gc.is_usa_or_territory("123 Address, GUAM something else"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, UsVi"))
        self.assertTrue(
            self.gc.is_usa_or_territory("123 Address, American Samoa else"))
        self.assertTrue(
            self.gc.is_usa_or_territory(
                "123 Address, Puerto Rico something else"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, Cnmi"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, rmi"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, Usa"))

    def test_is_usa_or_territory__wrong(self):
        """ Testing is_usa_or_territory, Not USA address """
        self.assertFalse(
            self.gc.is_usa_or_territory("123 Address, Mexico City, Mexico"))

    def test_signed_url__correct(self):
        """ Testing signed_url, correct url """
        url = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"
        expected = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA="
        self.gc.privateKey = "vNIXE0xscrmjlyV-12Nj_BvUPaw="
        self.gc.clientID = "clientID"
        signed_url = self.gc.signed_url(url)
        self.assertEqual(signed_url, expected)

    def test_signed_url__empty(self):
        """ Testing signed_url, empty url """
        url = ''
        signed_url = self.gc.signed_url(url)
        self.assertEqual(signed_url, None)

    @patch('urllib2.urlopen')
    def test_request_google_maps(self, mock_urlopen):
        """ Testing request_google_maps """
        def urlopen_check_param(param):
            self.param = param
            mm = MagicMock()
            mm.read.return_value = '{"Success":"success"}'
            return mm

        mock_urlopen.side_effect = urlopen_check_param
        expected = "http://maps.googleapis.com/maps/api/geocode/json?address=20005&sensor=false&client=" + self.gc.clientID
        response = self.gc.request_google_maps(20005)
        self.assertTrue(expected in self.param)
        self.assertEqual(response['Success'], 'success')

    def test_geocode_compute_key__empty(self):
        """Testing _geocode_compute_key, with empty argument."""
        result = _geocode_compute_key('')
        self.assertEqual(result, '')

    def test_geocode_compute_key__zipcode(self):
        """Testing _geocode_compute_key, zipcode as argument."""
        result = _geocode_compute_key('20005')
        self.assertEqual(result, '20005')
        result = _geocode_compute_key('20005-1999')
        self.assertEqual(result, '20005-1999')

    def test_geocode_compute_key__address(self):
        """Testing _geocode_compute_key, with an address."""
        result = _geocode_compute_key('123 Some str, Washington DC, 20005')
        self.assertEqual(result, '123 NONE SOME ST.|WASHINGTON,DC|20005')

    def test_extract_zip_code__empty(self):
        """Testing _extract_zip_code, with empty argument."""
        result = _extract_zip_code('')
        self.assertEqual(result, '')

    def test_extract_zip_code__zipcode(self):
        """Testing _extract_zip_code, with zip code."""
        result = _extract_zip_code('20005')
        self.assertEqual(result, '20005')
        result = _extract_zip_code('20005-1999')
        self.assertEqual(result, '20005-1999')

    def test_extract_zip_code__address(self):
        """Testing _extract_zip_code, with a key generated from an address."""
        result = _extract_zip_code('123 NONE SOME ST.|WASHINGTON,DC|20005')
        self.assertEqual(result, '20005')

    def test_geocode_cached_data__new(self):
        """Testing _geocode_cached_data, with an arg that is not in caches."""
        self.assertRaises(Exception, _geocode_cached_data,
                          'DEFINITELY-NOT-CACHED')

    def test_geocode_cached_data__existent(self):
        """Testing _geocode_cached_data, with empty argument."""
        cg = CachedGeodata(key='NEW-RECORD',
                           lat=1,
                           lon=2,
                           expires=time.time() + 10000)
        cg.save()
        result = _geocode_cached_data('NEW-RECORD')
        self.assertTrue('result' in result)
        self.assertEqual(result['result'][0], 'NEW-RECORD')
        self.assertEqual(result['result'][1], 1)
        self.assertEqual(result['result'][2], 2)

    def test_geocode_cached_data__expired(self):
        """Testing _geocode_cached_data, with an expired record."""
        cg = CachedGeodata(key='EXPIRED-RECORD',
                           lat=1,
                           lon=2,
                           expires=time.time() - 10)
        cg.save()
        self.assertRaises(Exception, _geocode_cached_data, 'EXPIRED-RECORD')

    def test_convert_data__good_data(self):
        """Testing _convert_data, with an expected data structure."""
        data = {'result': ['20005', 'LAT', 'LON']}
        result = _convert_data(data)
        self.assertTrue('zip' in result)
        self.assertEqual(result['zip']['zipcode'], '20005')
        self.assertEqual(result['zip']['lat'], 'LAT')
        self.assertEqual(result['zip']['lng'], 'LON')

    def test_conver_data__bad_data(self):
        """Testing _convert_data, with bad data structure."""
        data = {'esult': ['20005', 'LAT', 'LON']}
        result = _convert_data(data)
        self.assertEqual(result, data)

    @patch('hud_api_replace.geocode.GoogleGeocode.google_maps_api')
    def test_geocode_get_data__new(self, mock_geocode):
        """Testing geocode_get_data, with new argument."""
        mock_geocode.return_value = {'result': ['20005', 11, 22]}
        result = geocode_get_data('DEFINITELY-NOT_CACHED')
        self.assertTrue('zip' in result)
        self.assertEqual(result['zip']['zipcode'], '20005')
        self.assertEqual(result['zip']['lat'], 11)
        self.assertEqual(result['zip']['lng'], 22)

    def test_geocode_get_data__existent(self):
        """Testing geocode_get_data, with cached argument."""
        cg = CachedGeodata(key='20005',
                           lat=111,
                           lon=222,
                           expires=time.time() + 10000)
        cg.save()
        result = geocode_get_data('20005')
        self.assertTrue('zip' in result)
        self.assertEqual(result['zip']['zipcode'], '20005')
        self.assertEqual(result['zip']['lat'], 111)
        self.assertEqual(result['zip']['lng'], 222)

    @patch('hud_api_replace.geocode.GoogleGeocode.google_maps_api')
    def test_geocode_get_data__expired(self, mock_geocode):
        """Testing geocode_get_data, with empty argument."""
        mock_geocode.return_value = {'result': ['20006', 11, 22]}
        cg = CachedGeodata(key='20006',
                           lat=111,
                           lon=222,
                           expires=time.time() - 10000)
        cg.save()
        self.assertRaises(IntegrityError, geocode_get_data, '20006')
Ejemplo n.º 5
0
 def setUp(self):
     self.gc = GoogleGeocode(20005)
Ejemplo n.º 6
0
class TestGoogleGeocode(TestCase):
    def setUp(self):
        self.gc = GoogleGeocode(20005)

    def test_init(self):
        """ Testing __init__ """
        self.assertEqual(self.gc.zipcode, 20005)
        self.assertEqual(type(self.gc.invisible_zipcodes), dict)
        self.assertTrue(len(self.gc.invisible_zipcodes) > 0)
        self.assertTrue(self.gc.privateKey is not None)
        self.assertTrue(self.gc.clientID is not None)

    def test_is_usa_or_territory__correct(self):
        """ Testing is_usa_or_territory, GUAM, USVI, ASamoa, PRico, Cnmi, Rmi, Usa """
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, GUAM something else"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, UsVi"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, American Samoa else"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, Puerto Rico something else"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, Cnmi"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, rmi"))
        self.assertTrue(self.gc.is_usa_or_territory("123 Address, Usa"))

    def test_is_usa_or_territory__wrong(self):
        """ Testing is_usa_or_territory, Not USA address """
        self.assertFalse(self.gc.is_usa_or_territory("123 Address, Mexico City, Mexico"))

    def test_signed_url__correct(self):
        """ Testing signed_url, correct url """
        url = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID"
        expected = "http://maps.googleapis.com/maps/api/geocode/json?address=New+York&sensor=false&client=clientID&signature=KrU1TzVQM7Ur0i8i7K3huiw3MsA="
        self.gc.privateKey = "vNIXE0xscrmjlyV-12Nj_BvUPaw="
        self.gc.clientID = "clientID"
        signed_url = self.gc.signed_url(url)
        self.assertEqual(signed_url, expected)

    def test_signed_url__empty(self):
        """ Testing signed_url, empty url """
        url = ""
        signed_url = self.gc.signed_url(url)
        self.assertEqual(signed_url, None)

    @patch.object(GoogleGeocode, "signed_url")
    @patch("urllib2.urlopen")
    def test_request_google_maps(self, mock_urlopen, mock_signed_url):
        """ Testing request_google_maps """

        def urlopen_check_param(param):
            self.param = param
            mm = MagicMock()
            mm.read.return_value = '{"Success":"success"}'
            return mm

        mock_urlopen.side_effect = urlopen_check_param
        expected = (
            "http://maps.googleapis.com/maps/api/geocode/json?address=20005&sensor=false&client=" + self.gc.clientID
        )
        mock_signed_url.return_value = expected
        response = self.gc.request_google_maps(20005)
        self.assertTrue(expected in self.param)
        self.assertEqual(response["Success"], "success")

    def test_geocode_compute_key__empty(self):
        """Testing _geocode_compute_key, with empty argument."""
        result = _geocode_compute_key("")
        self.assertEqual(result, "")

    def test_geocode_compute_key__zipcode(self):
        """Testing _geocode_compute_key, zipcode as argument."""
        result = _geocode_compute_key("20005")
        self.assertEqual(result, "20005")
        result = _geocode_compute_key("20005-1999")
        self.assertEqual(result, "20005-1999")

    def test_geocode_compute_key__address(self):
        """Testing _geocode_compute_key, with an address."""
        result = _geocode_compute_key("123 Some str, Washington DC, 20005")
        self.assertEqual(result, "123 NONE SOME ST.|WASHINGTON,DC|20005")

    def test_extract_zip_code__empty(self):
        """Testing _extract_zip_code, with empty argument."""
        result = _extract_zip_code("")
        self.assertEqual(result, "")

    def test_extract_zip_code__zipcode(self):
        """Testing _extract_zip_code, with zip code."""
        result = _extract_zip_code("20005")
        self.assertEqual(result, "20005")
        result = _extract_zip_code("20005-1999")
        self.assertEqual(result, "20005-1999")

    def test_extract_zip_code__address(self):
        """Testing _extract_zip_code, with a key generated from an address."""
        result = _extract_zip_code("123 NONE SOME ST.|WASHINGTON,DC|20005")
        self.assertEqual(result, "20005")

    def test_geocode_cached_data__new(self):
        """Testing _geocode_cached_data, with an arg that is not in caches."""
        self.assertRaises(Exception, _geocode_cached_data, "DEFINITELY-NOT-CACHED")

    def test_geocode_cached_data__existent(self):
        """Testing _geocode_cached_data, with empty argument."""
        cg = CachedGeodata(key="NEW-RECORD", lat=1, lon=2, expires=time.time() + 10000)
        cg.save()
        result = _geocode_cached_data("NEW-RECORD")
        self.assertTrue("result" in result)
        self.assertEqual(result["result"][0], "NEW-RECORD")
        self.assertEqual(result["result"][1], 1)
        self.assertEqual(result["result"][2], 2)

    def test_geocode_cached_data__expired(self):
        """Testing _geocode_cached_data, with an expired record."""
        cg = CachedGeodata(key="EXPIRED-RECORD", lat=1, lon=2, expires=time.time() - 10)
        cg.save()
        self.assertRaises(Exception, _geocode_cached_data, "EXPIRED-RECORD")

    def test_convert_data__good_data(self):
        """Testing _convert_data, with an expected data structure."""
        data = {"result": ["20005", "LAT", "LON"]}
        result = _convert_data(data)
        self.assertTrue("zip" in result)
        self.assertEqual(result["zip"]["zipcode"], "20005")
        self.assertEqual(result["zip"]["lat"], "LAT")
        self.assertEqual(result["zip"]["lng"], "LON")

    def test_conver_data__bad_data(self):
        """Testing _convert_data, with bad data structure."""
        data = {"esult": ["20005", "LAT", "LON"]}
        result = _convert_data(data)
        self.assertEqual(result, data)

    @patch("hud_api_replace.geocode.GoogleGeocode.google_maps_api")
    def test_geocode_get_data__new(self, mock_geocode):
        """Testing geocode_get_data, with new argument."""
        mock_geocode.return_value = {"result": ["20005", 11, 22]}
        result = geocode_get_data("DEFINITELY-NOT_CACHED")
        self.assertTrue("zip" in result)
        self.assertEqual(result["zip"]["zipcode"], "20005")
        self.assertEqual(result["zip"]["lat"], 11)
        self.assertEqual(result["zip"]["lng"], 22)

    def test_geocode_get_data__existent(self):
        """Testing geocode_get_data, with cached argument."""
        cg = CachedGeodata(key="20005", lat=111, lon=222, expires=time.time() + 10000)
        cg.save()
        result = geocode_get_data("20005")
        self.assertTrue("zip" in result)
        self.assertEqual(result["zip"]["zipcode"], "20005")
        self.assertEqual(result["zip"]["lat"], 111)
        self.assertEqual(result["zip"]["lng"], 222)

    @patch("hud_api_replace.geocode.GoogleGeocode.google_maps_api")
    def test_geocode_get_data__expired(self, mock_geocode):
        """Testing geocode_get_data, with empty argument."""
        mock_geocode.return_value = {"result": ["20006", 11, 22]}
        cg = CachedGeodata(key="20006", lat=111, lon=222, expires=time.time() - 10000)
        cg.save()
        result = geocode_get_data("20006")
        self.assertEqual(result["zip"]["zipcode"], "20006")
        self.assertEqual(result["zip"]["lat"], 11)
        self.assertEqual(result["zip"]["lng"], 22)