~ubuntu-branches/debian/sid/python-django/sid

« back to all changes in this revision

Viewing changes to django/contrib/gis/geoip/base.py

  • Committer: Package Import Robot
  • Author(s): Luke Faraone
  • Date: 2013-11-07 15:33:49 UTC
  • mfrom: (1.3.12)
  • Revision ID: package-import@ubuntu.com-20131107153349-e31sc149l2szs3jb
Tags: 1.6-1
* New upstream version. Closes: #557474, #724637.
* python-django now also suggests the installation of ipython,
  bpython, python-django-doc, and libgdal1.
  Closes: #636511, #686333, #704203
* Set package maintainer to Debian Python Modules Team.
* Bump standards version to 3.9.5, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
11
11
    GeoIP_country_name_by_addr, GeoIP_country_name_by_name)
12
12
 
13
13
from django.utils import six
 
14
from django.utils.encoding import force_bytes
14
15
 
15
16
# Regular expressions for recognizing the GeoIP free database editions.
16
17
free_regex = re.compile(r'^GEO-\d{3}FREE')
99
100
            # and/or city datasets exist, then try and open them.
100
101
            country_db = os.path.join(path, country or GEOIP_SETTINGS.get('GEOIP_COUNTRY', 'GeoIP.dat'))
101
102
            if os.path.isfile(country_db):
102
 
                self._country = GeoIP_open(country_db, cache)
 
103
                self._country = GeoIP_open(force_bytes(country_db), cache)
103
104
                self._country_file = country_db
104
105
 
105
106
            city_db = os.path.join(path, city or GEOIP_SETTINGS.get('GEOIP_CITY', 'GeoIPCity.dat'))
106
107
            if os.path.isfile(city_db):
107
 
                self._city = GeoIP_open(city_db, cache)
 
108
                self._city = GeoIP_open(force_bytes(city_db), cache)
108
109
                self._city_file = city_db
109
110
        elif os.path.isfile(path):
110
111
            # Otherwise, some detective work will be needed to figure
111
112
            # out whether the given database path is for the GeoIP country
112
113
            # or city databases.
113
 
            ptr = GeoIP_open(path, cache)
 
114
            ptr = GeoIP_open(force_bytes(path), cache)
114
115
            info = GeoIP_database_info(ptr)
115
116
            if lite_regex.match(info):
116
117
                # GeoLite City database detected.
127
128
 
128
129
    def __del__(self):
129
130
        # Cleaning any GeoIP file handles lying around.
 
131
        if GeoIP_delete is None:
 
132
            return
130
133
        if self._country: GeoIP_delete(self._country)
131
134
        if self._city: GeoIP_delete(self._city)
132
135
 
136
139
        if not isinstance(query, six.string_types):
137
140
            raise TypeError('GeoIP query must be a string, not type %s' % type(query).__name__)
138
141
 
139
 
        # GeoIP only takes ASCII-encoded strings.
140
 
        query = query.encode('ascii')
141
 
 
142
142
        # Extra checks for the existence of country and city databases.
143
143
        if city_or_country and not (self._country or self._city):
144
144
            raise GeoIPException('Invalid GeoIP country and city data files.')
147
147
        elif city and not self._city:
148
148
            raise GeoIPException('Invalid GeoIP city data file: %s' % self._city_file)
149
149
 
150
 
        # Return the query string back to the caller.
151
 
        return query
 
150
        # Return the query string back to the caller. GeoIP only takes bytestrings.
 
151
        return force_bytes(query)
152
152
 
153
153
    def city(self, query):
154
154
        """
156
156
        Fully Qualified Domain Name (FQDN).  Some information in the dictionary
157
157
        may be undefined (None).
158
158
        """
159
 
        query = self._check_query(query, city=True)
 
159
        enc_query = self._check_query(query, city=True)
160
160
        if ipv4_re.match(query):
161
161
            # If an IP address was passed in
162
 
            return GeoIP_record_by_addr(self._city, c_char_p(query))
 
162
            return GeoIP_record_by_addr(self._city, c_char_p(enc_query))
163
163
        else:
164
164
            # If a FQDN was passed in.
165
 
            return GeoIP_record_by_name(self._city, c_char_p(query))
 
165
            return GeoIP_record_by_name(self._city, c_char_p(enc_query))
166
166
 
167
167
    def country_code(self, query):
168
168
        "Returns the country code for the given IP Address or FQDN."
169
 
        query = self._check_query(query, city_or_country=True)
 
169
        enc_query = self._check_query(query, city_or_country=True)
170
170
        if self._country:
171
171
            if ipv4_re.match(query):
172
 
                return GeoIP_country_code_by_addr(self._country, query)
 
172
                return GeoIP_country_code_by_addr(self._country, enc_query)
173
173
            else:
174
 
                return GeoIP_country_code_by_name(self._country, query)
 
174
                return GeoIP_country_code_by_name(self._country, enc_query)
175
175
        else:
176
176
            return self.city(query)['country_code']
177
177
 
178
178
    def country_name(self, query):
179
179
        "Returns the country name for the given IP Address or FQDN."
180
 
        query = self._check_query(query, city_or_country=True)
 
180
        enc_query = self._check_query(query, city_or_country=True)
181
181
        if self._country:
182
182
            if ipv4_re.match(query):
183
 
                return GeoIP_country_name_by_addr(self._country, query)
 
183
                return GeoIP_country_name_by_addr(self._country, enc_query)
184
184
            else:
185
 
                return GeoIP_country_name_by_name(self._country, query)
 
185
                return GeoIP_country_name_by_name(self._country, enc_query)
186
186
        else:
187
187
            return self.city(query)['country_name']
188
188