diff --git a/django/contrib/gis/geoip/base.py b/django/contrib/gis/geoip/base.py index a1d476c0a5..d40ae7e9ae 100644 --- a/django/contrib/gis/geoip/base.py +++ b/django/contrib/gis/geoip/base.py @@ -13,7 +13,7 @@ from django.contrib.gis.geoip.prototypes import ( from django.core.validators import ipv4_re from django.utils import six from django.utils.deprecation import RemovedInDjango20Warning -from django.utils.encoding import force_bytes +from django.utils.encoding import force_bytes, force_text # Regular expressions for recognizing the GeoIP free database editions. free_regex = re.compile(r'^GEO-\d{3}FREE') @@ -145,6 +145,17 @@ class GeoIP(object): if self._city: GeoIP_delete(self._city) + def __repr__(self): + version = '' + if GeoIP_lib_version is not None: + version += ' [v%s]' % force_text(GeoIP_lib_version()) + return '<%(cls)s%(version)s _country_file="%(country)s", _city_file="%(city)s">' % { + 'cls': self.__class__.__name__, + 'version': version, + 'country': self._country_file, + 'city': self._city_file, + } + def _check_query(self, query, country=False, city=False, city_or_country=False): "Helper routine for checking the query and database availability." # Making sure a string was passed in for the query. diff --git a/django/contrib/gis/geoip2/base.py b/django/contrib/gis/geoip2/base.py index 582358969c..44164f1d71 100644 --- a/django/contrib/gis/geoip2/base.py +++ b/django/contrib/gis/geoip2/base.py @@ -133,6 +133,16 @@ class GeoIP2(object): if self._reader: self._reader.close() + def __repr__(self): + meta = self._reader.metadata() + version = '[v%s.%s]' % (meta.binary_format_major_version, meta.binary_format_minor_version) + return '<%(cls)s %(version)s _country_file="%(country)s", _city_file="%(city)s">' % { + 'cls': self.__class__.__name__, + 'version': version, + 'country': self._country_file, + 'city': self._city_file, + } + def _check_query(self, query, country=False, city=False, city_or_country=False): "Helper routine for checking the query and database availability." # Making sure a string was passed in for the query. diff --git a/tests/gis_tests/test_geoip.py b/tests/gis_tests/test_geoip.py index 6afd441216..116f65b11d 100644 --- a/tests/gis_tests/test_geoip.py +++ b/tests/gis_tests/test_geoip.py @@ -8,10 +8,12 @@ from unittest import skipUnless from django.conf import settings from django.contrib.gis.geoip import HAS_GEOIP +from django.contrib.gis.geoip.prototypes import GeoIP_lib_version from django.contrib.gis.geos import HAS_GEOS, GEOSGeometry from django.test import ignore_warnings from django.utils import six from django.utils.deprecation import RemovedInDjango20Warning +from django.utils.encoding import force_text if HAS_GEOIP: from django.contrib.gis.geoip import GeoIP, GeoIPException @@ -128,3 +130,21 @@ class GeoIPTest(unittest.TestCase): self.assertEqual(len(warns), 1) msg = str(warns[0].message) self.assertIn('django.contrib.gis.geoip is deprecated', msg) + + def test_repr(self): + path = settings.GEOIP_PATH + g = GeoIP(path=path) + country_path = g._country_file + city_path = g._city_file + if GeoIP_lib_version: + expected = '' % { + 'version': force_text(GeoIP_lib_version()), + 'country': country_path, + 'city': city_path, + } + else: + expected = '' % { + 'country': country_path, + 'city': city_path, + } + self.assertEqual(repr(g), expected) diff --git a/tests/gis_tests/test_geoip2.py b/tests/gis_tests/test_geoip2.py index 7b81a1a7de..71824c3916 100644 --- a/tests/gis_tests/test_geoip2.py +++ b/tests/gis_tests/test_geoip2.py @@ -137,3 +137,17 @@ class GeoIPTest(unittest.TestCase): self.assertEqual('US', d['country_code']) self.assertEqual('Lawrence', d['city']) self.assertEqual('KS', d['region']) + + def test_repr(self): + path = settings.GEOIP_PATH + g = GeoIP2(path=path) + meta = g._reader.metadata() + version = '%s.%s' % (meta.binary_format_major_version, meta.binary_format_minor_version) + country_path = g._country_file + city_path = g._city_file + expected = '' % { + 'version': version, + 'country': country_path, + 'city': city_path, + } + self.assertEqual(repr(g), expected)