def _set_maxmind_geolocation(self, ip_address, country, city): geo_record = maxmind.GeoRecord() address_family = None if _is_valid_ipv6(ip_address): address_family = message.ADDRESS_FAMILY_IPv6 elif _is_valid_ipv4(ip_address): address_family = message.ADDRESS_FAMILY_IPv4 if ip_address is not None: logging.debug('Getting maxmind info for ip %s in family %s', ip_address, address_family) geo_record = maxmind.get_ip_geolocation(ip_address) elif city is not None and country is not None: geo_record = maxmind.get_city_geolocation(city, country) elif country is not None: geo_record = maxmind.get_country_geolocation(country) self._maxmind_city = geo_record.city self._maxmind_country = geo_record.country self._maxmind_latitude = geo_record.latitude self._maxmind_longitude = geo_record.longitude return (geo_record.latitude is not None and geo_record.longitude is not None)
def testGetCityGeolocationNoCity(self): self.assertNoneGeoRecord( maxmind.get_city_geolocation( 'unused_city', 'unused_country', city_table=MaxmindTestClass.ModelMockup( gql_obj=MaxmindTestClass.GqlMockup())))
def _set_maxmind_geolocation(self, ip_address, country, city): geo_record = maxmind.GeoRecord() if ip_address is not None: geo_record = maxmind.get_ip_geolocation(ip_address) elif city is not None and country is not None: geo_record = maxmind.get_city_geolocation(city, country) elif country is not None: geo_record = maxmind.get_country_geolocation(country) self._maxmind_city = geo_record.city self._maxmind_country = geo_record.country self._maxmind_latitude = geo_record.latitude self._maxmind_longitude = geo_record.longitude
def testGetCityGeolocationYesCity(self): class Location: def __init__(self): self.city = 'city' self.country = 'country' self.latitude = 'latitude' self.longitude = 'longitude' location = Location() expected_geo_record = maxmind.GeoRecord() expected_geo_record.city = location.city expected_geo_record.country = location.country expected_geo_record.latitude = location.latitude expected_geo_record.longitude = location.longitude self.assertGeoRecordEqual( expected_geo_record, maxmind.get_city_geolocation( 'unused_city', 'unused_country', city_table=MaxmindTestClass.ModelMockup( gql_obj=MaxmindTestClass.GqlMockup(result=location))))
def testGetCityGeolocationYesCity(self): class Location: def __init__(self): self.city = 'city' self.country = 'country' self.latitude = 'latitude' self.longitude = 'longitude' location = Location() expected_geo_record = maxmind.GeoRecord() expected_geo_record.city = location.city expected_geo_record.country = location.country expected_geo_record.latitude = location.latitude expected_geo_record.longitude = location.longitude self.assertEqual( expected_geo_record, maxmind.get_city_geolocation( 'unused_city', 'unused_country', city_table=MaxmindTestClass.ModelMockup( gql_obj=MaxmindTestClass.GqlMockup(result=location))))
def _set_maxmind_geolocation(self, ip_address, country, city): geo_record = maxmind.GeoRecord() address_family = None if _is_valid_ipv6(ip_address): address_family = message.ADDRESS_FAMILY_IPv6 elif _is_valid_ipv4(ip_address): address_family = message.ADDRESS_FAMILY_IPv4 if ip_address is not None: logging.debug('Getting maxmind info for ip %s in family %s', ip_address, address_family) geo_record = maxmind.get_ip_geolocation(ip_address) elif city is not None and country is not None: geo_record = maxmind.get_city_geolocation(city, country) elif country is not None: geo_record = maxmind.get_country_geolocation(country) self._maxmind_city = geo_record.city self._maxmind_country = geo_record.country self._maxmind_latitude = geo_record.latitude self._maxmind_longitude = geo_record.longitude