Skip to content

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

Add currency middleware

Michał Ociepka75cf8df03b30
diff --git a/saleor/core/__init__.py b/saleor/core/__init__.py
index ec32ac5994..3a087f3480 100644
--- a/saleor/core/__init__.py
+++ b/saleor/core/__init__.py
@@ -1,5 +1,7 @@
from __future__ import unicode_literals
+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
@@ -12,3 +14,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)
+
+
+def get_currency_for_country(country):
+ currencies = get_territory_currencies(country.code)
+ if len(currencies):
+ main_currency = currencies[0]
+ if main_currency in settings.AVAILABLE_CURRENCIES:
+ return main_currency
+ return settings.DEFAULT_CURRENCY
diff --git a/saleor/core/middleware.py b/saleor/core/middleware.py
index 171dca1533..3eaef95d25 100644
--- a/saleor/core/middleware.py
+++ b/saleor/core/middleware.py
@@ -3,7 +3,7 @@ import logging
from django.conf import settings
from django.utils.translation import get_language
-from . import analytics, get_country_by_ip
+from . import analytics, get_country_by_ip, get_currency_for_country
from ..product.models import Discount
logger = logging.getLogger(__name__)
diff --git a/saleor/core/test_core.py b/saleor/core/test_core.py
index eafa0e2027..f61fabd411 100644
--- a/saleor/core/test_core.py
+++ b/saleor/core/test_core.py
@@ -1,7 +1,7 @@
from mock import Mock
import pytest
-from . import get_country_by_ip, Country
+from . import get_country_by_ip, get_currency_for_country, Country
@pytest.mark.parametrize('lookup, country', [
@@ -12,3 +12,17 @@ def test_get_country_by_ip(lookup, country, monkeypatch):
monkeypatch.setattr('saleor.core.geolite2.lookup', lookup)
country = get_country_by_ip('127.0.0.1')
assert country == country
+
+
[email protected]('settings, country, expected_currency', [
+ (Mock(AVAILABLE_CURRENCIES=['GBP', 'PLN'], DEFAULT_CURRENCY='PLN'),
+ Country('PL'), 'PLN'),
+ (Mock(AVAILABLE_CURRENCIES=['GBP', 'PLN'], DEFAULT_CURRENCY='PLN'),
+ Country('USA'), 'PLN'),
+ (Mock(AVAILABLE_CURRENCIES=['GBP', 'PLN'], DEFAULT_CURRENCY='PLN'),
+ Country('GB'), 'GBP')
+])
+def test_get_currency_for_country(settings, country, expected_currency, monkeypatch):
+ monkeypatch.setattr('saleor.core.settings', settings)
+ currency = get_currency_for_country(country)
+ assert currency == expected_currency
diff --git a/saleor/settings.py b/saleor/settings.py
index 791f86b4e3..684ea0c47d 100644
--- a/saleor/settings.py
+++ b/saleor/settings.py
@@ -111,6 +111,7 @@ MIDDLEWARE_CLASSES = [
'saleor.core.middleware.DiscountMiddleware',
'saleor.core.middleware.GoogleAnalytics',
'saleor.core.middleware.CountryMiddleware',
+ 'saleor.core.middleware.CurrencyMiddleware'
]
INSTALLED_APPS = [
@@ -208,6 +209,7 @@ AUTH_USER_MODEL = 'userprofile.User'
LOGIN_URL = '/account/login'
DEFAULT_CURRENCY = 'USD'
+AVAILABLE_CURRENCIES = [DEFAULT_CURRENCY]
DEFAULT_WEIGHT = 'lb'
ACCOUNT_ACTIVATION_DAYS = 3