mirror of https://github.com/django/django.git
Fixed #28981 -- Added an exception if GeoIP database can't be loaded from the path.
This commit is contained in:
parent
84398897a7
commit
d171843f57
|
@ -92,6 +92,9 @@ class GeoIP2:
|
||||||
if os.path.isfile(city_db):
|
if os.path.isfile(city_db):
|
||||||
self._city = geoip2.database.Reader(city_db, mode=cache)
|
self._city = geoip2.database.Reader(city_db, mode=cache)
|
||||||
self._city_file = city_db
|
self._city_file = city_db
|
||||||
|
|
||||||
|
if not self._reader:
|
||||||
|
raise GeoIP2Exception('Could not load a database from %s.' % path)
|
||||||
elif os.path.isfile(path):
|
elif os.path.isfile(path):
|
||||||
# Otherwise, some detective work will be needed to figure out
|
# Otherwise, some detective work will be needed to figure out
|
||||||
# whether the given database path is for the GeoIP country or city
|
# whether the given database path is for the GeoIP country or city
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import os
|
import os
|
||||||
import unittest
|
|
||||||
from unittest import mock, skipUnless
|
from unittest import mock, skipUnless
|
||||||
|
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.contrib.gis.geoip2 import HAS_GEOIP2
|
from django.contrib.gis.geoip2 import HAS_GEOIP2
|
||||||
from django.contrib.gis.geos import GEOSGeometry
|
from django.contrib.gis.geos import GEOSGeometry
|
||||||
|
from django.test import SimpleTestCase
|
||||||
|
|
||||||
if HAS_GEOIP2:
|
if HAS_GEOIP2:
|
||||||
from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
|
from django.contrib.gis.geoip2 import GeoIP2, GeoIP2Exception
|
||||||
|
@ -18,7 +18,7 @@ if HAS_GEOIP2:
|
||||||
HAS_GEOIP2 and getattr(settings, "GEOIP_PATH", None),
|
HAS_GEOIP2 and getattr(settings, "GEOIP_PATH", None),
|
||||||
"GeoIP is required along with the GEOIP_PATH setting."
|
"GeoIP is required along with the GEOIP_PATH setting."
|
||||||
)
|
)
|
||||||
class GeoIPTest(unittest.TestCase):
|
class GeoIPTest(SimpleTestCase):
|
||||||
addr = '128.249.1.1'
|
addr = '128.249.1.1'
|
||||||
fqdn = 'tmc.edu'
|
fqdn = 'tmc.edu'
|
||||||
|
|
||||||
|
@ -53,6 +53,12 @@ class GeoIPTest(unittest.TestCase):
|
||||||
with self.assertRaises(e):
|
with self.assertRaises(e):
|
||||||
GeoIP2(bad, 0)
|
GeoIP2(bad, 0)
|
||||||
|
|
||||||
|
def test_no_database_file(self):
|
||||||
|
invalid_path = os.path.join(os.path.dirname(__file__), 'data')
|
||||||
|
msg = 'Could not load a database from %s.' % invalid_path
|
||||||
|
with self.assertRaisesMessage(GeoIP2Exception, msg):
|
||||||
|
GeoIP2(invalid_path)
|
||||||
|
|
||||||
def test02_bad_query(self):
|
def test02_bad_query(self):
|
||||||
"GeoIP query parameter checking."
|
"GeoIP query parameter checking."
|
||||||
cntry_g = GeoIP2(city='<foo>')
|
cntry_g = GeoIP2(city='<foo>')
|
||||||
|
|
Loading…
Reference in New Issue