Move to environs[django]

This commit is contained in:
Jan Dittberner 2024-04-13 13:18:22 +02:00
parent a3feb92503
commit f3ef40df0b

View file

@ -6,9 +6,12 @@ Common settings and globals.
""" """
from os.path import abspath, basename, dirname, join, normpath from os.path import abspath, basename, dirname, join, normpath
from environs import Env
from django.contrib.messages import constants as messages from django.contrib.messages import constants as messages
from gvacommon.settings_utils import get_env_variable
env = Env()
env.read_env()
# ######### PATH CONFIGURATION # ######### PATH CONFIGURATION
# Absolute filesystem path to the Django project directory: # Absolute filesystem path to the Django project directory:
@ -17,12 +20,14 @@ DJANGO_ROOT = dirname(dirname(abspath(__file__)))
# Absolute filesystem path to the top-level project folder: # Absolute filesystem path to the top-level project folder:
SITE_ROOT = dirname(DJANGO_ROOT) SITE_ROOT = dirname(DJANGO_ROOT)
ROOT_DIR = dirname(DJANGO_ROOT)
# Site name: # Site name:
SITE_NAME = basename(DJANGO_ROOT) SITE_NAME = basename(DJANGO_ROOT)
# ######### END PATH CONFIGURATION # ######### END PATH CONFIGURATION
GVA_ENVIRONMENT = get_env_variable("GVA_ENVIRONMENT", default="prod") GVA_ENVIRONMENT = env.str("GVA_ENVIRONMENT", default="prod")
# ######### DEBUG CONFIGURATION # ######### DEBUG CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#debug # See: https://docs.djangoproject.com/en/dev/ref/settings/#debug
@ -34,8 +39,8 @@ DEBUG = GVA_ENVIRONMENT == "local"
# See: https://docs.djangoproject.com/en/dev/ref/settings/#admins # See: https://docs.djangoproject.com/en/dev/ref/settings/#admins
ADMINS = ( ADMINS = (
( (
get_env_variable("GVA_ADMIN_NAME", default="Admin"), env.str("GVA_ADMIN_NAME", default="Admin"),
get_env_variable("GVA_ADMIN_EMAIL", default="admin@example.org"), env.str("GVA_ADMIN_EMAIL", default="admin@example.org"),
), ),
) )
@ -47,14 +52,7 @@ MANAGERS = ADMINS
# ######### DATABASE CONFIGURATION # ######### DATABASE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#databases # See: https://docs.djangoproject.com/en/dev/ref/settings/#databases
DATABASES = { DATABASES = {
"default": { "default": env.dj_db_url("GVA_DATABASE_URL"),
"ENGINE": "django.db.backends.postgresql",
"NAME": get_env_variable("GVA_PGSQL_DATABASE", default="gnuviechadmin"),
"USER": get_env_variable("GVA_PGSQL_USER", default="gnuviechadmin"),
"PASSWORD": get_env_variable("GVA_PGSQL_PASSWORD"),
"HOST": get_env_variable("GVA_PGSQL_HOSTNAME", default="db"),
"PORT": get_env_variable("GVA_PGSQL_PORT", int, default=5432),
}
} }
DEFAULT_AUTO_FIELD = "django.db.models.AutoField" DEFAULT_AUTO_FIELD = "django.db.models.AutoField"
@ -70,8 +68,8 @@ LANGUAGE_CODE = "en-us"
# See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id # See: https://docs.djangoproject.com/en/dev/ref/settings/#site-id
SITE_ID = 1 SITE_ID = 1
SITES_DOMAIN_NAME = get_env_variable("GVA_DOMAIN_NAME") SITES_DOMAIN_NAME = env.str("GVA_DOMAIN_NAME")
SITES_SITE_NAME = get_env_variable("GVA_SITE_NAME") SITES_SITE_NAME = env.str("GVA_SITE_NAME")
# See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n # See: https://docs.djangoproject.com/en/dev/ref/settings/#use-i18n
USE_I18N = True USE_I18N = True
@ -109,7 +107,7 @@ STATICFILES_FINDERS = (
# ######### SECRET CONFIGURATION # ######### SECRET CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key # See: https://docs.djangoproject.com/en/dev/ref/settings/#secret-key
# Note: This key should only be used for development and testing. # Note: This key should only be used for development and testing.
SECRET_KEY = get_env_variable("GVA_SITE_SECRET") SECRET_KEY = env.str("GVA_SITE_SECRET")
# ######### END SECRET CONFIGURATION # ######### END SECRET CONFIGURATION
@ -164,7 +162,6 @@ MIDDLEWARE = [
"allauth.account.middleware.AccountMiddleware", "allauth.account.middleware.AccountMiddleware",
"django.middleware.locale.LocaleMiddleware", "django.middleware.locale.LocaleMiddleware",
"django.contrib.messages.middleware.MessageMiddleware", "django.contrib.messages.middleware.MessageMiddleware",
# uncomment next line to enable translation to browser locale
"django.middleware.clickjacking.XFrameOptionsMiddleware", "django.middleware.clickjacking.XFrameOptionsMiddleware",
] ]
# ######### END MIDDLEWARE CONFIGURATION # ######### END MIDDLEWARE CONFIGURATION
@ -236,6 +233,7 @@ LOCAL_APPS = (
"help", "help",
"invoices", "invoices",
"contact_form", "contact_form",
"exit",
) )
# See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps # See: https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps
@ -309,7 +307,7 @@ LOGGING = {
"logfile": { "logfile": {
"level": "INFO", "level": "INFO",
"class": "logging.FileHandler", "class": "logging.FileHandler",
"filename": get_env_variable("GVA_LOG_FILE", default="gva.log"), "filename": env.str("GVA_LOG_FILE", default="gva.log"),
"formatter": "verbose", "formatter": "verbose",
}, },
"mail_admins": { "mail_admins": {
@ -352,7 +350,7 @@ WSGI_APPLICATION = "%s.wsgi.application" % SITE_NAME
# ######### CELERY CONFIGURATION # ######### CELERY CONFIGURATION
BROKER_URL = get_env_variable( BROKER_URL = env.str(
"GVA_BROKER_URL", default="amqp://gnuviechadmin:gnuviechadmin@mq/gnuviechadmin" "GVA_BROKER_URL", default="amqp://gnuviechadmin:gnuviechadmin@mq/gnuviechadmin"
) )
BROKER_TRANSPORT_OPTIONS = { BROKER_TRANSPORT_OPTIONS = {
@ -361,7 +359,7 @@ BROKER_TRANSPORT_OPTIONS = {
"interval_step": 0.2, "interval_step": 0.2,
"interval_max": 0.2, "interval_max": 0.2,
} }
CELERY_RESULT_BACKEND = get_env_variable( CELERY_RESULT_BACKEND = env.str(
"GVA_RESULTS_REDIS_URL", default="redis://:gnuviechadmin@redis:6379/0" "GVA_RESULTS_REDIS_URL", default="redis://:gnuviechadmin@redis:6379/0"
) )
CELERY_TASK_RESULT_EXPIRES = None CELERY_TASK_RESULT_EXPIRES = None
@ -375,32 +373,32 @@ CELERY_RESULT_SERIALIZER = "json"
# ######### CUSTOM APP CONFIGURATION # ######### CUSTOM APP CONFIGURATION
OSUSER_MINUID = get_env_variable("GVA_MIN_OS_UID", int, default=10000) OSUSER_MINUID = env.int("GVA_MIN_OS_UID", default=10000)
OSUSER_MINGID = get_env_variable("GVA_MIN_OS_GID", int, default=10000) OSUSER_MINGID = env.int("GVA_MIN_OS_GID", default=10000)
OSUSER_USERNAME_PREFIX = get_env_variable("GVA_OSUSER_PREFIX", default="usr") OSUSER_USERNAME_PREFIX = env.str("GVA_OSUSER_PREFIX", default="usr")
OSUSER_HOME_BASEPATH = get_env_variable("GVA_OSUSER_HOME_BASEPATH", default="/home") OSUSER_HOME_BASEPATH = env.str("GVA_OSUSER_HOME_BASEPATH", default="/home")
OSUSER_DEFAULT_SHELL = get_env_variable( OSUSER_DEFAULT_SHELL = env.str(
"GVA_OSUSER_DEFAULT_SHELL", default="/usr/bin/rssh" "GVA_OSUSER_DEFAULT_SHELL", default="/usr/bin/rssh"
) )
OSUSER_SFTP_GROUP = "sftponly" OSUSER_SFTP_GROUP = "sftponly"
OSUSER_SSH_GROUP = "sshusers" OSUSER_SSH_GROUP = "sshusers"
OSUSER_DEFAULT_GROUPS = [OSUSER_SFTP_GROUP] OSUSER_DEFAULT_GROUPS = [OSUSER_SFTP_GROUP]
OSUSER_UPLOAD_SERVER = get_env_variable("GVA_OSUSER_UPLOADSERVER", default="file") OSUSER_UPLOAD_SERVER = env.str("GVA_OSUSER_UPLOADSERVER", default="file")
GVA_LINK_WEBMAIL = get_env_variable( GVA_LINK_WEBMAIL = env.str(
"GVA_WEBMAIL_URL", default="https://webmail.example.org/" "GVA_WEBMAIL_URL", default="https://webmail.example.org/"
) )
GVA_LINK_PHPMYADMIN = get_env_variable( GVA_LINK_PHPMYADMIN = env.str(
"GVA_PHPMYADMIN_URL", default="https://phpmyadmin.example.org/" "GVA_PHPMYADMIN_URL", default="https://phpmyadmin.example.org/"
) )
GVA_LINK_PHPPGADMIN = get_env_variable( GVA_LINK_PHPPGADMIN = env.str(
"GVA_PHPPGADMIN_URL", default="https://phppgadmin.example.org/" "GVA_PHPPGADMIN_URL", default="https://phppgadmin.example.org/"
) )
# ######### END CUSTOM APP CONFIGURATION # ######### END CUSTOM APP CONFIGURATION
# ######### STATIC FILE CONFIGURATION # ######### STATIC FILE CONFIGURATION
# See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root # See: https://docs.djangoproject.com/en/dev/ref/settings/#static-root
STATIC_ROOT = "/srv/gva/static/" STATIC_ROOT = env.str("GVA_STATIC_PATH", default=normpath(join(ROOT_DIR, "static")))
def show_debug_toolbar(request): def show_debug_toolbar(request):
@ -495,12 +493,12 @@ else:
EMAIL_SUBJECT_PREFIX = "[%s] " % SITE_NAME EMAIL_SUBJECT_PREFIX = "[%s] " % SITE_NAME
# See: https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email # See: https://docs.djangoproject.com/en/dev/ref/settings/#default-from-email
DEFAULT_FROM_EMAIL = get_env_variable( DEFAULT_FROM_EMAIL = env.str(
"GVA_SITE_ADMINMAIL", default="admin@example.org" "GVA_SITE_ADMINMAIL", default="admin@example.org"
) )
# See: https://docs.djangoproject.com/en/dev/ref/settings/#server-email # See: https://docs.djangoproject.com/en/dev/ref/settings/#server-email
SERVER_EMAIL = get_env_variable("GVA_SITE_ADMINMAIL", default="admin@example.org") SERVER_EMAIL = env.str("GVA_SITE_ADMINMAIL", default="admin@example.org")
# ######### END EMAIL CONFIGURATION # ######### END EMAIL CONFIGURATION
# ######### CACHE CONFIGURATION # ######### CACHE CONFIGURATION