summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2018-06-22 16:20:50 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2018-07-03 14:14:01 (GMT)
commitc37e52875758c634da55f4969c4a0bde54c1a9d6 (patch)
tree762d8c557c7c86ce0e57d5492b34ec7b311c197a
parentcf70b636e0b0ef390ee44618cd4b10ebbca21659 (diff)
downloadhobo-c37e52875758c634da55f4969c4a0bde54c1a9d6.zip
hobo-c37e52875758c634da55f4969c4a0bde54c1a9d6.tar.gz
hobo-c37e52875758c634da55f4969c4a0bde54c1a9d6.tar.bz2
multitenant: implement .extend and .update suffix for key in settings.json (#24700)
-rw-r--r--hobo/multitenant/settings_loaders.py18
-rw-r--r--tests_multitenant/conftest.py7
-rw-r--r--tests_multitenant/settings.py3
-rw-r--r--tests_multitenant/test_settings.py8
4 files changed, 34 insertions, 2 deletions
diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py
index 562a043..34170e8 100644
--- a/hobo/multitenant/settings_loaders.py
+++ b/hobo/multitenant/settings_loaders.py
@@ -291,11 +291,27 @@ class SiteBaseUrl(object):
class SettingsJSON(FileBaseSettingsLoader):
FILENAME = 'settings.json'
+ def do_update(self, tenant_settings, key, value):
+ old_value = getattr(tenant_settings, key, {})
+ new_value = old_value.copy()
+ new_value.update(value)
+ setattr(tenant_settings, key, new_value)
+
+ def do_extend(self, tenant_settings, key, value):
+ new_value = list(getattr(tenant_settings, key, []))
+ new_value.extend(value)
+ setattr(tenant_settings, key, new_value)
+
def update_settings_from_path(self, tenant_settings, path):
with open(path) as f:
json_settings = json.load(f)
for key in json_settings:
- setattr(tenant_settings, key, json_settings[key])
+ if '.' in key:
+ real_key, op = key.rsplit('.', 1)
+ method = getattr(self, 'do_%s' % op)
+ method(tenant_settings, real_key, json_settings[key])
+ else:
+ setattr(tenant_settings, key, json_settings[key])
class DictAdapter(dict):
diff --git a/tests_multitenant/conftest.py b/tests_multitenant/conftest.py
index 77b326f..b550dad 100644
--- a/tests_multitenant/conftest.py
+++ b/tests_multitenant/conftest.py
@@ -11,6 +11,7 @@ def tenants(transactional_db, request, settings):
from hobo.multitenant.models import Tenant
base = tempfile.mkdtemp('combo-tenant-base')
settings.TENANT_BASE = base
+
@pytest.mark.django_db
def make_tenant(name):
tenant_dir = os.path.join(base, name)
@@ -18,7 +19,11 @@ def tenants(transactional_db, request, settings):
with open(os.path.join(tenant_dir, 'unsecure'), 'w') as fd:
fd.write('1')
with open(os.path.join(tenant_dir, 'settings.json'), 'w') as fd:
- json.dump({'HOBO_TEST_VARIABLE': name}, fd)
+ json.dump({
+ 'HOBO_TEST_VARIABLE': name,
+ 'UPDATE_ME.update': {'y': 2},
+ 'EXTEND_ME.extend': [2],
+ }, fd)
with open(os.path.join(tenant_dir, 'hobo.json'), 'w') as fd:
json.dump({
'variables': {
diff --git a/tests_multitenant/settings.py b/tests_multitenant/settings.py
index dcbf8c3..4c6a7fb 100644
--- a/tests_multitenant/settings.py
+++ b/tests_multitenant/settings.py
@@ -57,3 +57,6 @@ STATIC_URL = '/static/'
MELLON_IDENTITY_PROVIDERS = []
LOGGING = None
+
+UPDATE_ME = {'x': 1}
+EXTEND_ME = [1]
diff --git a/tests_multitenant/test_settings.py b/tests_multitenant/test_settings.py
index e432102..8e20333 100644
--- a/tests_multitenant/test_settings.py
+++ b/tests_multitenant/test_settings.py
@@ -32,16 +32,24 @@ def test_tenant_json_settings(tenants, settings):
# check the setting is not defined
with pytest.raises(AttributeError):
settings.HOBO_TEST_VARIABLE
+ assert django.conf.settings.UPDATE_ME == {'x': 1}
+ assert django.conf.settings.EXTEND_ME == [1]
+
# check that for each tenant it contains the tenant domain
# it's set by the tenants fixture in conftest.py
for tenant in tenants:
with tenant_context(tenant):
assert django.conf.settings.HOBO_TEST_VARIABLE == tenant.domain_url
+ assert django.conf.settings.UPDATE_ME == {'x': 1, 'y': 2}
+ assert django.conf.settings.EXTEND_ME == [1, 2]
# check it's no longer defined after going back to the public schema
with pytest.raises(AttributeError):
settings.HOBO_TEST_VARIABLE
+ assert django.conf.settings.UPDATE_ME == {'x': 1}
+ assert django.conf.settings.EXTEND_ME == [1]
+
def test_tenant_template_vars(tenants, settings, client):
from hobo.multitenant.models import Tenant