Skip to content

Commit

Permalink
Refs #24121 -- Added a repr() to gis.GeoIP and gis.GeoIP2.
Browse files Browse the repository at this point in the history
  • Loading branch information
kezabelle authored and timgraham committed Aug 31, 2015
1 parent 9c40f01 commit d4b10a7
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 1 deletion.
13 changes: 12 additions & 1 deletion django/contrib/gis/geoip/base.py
Expand Up @@ -13,7 +13,7 @@
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')
Expand Down Expand Up @@ -145,6 +145,17 @@ def __del__(self):
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.
Expand Down
10 changes: 10 additions & 0 deletions django/contrib/gis/geoip2/base.py
Expand Up @@ -133,6 +133,16 @@ def __del__(self):
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.
Expand Down
20 changes: 20 additions & 0 deletions tests/gis_tests/test_geoip.py
Expand Up @@ -8,10 +8,12 @@

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
Expand Down Expand Up @@ -128,3 +130,21 @@ def test_deprecation_warning(self):
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 = '<GeoIP [v%(version)s] _country_file="%(country)s", _city_file="%(city)s">' % {
'version': force_text(GeoIP_lib_version()),
'country': country_path,
'city': city_path,
}
else:
expected = '<GeoIP _country_file="%(country)s", _city_file="%(city)s">' % {
'country': country_path,
'city': city_path,
}
self.assertEqual(repr(g), expected)
14 changes: 14 additions & 0 deletions tests/gis_tests/test_geoip2.py
Expand Up @@ -137,3 +137,17 @@ def test06_ipv6_query(self):
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 = '<GeoIP2 [v%(version)s] _country_file="%(country)s", _city_file="%(city)s">' % {
'version': version,
'country': country_path,
'city': city_path,
}
self.assertEqual(repr(g), expected)

0 comments on commit d4b10a7

Please sign in to comment.