From 4431c8c843f28f9229a5cacb8b786b2b8303fd6b Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Wed, 9 May 2018 11:40:56 +0200 Subject: [PATCH] [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 --- addons/website/geoipresolver.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/addons/website/geoipresolver.py b/addons/website/geoipresolver.py index 1f9b0e22724..e95b9318eb2 100644 --- a/addons/website/geoipresolver.py +++ b/addons/website/geoipresolver.py @@ -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, }