Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Replace python-geoip by maxminddb

Michał Ociepka9f7251e6c727
diff --git a/requirements.in b/requirements.in
index 88b3f29282..fcce2fef11 100644
--- a/requirements.in
+++ b/requirements.in
@@ -20,9 +20,9 @@ django-versatileimagefield>=1.0.1
fake-factory>=0.3.2
google-measurement-protocol>=0.1.2,<0.2a0
jsonfield>=1.0.3
-python-geoip==1.2
-python-geoip-geolite2==2015.303
Markdown>=2.4
+maxminddb==1.2.0
+maxminddb-geolite2==2015.708
mock>=1.3.0
prices>=0.5.5,<0.6a0
psycopg2>=2.6
diff --git a/requirements.txt b/requirements.txt
index 025c5ad4ff..0a187789c2 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -39,6 +39,8 @@ ipaddress==1.0.16 # via cryptography
jsonfield==1.0.3
lxml==3.5.0 # via premailer
markdown==2.6.5
+maxminddb==1.2.0
+maxminddb-geolite2==2015.708
mock==1.3.0
pbr==1.8.1 # via mock
pillow==3.1.0 # via django-versatileimagefield
@@ -52,8 +54,6 @@ pycparser==2.14 # via cffi
pyjwt==1.4.0 # via django-payments
pytest-django==2.9.1
pytest==2.8.7
-python-geoip-geolite2==2015.303
-python-geoip==1.2
pytz==2015.7 # via babel
redis==2.10.5 # via django-redis
requests==2.9.1
diff --git a/saleor/core/__init__.py b/saleor/core/__init__.py
index 3a087f3480..5c3cdf49c9 100644
--- a/saleor/core/__init__.py
+++ b/saleor/core/__init__.py
@@ -4,16 +4,20 @@ from babel.numbers import get_territory_currencies
from django.conf import settings
from django_countries import countries
from django_countries.fields import Country
-from geoip import geolite2
+from geolite2 import geolite2
TOKEN_PATTERN = ('(?P<token>[0-9a-z]{8}-[0-9a-z]{4}-[0-9a-z]{4}-[0-9a-z]{4}'
'-[0-9a-z]{12})')
def get_country_by_ip(ip_address):
- geo_data = geolite2.lookup(ip_address)
- if geo_data and geo_data.country in countries:
- return Country(geo_data.country)
+ reader = geolite2.reader()
+ geo_data = reader.get(ip_address)
+ geolite2.close()
+ if geo_data and 'country' in geo_data and 'iso_code' in geo_data['country']:
+ country_iso_code = geo_data['country']['iso_code']
+ if country_iso_code in countries:
+ return Country(country_iso_code)
def get_currency_for_country(country):
diff --git a/saleor/core/test_core.py b/saleor/core/test_core.py
index f61fabd411..09f003633b 100644
--- a/saleor/core/test_core.py
+++ b/saleor/core/test_core.py
@@ -4,14 +4,19 @@ import pytest
from . import get_country_by_ip, get_currency_for_country, Country
[email protected]('lookup, country', [
- (Mock(return_value=Mock(country='PL')), Country('PL')),
- (Mock(return_value=Mock(country='UNKNOWN')), None),
[email protected]('reader, expected_country', [
+ (Mock(return_value=Mock(get=Mock(return_value={
+ 'country': {'iso_code': 'PL'}}))), Country('PL')),
+ (Mock(return_value=Mock(get=Mock(return_value={
+ 'country': {'iso_code': 'UNKNOWN'}}))), None),
+ (Mock(return_value=Mock(get=Mock(return_value=None))), None),
+ (Mock(return_value=Mock(get=Mock(return_value={}))), None),
+ (Mock(return_value=Mock(get=Mock(return_value={'country': {}}))), None),
])
-def test_get_country_by_ip(lookup, country, monkeypatch):
- monkeypatch.setattr('saleor.core.geolite2.lookup', lookup)
+def test_get_country_by_ip(reader, expected_country, monkeypatch):
+ monkeypatch.setattr('saleor.core.geolite2.reader', reader)
country = get_country_by_ip('127.0.0.1')
- assert country == country
+ assert country == expected_country
@pytest.mark.parametrize('settings, country, expected_currency', [