Updated configuration using config.toml file instead of environment variables

This commit is contained in:
Edgar P. Burkhart 2022-12-29 17:26:54 +01:00
parent bcbf33984b
commit a06f49c2a5
Signed by: edpibu
GPG Key ID: 9833D3C5A25BD227
3 changed files with 30 additions and 12 deletions

13
config.toml Normal file
View File

@ -0,0 +1,13 @@
secret_key = "dev_sDo6gl1Yf8GIE7XEA2B4xF841eMOpfG1"
debug = false
hosts = ["localhost"]
trusted_origins = ["http://localhost"]
time_zone = "CET"
[media]
root = "/var/lib/nummi"
[databases.default]
ENGINE = "django.db.backends.postgresql"
NAME = "nummi"
USER = "nummi"

View File

@ -1,4 +0,0 @@
NUMMI_MEDIA_ROOT="/var/lib/nummi"
NUMMI_SECRET="xxxxxx"
#NUMMI_DEBUG="True"
NUMMI_HOST="nummi.edgarpierre.fr"

View File

@ -10,12 +10,21 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import tomllib
import os
from pathlib import Path
CONFIG_PATH = os.environ.get("NUMMI_CONFIG", None)
if CONFIG_PATH is None:
CONFIG = dict()
else:
with open(CONFIG_PATH, "rb") as CONFIG_FILE:
CONFIG = tomllib.load(CONFIG_FILE)
# Build paths inside the project like this: BASE_DIR / 'subdir'.
MEDIA_CONF = CONFIG.get("media", {})
BASE_DIR = Path(__file__).resolve().parent.parent
MEDIA_ROOT = Path(os.environ.get("NUMMI_MEDIA_ROOT", "/var/lib/nummi"))
MEDIA_ROOT = Path(MEDIA_CONF.get("root", "/var/lib/nummi"))
MEDIA_URL = "media/"
@ -23,13 +32,13 @@ MEDIA_URL = "media/"
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("NUMMI_SECRET", "dev_sDo6gl1Yf8GIE7XEA2B4xF841eMOpfG1")
SECRET_KEY = CONFIG.get("secret_key", "dev_sDo6gl1Yf8GIE7XEA2B4xF841eMOpfG1")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("NUMMI_DEBUG", None) == "True"
DEBUG = CONFIG.get("debug", False)
ALLOWED_HOSTS = [os.environ.get("NUMMI_HOST", "localhost")]
CSRF_TRUSTED_ORIGINS = [f"https://{os.environ.get('NUMMI_HOST', 'localhost')}"]
ALLOWED_HOSTS = CONFIG.get("hosts", ["localhost"])
CSRF_TRUSTED_ORIGINS = CONFIG.get("trusted_origins", ["http://localhost"])
# Application definition
@ -78,13 +87,13 @@ WSGI_APPLICATION = "nummi.wsgi.application"
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
DATABASES = CONFIG.get("databases", {
"default": {
"ENGINE": "django.db.backends.postgresql",
"NAME": "nummi",
"USER": "nummi",
}
}
})
# Password validation
@ -110,7 +119,7 @@ AUTH_PASSWORD_VALIDATORS = [
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = "fr-fr"
TIME_ZONE = "CET"
TIME_ZONE = CONFIG.get("time_zone", "CET")
USE_I18N = True
USE_TZ = True
LOCALE_PATHS = [