def to_python(cls, data): data = data.copy() ident = data.pop('id', None) if ident is not None: ident = trim(six.text_type(ident), 128) try: email = trim(validate_email(data.pop('email', None), False), MAX_EMAIL_FIELD_LENGTH) except ValueError: raise InterfaceValidationError("Invalid value for 'email'") username = data.pop('username', None) if username is not None: username = trim(six.text_type(username), 128) name = data.pop('name', None) if name is not None: name = trim(six.text_type(name), 128) try: ip_address = validate_ip(data.pop('ip_address', None), False) except ValueError: raise InterfaceValidationError("Invalid value for 'ip_address'") geo = data.pop('geo', None) if not geo and ip_address: geo = Geo.from_ip_address(ip_address) elif geo: geo = Geo.to_python(geo) extra_data = data.pop('data', None) if not isinstance(extra_data, dict): extra_data = {} extra_data.update(data) # TODO(dcramer): patch in fix to deal w/ old data but not allow new # if not (ident or email or username or ip_address): # raise ValueError('No identifying value') kwargs = { 'id': ident, 'email': email, 'username': username, 'ip_address': ip_address, 'name': name, 'geo': geo, 'data': trim_dict(extra_data) } return cls(**kwargs)
def test_iso_8859_1_country_code(self, geo_by_addr_mock): # https://github.com/maxmind/geoip-api-python/releases/tag/v1.3.2 # Previously GeoIP.country_names was populated from GeoIP_country_name in # the libGeoIP C API. Some versions of the libGeoIP include non-ASCII # ISO-8859-1 characters in these names, causing encoding errors under Python geo_by_addr_mock.return_value = { 'city': 'San Francisco', 'country_code': '\xc5lborg', 'region': 'CA', } assert Geo.from_ip_address('192.168.0.1').to_json() == { 'city': u'San Francisco', 'country_code': u'\xc5lborg', 'region': u'CA' }
def test_iso_8859_1_country_code(geo_by_addr_mock): # https://github.com/maxmind/geoip-api-python/releases/tag/v1.3.2 # Previously GeoIP.country_names was populated from GeoIP_country_name in # the libGeoIP C API. Some versions of the libGeoIP include non-ASCII # ISO-8859-1 characters in these names, causing encoding errors under Python geo_by_addr_mock.return_value = { "city": "San Francisco", "country_code": "\xc5lborg", "region": "CA", } assert Geo.from_ip_address("192.168.0.1").to_json() == { "city": u"San Francisco", "country_code": u"\xc5lborg", "region": u"CA", }
def test_from_ip_address(self, geo_by_addr_mock): geo_by_addr_mock.return_value = { 'area_code': 415, 'city': 'San Francisco', 'country_code': 'US', 'country_code3': 'USA', 'country_name': 'United States', 'dma_code': 807, 'latitude': 37.79570007324219, 'longitude': -122.4208984375, 'metro_code': 807, 'postal_code': '94109', 'region': 'CA', 'region_name': 'California', 'time_zone': 'America/Los_Angeles' } assert Geo.from_ip_address('192.168.0.1').to_json() == { 'country_code': 'US', 'city': 'San Francisco', 'region': 'CA', }
def test_from_ip_address(geo_by_addr_mock): geo_by_addr_mock.return_value = { "area_code": 415, "city": "San Francisco", "country_code": "US", "country_code3": "USA", "country_name": "United States", "dma_code": 807, "latitude": 37.79570007324219, "longitude": -122.4208984375, "metro_code": 807, "postal_code": "94109", "region": "CA", "region_name": "California", "time_zone": "America/Los_Angeles", } assert Geo.from_ip_address("192.168.0.1").to_json() == { "country_code": "US", "city": "San Francisco", "region": "CA", }
def to_python(cls, data, rust_renormalized=RUST_RENORMALIZED_DEFAULT): if rust_renormalized: data = data.copy() for key in ( 'id', 'email', 'username', 'ip_address', 'name', 'geo', 'data', ): data.setdefault(key, None) if data['geo'] is not None: data['geo'] = Geo.to_python(data['geo']) return cls(**data) data = data.copy() ident = data.pop('id', None) if ident is not None: ident = trim(six.text_type(ident), 128) email = data.pop('email', None) if not isinstance(email, six.string_types): email = None email = trim(email, MAX_EMAIL_FIELD_LENGTH) username = data.pop('username', None) if username is not None: username = trim(six.text_type(username), 128) name = data.pop('name', None) if name is not None: name = trim(six.text_type(name), 128) try: ip_address = validate_ip(data.pop('ip_address', None), False) except ValueError: ip_address = None geo = data.pop('geo', None) if not geo and ip_address: geo = Geo.from_ip_address(ip_address) elif geo: geo = Geo.to_python(geo) extra_data = data.pop('data', None) if not isinstance(extra_data, dict): extra_data = {} extra_data.update(data) # TODO(dcramer): patch in fix to deal w/ old data but not allow new # if not (ident or email or username or ip_address): # raise ValueError('No identifying value') kwargs = { 'id': ident, 'email': email, 'username': username, 'ip_address': ip_address, 'name': name, 'geo': geo, 'data': trim_dict(extra_data) } return cls(**kwargs)