Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Replace python-geoip by maxminddb
Michał Ociepka9f7251e6c727
diff --git a/requirements.in b/requirements.inindex 88b3f29282..fcce2fef11 100644--- a/requirements.in+++ b/requirements.in@@ -20,9 +20,9 @@ django-versatileimagefield>=1.0.1fake-factory>=0.3.2google-measurement-protocol>=0.1.2,<0.2a0jsonfield>=1.0.3-python-geoip==1.2-python-geoip-geolite2==2015.303Markdown>=2.4+maxminddb==1.2.0+maxminddb-geolite2==2015.708mock>=1.3.0prices>=0.5.5,<0.6a0psycopg2>=2.6diff --git a/requirements.txt b/requirements.txtindex 025c5ad4ff..0a187789c2 100644--- a/requirements.txt+++ b/requirements.txt@@ -39,6 +39,8 @@ ipaddress==1.0.16 # via cryptographyjsonfield==1.0.3lxml==3.5.0 # via premailermarkdown==2.6.5+maxminddb==1.2.0+maxminddb-geolite2==2015.708mock==1.3.0pbr==1.8.1 # via mockpillow==3.1.0 # via django-versatileimagefield@@ -52,8 +54,6 @@ pycparser==2.14 # via cffipyjwt==1.4.0 # via django-paymentspytest-django==2.9.1pytest==2.8.7-python-geoip-geolite2==2015.303-python-geoip==1.2pytz==2015.7 # via babelredis==2.10.5 # via django-redisrequests==2.9.1diff --git a/saleor/core/__init__.py b/saleor/core/__init__.pyindex 3a087f3480..5c3cdf49c9 100644--- a/saleor/core/__init__.py+++ b/saleor/core/__init__.py@@ -4,16 +4,20 @@ from babel.numbers import get_territory_currenciesfrom django.conf import settingsfrom django_countries import countriesfrom django_countries.fields import Country-from geoip import geolite2+from geolite2 import geolite2TOKEN_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.pyindex f61fabd411..09f003633b 100644--- a/saleor/core/test_core.py+++ b/saleor/core/test_core.py@@ -4,14 +4,19 @@ import pytestfrom . 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', [