[FIX] website: GeoIP Legacy compatibility

Some IPs (like google-proxy ones) are not bound to a specific country.
Legacy Database used to set country_code to 'EU' for these one. geoip2,
on contrary, does not set the country. Read information from `continent`
attribute in this case.

opw-1844888
This commit is contained in:
Christophe Simonis
2018-05-09 11:40:56 +02:00
parent 468e2e454e
commit 4431c8c843
+6 -2
View File
@@ -47,10 +47,14 @@ class GeoIPResolver(object):
r = self._db.city(ip)
except (ValueError, geoip2.errors.AddressNotFoundError):
return {}
# Compatibility with Legacy database.
# Some ips cannot be located to a specific country. Legacy DB used to locate them in
# continent instead of country. Do the same to not change behavior of existing code.
country = r.country if r.country.geoname_id else r.continent
return {
'city': r.city.name,
'country_code': r.country.iso_code,
'country_name': r.country.name,
'country_code': country.iso_code,
'country_name': country.name,
'region': r.subdivisions[0].iso_code if r.subdivisions else None,
'time_zone': r.location.time_zone,
}