summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorBenjamin Dauvergne <bdauvergne@entrouvert.com>2018-12-18 17:42:44 (GMT)
committerBenjamin Dauvergne <bdauvergne@entrouvert.com>2019-01-14 09:36:04 (GMT)
commit8b45ce4986e67284a0e293b69e4d5e297f77da5b (patch)
treefec83bcb4a0946ce1195688d0614e028849ef219
parent605e2f4bfedacf9350f3d7a43e90dbfd8585611c (diff)
downloadhobo-8b45ce4986e67284a0e293b69e4d5e297f77da5b.zip
hobo-8b45ce4986e67284a0e293b69e4d5e297f77da5b.tar.gz
hobo-8b45ce4986e67284a0e293b69e4d5e297f77da5b.tar.bz2
hobo: add setting loader for variables (#28930)
Variables must be prefixed with SETTING_, suffix .extend and .update are supported. Ex.: SETTING_LDAP_AUTH_SETTINGS.extend = [{...}]
-rw-r--r--debian/debian_config_common.py1
-rw-r--r--hobo/multitenant/settings_loaders.py32
-rw-r--r--tests_multitenant/conftest.py12
-rw-r--r--tests_multitenant/settings.py9
-rw-r--r--tests_multitenant/test_settings.py28
5 files changed, 81 insertions, 1 deletions
diff --git a/debian/debian_config_common.py b/debian/debian_config_common.py
index faca508..68f86e3 100644
--- a/debian/debian_config_common.py
+++ b/debian/debian_config_common.py
@@ -295,6 +295,7 @@ if PROJECT_NAME != 'wcs':
'hobo.multitenant.settings_loaders.SiteBaseUrl',
'hobo.multitenant.settings_loaders.CookieNames',
'hobo.multitenant.settings_loaders.SettingsJSON',
+ 'hobo.multitenant.settings_loaders.SettingsVars',
)
# templates in <tenant>/templates
diff --git a/hobo/multitenant/settings_loaders.py b/hobo/multitenant/settings_loaders.py
index d525680..fc21960 100644
--- a/hobo/multitenant/settings_loaders.py
+++ b/hobo/multitenant/settings_loaders.py
@@ -115,6 +115,8 @@ class KnownServices(FileBaseSettingsLoader):
tenant_settings.KNOWN_SERVICES = known_services
+SETTING_PREFIX = 'SETTING_'
+
class TemplateVars(FileBaseSettingsLoader):
FILENAME = 'hobo.json'
@@ -166,7 +168,11 @@ class TemplateVars(FileBaseSettingsLoader):
tenant_settings.TEMPLATE_VARS = tenant_settings.default_settings.TEMPLATE_VARS.copy()
else:
tenant_settings.TEMPLATE_VARS = {}
- tenant_settings.TEMPLATE_VARS.update(variables)
+
+ for key, value in variables.items():
+ # prevent mixing settings and template vars
+ if not key.startswith(SETTING_PREFIX):
+ tenant_settings.TEMPLATE_VARS[key] = value
if variables.get('default_from_email'):
if variables.get('global_title'):
@@ -178,6 +184,30 @@ class TemplateVars(FileBaseSettingsLoader):
tenant_settings.USER_PROFILE_CONFIG = hobo_json.get('profile')
+class SettingsVars(SettingsDictUpdateMixin, FileBaseSettingsLoader):
+ FILENAME = 'hobo.json'
+
+ @classmethod
+ def get_hobo_json_variables(cls, hobo_json):
+ variables = hobo_json.get('variables', {})
+ for service in hobo_json.get('services'):
+ if service.get('this'):
+ variables.update(service.get('variables') or {})
+ break
+ return variables
+
+ def update_settings_from_path(self, tenant_settings, path):
+ with open(path) as f:
+ hobo_json = json.load(f)
+ variables = self.get_hobo_json_variables(hobo_json)
+
+ settings_dict = {}
+ for key, value in variables.items():
+ if key.startswith(SETTING_PREFIX):
+ settings_dict[key[len(SETTING_PREFIX):]] = value
+ self.handle_settings(tenant_settings, settings_dict)
+
+
class CORSSettings(FileBaseSettingsLoader):
FILENAME = 'hobo.json'
diff --git a/tests_multitenant/conftest.py b/tests_multitenant/conftest.py
index 6ae101d..07bc0c9 100644
--- a/tests_multitenant/conftest.py
+++ b/tests_multitenant/conftest.py
@@ -30,6 +30,12 @@ def tenants(transactional_db, request, settings):
'hobo_test_variable': True,
'other_variable': 'foo',
'theme': 'publik',
+ 'SETTING_GLOBAL1': True,
+ 'SETTING_GLOBAL2.extend': [2, 3, 4],
+ 'SETTING_GLOBAL3.update': {"x": 1, "y": 2},
+ 'SETTING_OVERRIDE1': False,
+ 'SETTING_OVERRIDE2.extend': [6, 7, 8],
+ 'SETTING_OVERRIDE3.update': {"a": 1, "b": 2},
},
'services': [
{'slug': 'test',
@@ -41,6 +47,12 @@ def tenants(transactional_db, request, settings):
'saml-sp-metadata-url': 'http://%s/saml/metadata' % name,
'variables': {
'other_variable': 'bar',
+ 'SETTING_OVERRIDE1': True,
+ 'SETTING_OVERRIDE2.extend': [name, 7, 8],
+ 'SETTING_OVERRIDE3.update': {"a": name, "b": 2},
+ 'SETTING_LOCAL1': False,
+ 'SETTING_LOCAL2.extend': [name, 7, 8],
+ 'SETTING_LOCAL3.update': {"a": name, "b": 2},
}
},
{'slug': 'slug-with-hyphen',
diff --git a/tests_multitenant/settings.py b/tests_multitenant/settings.py
index 6a43a32..239bdf3 100644
--- a/tests_multitenant/settings.py
+++ b/tests_multitenant/settings.py
@@ -72,4 +72,13 @@ TENANT_SETTINGS_LOADERS = (
'hobo.multitenant.settings_loaders.SiteBaseUrl',
'hobo.multitenant.settings_loaders.CookieNames',
'hobo.multitenant.settings_loaders.SettingsJSON',
+ 'hobo.multitenant.settings_loaders.SettingsVars',
)
+
+GLOBAL1 = 0
+GLOBAL2 = [1, 2, 3]
+GLOBAL3 = {"z": 1}
+
+OVERRIDE1 = 0
+OVERRIDE2 = [1, 2, 3]
+OVERRIDE3 = {"z": 1}
diff --git a/tests_multitenant/test_settings.py b/tests_multitenant/test_settings.py
index 30279b7..f982661 100644
--- a/tests_multitenant/test_settings.py
+++ b/tests_multitenant/test_settings.py
@@ -80,6 +80,34 @@ def test_tenant_template_vars(tenants, settings, client):
with pytest.raises(AttributeError):
django.conf.settings.TEMPLATE_VARS
+def test_tenant_settings_vars(tenants, settings, client):
+ from hobo.multitenant.models import Tenant
+
+ django.conf.settings.clear_tenants_settings()
+
+ with utilities.patch_default_settings(settings,
+ TENANT_SETTINGS_LOADERS=('hobo.multitenant.settings_loaders.SettingsVars',)):
+ # check the setting is not defined
+ with pytest.raises(AttributeError):
+ django.conf.settings.LOCAL1
+ with pytest.raises(AttributeError):
+ django.conf.settings.LOCAL2
+ with pytest.raises(AttributeError):
+ django.conf.settings.LOCAL3
+
+ for tenant in tenants:
+ with tenant_context(tenant):
+ # check it's defined when moving into the schema
+ assert django.conf.settings.GLOBAL1 is True
+ assert django.conf.settings.GLOBAL2 == [1, 2, 3, 2, 3, 4]
+ assert django.conf.settings.GLOBAL3 == {'x': 1, 'y': 2, 'z': 1}
+ assert django.conf.settings.OVERRIDE1 is True
+ assert django.conf.settings.OVERRIDE2 == [1, 2, 3, tenant.domain_url, 7, 8]
+ assert django.conf.settings.OVERRIDE3 == {'a': tenant.domain_url, 'b': 2, 'z': 1}
+ assert django.conf.settings.LOCAL1 is False
+ assert django.conf.settings.LOCAL2 == [tenant.domain_url, 7, 8]
+ assert django.conf.settings.LOCAL3 == {'a': tenant.domain_url, 'b': 2}
+
def test_tenant_cors_settings(tenants, settings, client):
settings.clear_tenants_settings()