Ticket #18349: django_ipv6_geoip.2.patch

File django_ipv6_geoip.2.patch, 3.9 KB (added by j@…, 3 years ago)

new version of patch, adding a test

  • django/contrib/gis/geoip/base.py

    diff --git a/django/contrib/gis/geoip/base.py b/django/contrib/gis/geoip/base.py
    index e00e0a4..96fcc7a 100644
    a b import re 
    33from ctypes import c_char_p
    44
    55from django.core.validators import ipv4_re
     6from django.utils.ipv6 import is_valid_ipv6_address
    67from django.contrib.gis.geoip.libgeoip import GEOIP_SETTINGS
    78from django.contrib.gis.geoip.prototypes import (
    89    GeoIPRecord, GeoIPTag, GeoIP_open, GeoIP_delete, GeoIP_database_info,
    class GeoIP(object): 
    4647
    4748    # Paths to the city & country binary databases.
    4849    _city_file = ''
     50    _city6_file = ''
    4951    _country_file = ''
     52    _country6_file = ''
    5053
    5154    # Initially, pointers to GeoIP file references are NULL.
    5255    _city = None
     56    _city6 = None
    5357    _country = None
     58    _country6 = None
    5459
    5560    def __init__(self, path=None, cache=0, country=None, city=None):
    5661        """
    class GeoIP(object): 
    98103                self._country = GeoIP_open(country_db, cache)
    99104                self._country_file = country_db
    100105
     106            country6_db = os.path.join(path, country or GEOIP_SETTINGS.get('GEOIP_COUNTRY6', 'GeoIPv6.dat'))
     107            if os.path.isfile(country6_db):
     108                self._country6 = GeoIP_open(country6_db, cache)
     109                self._country6_file = country6_db
     110
    101111            city_db = os.path.join(path, city or GEOIP_SETTINGS.get('GEOIP_CITY', 'GeoLiteCity.dat'))
    102112            if os.path.isfile(city_db):
    103113                self._city = GeoIP_open(city_db, cache)
    104114                self._city_file = city_db
     115
     116            city6_db = os.path.join(path, city or GEOIP_SETTINGS.get('GEOIP_CITY6', 'GeoLiteCityv6.dat'))
     117            if os.path.isfile(city6_db):
     118                self._city6 = GeoIP_open(city6_db, cache)
     119                self._city6_file = city6_db
    105120        elif os.path.isfile(path):
    106121            # Otherwise, some detective work will be needed to figure
    107122            # out whether the given database path is for the GeoIP country
    class GeoIP(object): 
    124139    def __del__(self):
    125140        # Cleaning any GeoIP file handles lying around.
    126141        if self._country: GeoIP_delete(self._country)
     142        if self._country6: GeoIP_delete(self._country6)
    127143        if self._city: GeoIP_delete(self._city)
     144        if self._city6: GeoIP_delete(self._city6)
    128145
    129146    def _check_query(self, query, country=False, city=False, city_or_country=False):
    130147        "Helper routine for checking the query and database availability."
    class GeoIP(object): 
    156173        if ipv4_re.match(query):
    157174            # If an IP address was passed in
    158175            return GeoIP_record_by_addr(self._city, c_char_p(query))
     176        elif is_valid_ipv6_address(query):
     177            # If an IPv6 address was passed in
     178            return GeoIP_record_by_addr(self._city6, c_char_p(query))
    159179        else:
    160180            # If a FQDN was passed in.
    161181            return GeoIP_record_by_name(self._city, c_char_p(query))
    class GeoIP(object): 
    166186        if self._country:
    167187            if ipv4_re.match(query):
    168188                return GeoIP_country_code_by_addr(self._country, query)
     189            elif is_valid_ipv6_address(query):
     190                return GeoIP_country_code_by_addr(self._country6, query)
    169191            else:
    170192                return GeoIP_country_code_by_name(self._country, query)
    171193        else:
  • django/contrib/gis/geoip/tests.py

    diff --git a/django/contrib/gis/geoip/tests.py b/django/contrib/gis/geoip/tests.py
    index a629d86..6ff86e5 100644
    a b class GeoIPTest(unittest.TestCase): 
    107107        d = g.country(u'whitehouse.gov')
    108108        self.assertEqual(u'US', d['country_code'])
    109109
     110    def test07_ipv6_query(self):
     111        "Testing that GeoIP can lookup ipv6 addresses."
     112        g = GeoIP()
     113        d = g.city('2a03:2880:2110:3f01:face:b00c::')
     114        self.assertNotEqual(d, None)
    110115
    111116def suite():
    112117    s = unittest.TestSuite()
Back to Top