Skip to content

Commit

Permalink
additional-settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sdc50 committed Mar 21, 2024
1 parent 85f8e74 commit 3c2d94a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
TEST_SETTING = "Test Setting"
24 changes: 24 additions & 0 deletions tests/unit_tests/test_tethys_portal/test_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,27 @@ def test_bokehjsdir_compatibility(self, mock_oi):
reload(settings)
mock_bokeh_settings.bokehjs_path.assert_called_once()
mock_bokeh_settings.bokehjsdir.assert_called_once()

def test_get__all__(self):
expected = '__all__'
mock_mod = mock.MagicMock(__all__=expected)
actual = settings.get__all__(mock_mod)
self.assertEqual(expected, actual)

def test_get__all__error(self):
mock_mod = mock.MagicMock()
actual = settings.get__all__(mock_mod)
expected = [a for a in dir(mock_mod) if not a.startswith('__')]
self.assertListEqual(expected, actual)

@mock.patch(
"tethys_portal.settings.yaml.safe_load",
return_value={
"settings": {
"TETHYS_PORTAL_CONFIG": {"ADDITIONAL_SETTINGS_FILES": ['tethysapp.test_app.additional_settings']}
}
},
)
def test_additional_settings_files(self, _):
reload(settings)
self.assertEqual(settings.TEST_SETTING, "Test Setting")
18 changes: 18 additions & 0 deletions tethys_portal/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import datetime as dt
from pathlib import Path
from importlib import import_module
from importlib.machinery import SourceFileLoader

from django.contrib.messages import constants as message_constants

Expand Down Expand Up @@ -578,3 +579,20 @@
STATIC_URL = f"/{PREFIX_URL}/{STATIC_URL.strip('/')}/"
LOGIN_URL = f"/{PREFIX_URL}/{LOGIN_URL.strip('/')}/"
FIDO_LOGIN_URL = f"/{PREFIX_URL}/{FIDO_LOGIN_URL.strip('/')}/"


def get__all__(mod):
try:
return mod.__all__
except AttributeError:
return [a for a in dir(mod) if not a.startswith('__')]


ADDITIONAL_SETTINGS_FILES = TETHYS_PORTAL_CONFIG.pop("ADDITIONAL_SETTINGS_FILES", [])
for settings_module in ADDITIONAL_SETTINGS_FILES:
mod = SourceFileLoader("mod", settings_module).load_module() \
if Path(settings_module).is_file() else import_module(settings_module)
all_settings = get__all__(mod)
for setting in all_settings:
value = getattr(mod, setting)
setattr(this_module, setting, value)

0 comments on commit 3c2d94a

Please sign in to comment.