mirror of https://github.com/django/django.git
Refs #24121 -- Added a repr() to gis.GeoIP and gis.GeoIP2.
This commit is contained in:
parent
9c40f01a66
commit
d4b10a7256
|
@ -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.
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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 = '<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)
|
||||
|
|
|
@ -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 = '<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)
|
||||
|
|
Loading…
Reference in New Issue