Fixed #33442 -- Allowed GeoIP2 to use DB-IP Lite datasets.

This commit is contained in:
Claude Paroz 2022-07-29 18:34:18 +02:00 committed by Mariusz Felisiak
parent 6654289f5b
commit 1e5bbbb2a8
3 changed files with 14 additions and 5 deletions

View File

@ -103,11 +103,11 @@ class GeoIP2:
reader = geoip2.database.Reader(str(path), mode=cache) reader = geoip2.database.Reader(str(path), mode=cache)
db_type = reader.metadata().database_type db_type = reader.metadata().database_type
if db_type.endswith("City"): if "City" in db_type:
# GeoLite City database detected. # GeoLite City database detected.
self._city = reader self._city = reader
self._city_file = path self._city_file = path
elif db_type.endswith("Country"): elif "Country" in db_type:
# GeoIP Country database detected. # GeoIP Country database detected.
self._country = reader self._country = reader
self._country_file = path self._country_file = path

View File

@ -10,9 +10,10 @@ library`__. [#]_
In order to perform IP-based geolocation, the :class:`GeoIP2` object requires In order to perform IP-based geolocation, the :class:`GeoIP2` object requires
the `geoip2 Python library`__ and the GeoIP ``Country`` and/or ``City`` the `geoip2 Python library`__ and the GeoIP ``Country`` and/or ``City``
`datasets in binary format`__ (the CSV files will not work!). Grab the datasets in binary format (the CSV files will not work!), downloaded from e.g.
``GeoLite2-Country.mmdb.gz`` and ``GeoLite2-City.mmdb.gz`` files and unzip them `MaxMind`__ or `DB-IP`__ websites. Grab the ``GeoLite2-Country.mmdb.gz`` and
in a directory corresponding to the :setting:`GEOIP_PATH` setting. ``GeoLite2-City.mmdb.gz`` files and unzip them in a directory corresponding to
the :setting:`GEOIP_PATH` setting.
Additionally, it is recommended to install the `libmaxminddb C library`__, so Additionally, it is recommended to install the `libmaxminddb C library`__, so
that ``geoip2`` can leverage the C library's faster speed. that ``geoip2`` can leverage the C library's faster speed.
@ -20,8 +21,13 @@ that ``geoip2`` can leverage the C library's faster speed.
__ https://geoip2.readthedocs.io/ __ https://geoip2.readthedocs.io/
__ https://pypi.org/project/geoip2/ __ https://pypi.org/project/geoip2/
__ https://dev.maxmind.com/geoip/geolite2-free-geolocation-data __ https://dev.maxmind.com/geoip/geolite2-free-geolocation-data
__ https://db-ip.com/db/lite.php
__ https://github.com/maxmind/libmaxminddb/ __ https://github.com/maxmind/libmaxminddb/
.. versionchanged:: 4.2
Support for ``.mmdb`` files downloaded from DB-IP was added.
Example Example
======= =======

View File

@ -60,6 +60,9 @@ Minor features
* The :class:`~django.contrib.gis.gdal.GDALRaster` class now supports * The :class:`~django.contrib.gis.gdal.GDALRaster` class now supports
:class:`pathlib.Path`. :class:`pathlib.Path`.
* The :class:`~django.contrib.gis.geoip2.GeoIP2` class now supports ``.mmdb``
files downloaded from DB-IP.
:mod:`django.contrib.messages` :mod:`django.contrib.messages`
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~