def geoip_db_is_available(app_configs, **kwargs): errors = [] if geolocation.geoip_reader is None: # try loading it geolocation.load_geoip_database() if geolocation.geoip_reader is None: errors.append(Error("GeoIP DB not available", id=ERROR_GEOIP_DB_NOT_AVAILABLE)) if not errors: # DB seems to be available, test a known value expected = "US" actual = geolocation.get_country_code("1.2.3.4") if actual != expected: errors.append( Error( f"GeoIP DB returned unexpected result. Expected {expected} got {actual}", id=ERROR_GEOIP_DB_UNEXPECTED_RESULT, ) ) return errors
def geoip_db_is_available(app_configs, **kwargs): errors = [] if geolocation.geoip_reader is None: # try loading it geolocation.load_geoip_database() if geolocation.geoip_reader is None: errors.append( Error("GeoIP DB not available", id=ERROR_GEOIP_DB_NOT_AVAILABLE)) if not errors: # DB seems to be available, try and do a lookup that should resolve to # some country. The specific country isn't important. ip = "1.2.3.4" country = geolocation.get_country_code(ip) if country is None: errors.append( Error(f"GeoIP DB returned no country for {ip!r}", id=ERROR_GEOIP_DB_UNEXPECTED_RESULT)) return errors
def country(self): ip_address = get_client_ip(self.request) if ip_address is None: return None else: return get_country_code(ip_address)