Fixed #35092 -- Exposed extra fields for GeoIP2.country() and GeoIP2.city() responses.

This commit is contained in:
Nick Pope 2024-01-10 11:09:44 +00:00 committed by GitHub
parent 9b02ad91ea
commit f50184a84b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 52 additions and 17 deletions

View File

@ -203,18 +203,23 @@ class GeoIP2:
response = self._city.city(enc_query)
region = response.subdivisions[0] if response.subdivisions else None
return {
"accuracy_radius": response.location.accuracy_radius,
"city": response.city.name,
"continent_code": response.continent.code,
"continent_name": response.continent.name,
"country_code": response.country.iso_code,
"country_name": response.country.name,
"dma_code": response.location.metro_code,
"is_in_european_union": response.country.is_in_european_union,
"latitude": response.location.latitude,
"longitude": response.location.longitude,
"metro_code": response.location.metro_code,
"postal_code": response.postal.code,
"region": region.iso_code if region else None,
"region_code": region.iso_code if region else None,
"region_name": region.name if region else None,
"time_zone": response.location.time_zone,
# Kept for backward compatibility.
"dma_code": response.location.metro_code,
"region": region.iso_code if region else None,
}
def country_code(self, query):
@ -235,8 +240,11 @@ class GeoIP2:
enc_query = self._check_query(query, city_or_country=True)
response = self._country_or_city(enc_query)
return {
"continent_code": response.continent.code,
"continent_name": response.continent.name,
"country_code": response.country.iso_code,
"country_name": response.country.name,
"is_in_european_union": response.country.is_in_european_union,
}
def coords(self, query, ordering=("longitude", "latitude")):

View File

@ -33,20 +33,28 @@ Here is an example of its usage:
>>> from django.contrib.gis.geoip2 import GeoIP2
>>> g = GeoIP2()
>>> g.country("google.com")
{'country_code': 'US', 'country_name': 'United States'}
{'continent_code': 'NA',
'continent_name': 'North America',
'country_code': 'US',
'country_name': 'United States',
'is_in_european_union': False}
>>> g.city("72.14.207.99")
{'city': 'Mountain View',
'continent_code': 'NA',
'continent_name': 'North America',
'country_code': 'US',
'country_name': 'United States',
'dma_code': 807,
'is_in_european_union': False,
'latitude': 37.419200897216797,
'longitude': -122.05740356445312,
'postal_code': '94043',
'region': 'CA',
'time_zone': 'America/Los_Angeles'}
{'accuracy_radius': 1000,
'city': 'Mountain View',
'continent_code': 'NA',
'continent_name': 'North America',
'country_code': 'US',
'country_name': 'United States',
'is_in_european_union': False,
'latitude': 37.419200897216797,
'longitude': -122.05740356445312,
'metro_code': 807,
'postal_code': '94043',
'region_code': 'CA',
'region_name': 'California',
'time_zone': 'America/Los_Angeles',
'dma_code': 807,
'region': 'CA'}
>>> g.lat_lon("salon.com")
(39.0437, -77.4875)
>>> g.lon_lat("uh.edu")

View File

@ -62,6 +62,14 @@ Minor features
* :class:`~django.contrib.gis.geoip2.GeoIP2` now allows querying using
:class:`ipaddress.IPv4Address` or :class:`ipaddress.IPv6Address` objects.
* :meth:`.GeoIP2.country` now exposes the ``continent_code``,
``continent_name``, and ``is_in_european_union`` values.
* :meth:`.GeoIP2.city` now exposes the ``accuracy_radius`` and ``region_name``
values. In addition the ``dma_code`` and ``region`` values are now exposed as
``metro_code`` and ``region_code``, but the previous keys are also retained
for backward compatibility.
:mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

View File

@ -108,8 +108,11 @@ class GeoLite2Test(SimpleTestCase):
self.assertEqual(
g.country(query),
{
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
},
)
self.assertEqual(g.country_code(query), "GB")
@ -122,18 +125,23 @@ class GeoLite2Test(SimpleTestCase):
self.assertEqual(
g.city(query),
{
"accuracy_radius": 100,
"city": "Boxford",
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"dma_code": None,
"is_in_european_union": False,
"latitude": 51.75,
"longitude": -1.25,
"metro_code": None,
"postal_code": "OX1",
"region": "ENG",
"region_code": "ENG",
"region_name": "England",
"time_zone": "Europe/London",
# Kept for backward compatibility.
"dma_code": None,
"region": "ENG",
},
)
@ -148,8 +156,11 @@ class GeoLite2Test(SimpleTestCase):
self.assertEqual(
g.country(query),
{
"continent_code": "EU",
"continent_name": "Europe",
"country_code": "GB",
"country_name": "United Kingdom",
"is_in_european_union": False,
},
)
self.assertEqual(g.country_code(query), "GB")