From 11e8fe891229d14db53dccfd9cd3faef8d09d860 Mon Sep 17 00:00:00 2001 From: Kumi Date: Fri, 12 Jul 2024 08:41:33 +0200 Subject: [PATCH] feat(settings): enhance configuration handling Updated settings to ensure "ColdBrew" section exists in the config before accessing it. Defaulted `FIELD_ENCRYPTION_KEY` to a generated value if not present and corrected encoding. Made `ALLOWED_HOSTS` configurable via settings file and adjusted `DEBUG` logic to depend on `ALLOWED_HOSTS`. --- coldbrew/settings.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/coldbrew/settings.py b/coldbrew/settings.py index fd7b7c1..bbc27d3 100644 --- a/coldbrew/settings.py +++ b/coldbrew/settings.py @@ -23,15 +23,18 @@ BASE_DIR = Path(__file__).resolve().parent.parent ASK = AutoSecretKey("settings.ini") SECRET_KEY = ASK.secret_key CONFIG = ASK.config -DEBUG = CONFIG.getboolean("ColdBrew", "Debug", fallback=False) -if not (FIELD_ENCRYPTION_KEY := CONFIG.get("ColdBrew", "EncryptionKey")): - FIELD_ENCRYPTION_KEY = base64.urlsafe_b64encode(os.urandom(32)) +if "ColdBrew" not in CONFIG: + CONFIG["ColdBrew"] = {} + +if not (FIELD_ENCRYPTION_KEY := CONFIG.get("ColdBrew", "EncryptionKey", fallback=None)): + FIELD_ENCRYPTION_KEY = base64.urlsafe_b64encode(os.urandom(32)).decode() CONFIG["ColdBrew"]["EncryptionKey"] = FIELD_ENCRYPTION_KEY ASK.write() -ALLOWED_HOSTS = [] +ALLOWED_HOSTS = CONFIG.get("ColdBrew", "AllowedHosts", fallback="*").split(",") +DEBUG = CONFIG.getboolean("ColdBrew", "Debug", fallback=False) if ALLOWED_HOSTS else True # Application definition