Skip to content

Commit

Permalink
Merge pull request #5 from loadsmart/change-how-get-constance-settingsa
Browse files Browse the repository at this point in the history
Change how to get constance keys
  • Loading branch information
marlontrapp authored Aug 25, 2021
2 parents a032726 + f0594bd commit c260a88
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,5 @@ The default prefix for flags is `FRONTEND_`, if you like to change it you can do
FRONTEND_SETTINGS = {
"WAFFLE_FLAG_PREFIX": "FRONTEND_", # Prefix used to filter out the flag that should be exposed in the endpoint
"CONSTANCE_KEY": "FRONTEND_SETTINGS", # Name of the settings in constance
"CONSTANCE_KEY_PREFIX": "FRONTEND_", # Prefix used to filter out the settings in constance
}
4 changes: 2 additions & 2 deletions frontend_settings/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

DEFAULTS = {
"WAFFLE_FLAG_PREFIX": "FRONTEND_",
"CONSTANCE_KEY": "FRONTEND_SETTINGS",
"CONSTANCE_KEY_PREFIX": "FRONTEND_",
}

USER_SETTINGS = getattr(dj_settings, "FRONTEND_SETTINGS", None)
Expand All @@ -14,7 +14,7 @@

class APISettings(_APISettings):
WAFFLE_FLAG_PREFIX: str
CONSTANCE_KEY: str
CONSTANCE_KEY_PREFIX: str

@property
def user_settings(self):
Expand Down
7 changes: 4 additions & 3 deletions frontend_settings/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ def get_flags(request: HttpRequest) -> Dict[str, bool]:


def get_settings() -> Dict[str, Any]:
constance_key: str = settings.CONSTANCE_KEY
constance_key_prefix: str = settings.CONSTANCE_KEY_PREFIX
return {
key: getattr(constance_config, key, None)
for key in getattr(constance_config, constance_key, [])
key.replace(constance_key_prefix, ""): getattr(constance_config, key, None)
for key in dir(constance_config)
if key.startswith(constance_key_prefix)
}
13 changes: 8 additions & 5 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@
ALLOWED_HOSTS = ("*",)
CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend"

FRONTEND_SETTINGS = {"WAFFLE_FLAG_PREFIX": "TEST_", "CONSTANCE_KEY": "TEST_FRONTEND"}
FRONTEND_SETTINGS = {
"WAFFLE_FLAG_PREFIX": "TEST_",
"CONSTANCE_KEY_PREFIX": "TEST_FRONTEND_",
}

CONSTANCE_CONFIG = {
"TEST_FRONTEND": (["SETTINGS_AAA", "SETTINGS_BBB", "SETTINGS_CCC"], ""),
"SETTINGS_AAA": ("VALUE AAA", ""),
"SETTINGS_BBB": ("VALUE BBB", ""),
"SETTINGS_CCC": ("VALUE CCC", ""),
"TEST_FRONTEND_SETTING_AAA": ("VALUE AAA", ""),
"TEST_FRONTEND_SETTING_BBB": ("VALUE BBB", ""),
"TEST_FRONTEND_SETTING_CCC": ("VALUE CCC", ""),
"NOT_FRONTEND": ("NOT ON SETTINGS", ""),
}
6 changes: 3 additions & 3 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_return_settings(client):
response = client.get(url)
data = response.data["settings"]

assert data["SETTINGS_AAA"] == "VALUE AAA"
assert data["SETTINGS_BBB"] == "VALUE BBB"
assert data["SETTINGS_CCC"] == "VALUE CCC"
assert data["SETTING_AAA"] == "VALUE AAA"
assert data["SETTING_BBB"] == "VALUE BBB"
assert data["SETTING_CCC"] == "VALUE CCC"
assert "NOT_FRONTEND" not in data

0 comments on commit c260a88

Please sign in to comment.