Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Change settings to switchable django backends
Jakub Draganek9a84718c47ba
diff --git a/saleor/search/backends/picker.py b/saleor/search/backends/picker.pyindex c431c3309a..e2d559dd5d 100644--- a/saleor/search/backends/picker.py+++ b/saleor/search/backends/picker.py@@ -1,13 +1,7 @@from __future__ import unicode_literals+import importlibfrom django.conf import settings-from . import (elasticsearch, elasticsearch_dashboard, postgresql,- postgresql_dashboard)---def elastic_configured():- '''Evaluate elasticsearch configuration status'''- return settings.ES_URL and not settings.PREFER_DB_SEARCHdef pick_backend():@@ -16,9 +10,9 @@ def pick_backend()::rtype: callable with one argument of type str'''- if elastic_configured():- return elasticsearch.search- return postgresql.search+ backend = importlib.import_module(+ settings.SEARCH_BACKENDS[settings.SEARCH_BACKEND]['storefront'])+ return backend.searchdef pick_dashboard_backend():@@ -27,6 +21,6 @@ def pick_dashboard_backend()::rtype: callable with one argument of type str'''- if elastic_configured():- return elasticsearch_dashboard.search- return postgresql_dashboard.search+ backend = importlib.import_module(+ settings.SEARCH_BACKENDS[settings.SEARCH_BACKEND]['dashboard'])+ return backend.searchdiff --git a/saleor/search/backends/test_picker.py b/saleor/search/backends/test_picker.pydeleted file mode 100644index e733a0c5cd..0000000000--- a/saleor/search/backends/test_picker.py+++ /dev/null@@ -1,31 +0,0 @@-from __future__ import unicode_literals--from .picker import pick_backend, pick_dashboard_backend-from . import (postgresql, postgresql_dashboard, elasticsearch_dashboard,- elasticsearch)---POSTGRES_STOREFRONT_BACKEND = postgresql.search-POSTGRES_DASHBOARD_BACKEND = postgresql_dashboard.search-ELASTIC_STOREFRONT_BACKEND = elasticsearch.search-ELASTIC_DASHBOARD_BACKEND = elasticsearch_dashboard.search---def test_picks_db(settings):- settings.ES_URL = None- assert pick_backend() is POSTGRES_STOREFRONT_BACKEND- assert pick_dashboard_backend() is POSTGRES_DASHBOARD_BACKEND---def test_pick_elastic_when_configured(settings):- settings.ES_URL = 'whatever'- settings.PREFER_DB_SEARCH = False- assert pick_backend() is ELASTIC_STOREFRONT_BACKEND- assert pick_dashboard_backend() is ELASTIC_DASHBOARD_BACKEND---def test_pick_db_when_preferred(settings):- settings.ES_URL = 'whatever'- settings.PREFER_DB_SEARCH = True- assert pick_backend() is POSTGRES_STOREFRONT_BACKEND- assert pick_dashboard_backend() is POSTGRES_DASHBOARD_BACKENDdiff --git a/saleor/settings.py b/saleor/settings.pyindex 56a6ef9e4b..0bf6ca2dc7 100644--- a/saleor/settings.py+++ b/saleor/settings.py@@ -359,7 +359,6 @@ LOGOUT_ON_PASSWORD_CHANGE = False# SEARCH CONFIGURATIONDB_SEARCH_ENABLED = True-PREFER_DB_SEARCH = False # prefer use of db backend over elasticsearch# support deployment-dependant elastic enviroment variableES_URL = (os.environ.get('ELASTICSEARCH_URL') or@@ -367,7 +366,21 @@ ES_URL = (os.environ.get('ELASTICSEARCH_URL') orENABLE_SEARCH = bool(ES_URL) or DB_SEARCH_ENABLED # global search disabling+SEARCH_BACKENDS = {+ 'default': {+ 'storefront': 'saleor.search.backends.postgresql',+ 'dashboard': 'saleor.search.backends.postgresql_dashboard'+ },+ 'elasticsearch': {+ 'storefront': 'saleor.search.backends.elasticsearch',+ 'dashboard': 'saleor.search.backends.elasticsearch_dashboard'+ }+}++SEARCH_BACKEND = 'default'+if ES_URL:+ SEARCH_BACKEND = 'elasticsearch'INSTALLED_APPS.append('django_elasticsearch_dsl')ELASTICSEARCH_DSL = {'default': {diff --git a/tests/test_elasticsearch.py b/tests/test_elasticsearch.pyindex bad737ee47..dacafaf283 100644--- a/tests/test_elasticsearch.py+++ b/tests/test_elasticsearch.py@@ -30,7 +30,7 @@ def elasticsearch_connection():def elasticsearch_enabled(settings):settings.ES_URL = ES_URLsettings.ENABLE_SEARCH = True- settings.PREFER_DB_SEARCH = False+ settings.SEARCH_BACKEND = 'elasticsearch'@pytest.fixture(scope='function', autouse=True)diff --git a/tests/test_postgresql_search.py b/tests/test_postgresql_search.pyindex ae10adef0a..d3d5ef04e6 100644--- a/tests/test_postgresql_search.py+++ b/tests/test_postgresql_search.py@@ -12,9 +12,8 @@ import pytest@pytest.fixture(scope='function', autouse=True)def postgresql_search_enabled(settings):- settings.ELASTICSEARCH_URL = Nonesettings.ENABLE_SEARCH = True- settings.PREFER_DB_SEARCH = True+ settings.SEARCH_BACKEND = 'default'PRODUCTS = [('Arabica Coffee', 'The best grains in galactic'),