Use django-environ for settings

This commit is contained in:
Luca 2023-10-11 16:14:21 +02:00
parent 204a35100d
commit b23082e79a
4 changed files with 45 additions and 22 deletions

11
.env.example Normal file
View File

@ -0,0 +1,11 @@
ALLOWED_HOSTS=
APP_URL=
DATABASE_URL=
DEBUG=no
SECRET_KEY=
OIDC_OP_AUTHORIZATION_ENDPOINT=
OIDC_OP_TOKEN_ENDPOINT=
OIDC_OP_USER_ENDPOINT=
OIDC_RP_CLIENT_ID=
OIDC_RP_CLIENT_SECRET=

View File

@ -10,26 +10,28 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.2/ref/settings/
"""
from os import getenv
from pathlib import Path
from sys import argv
import environ
env = environ.Env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
environ.Env.read_env(BASE_DIR / ".env")
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = getenv(
"SECRET_KEY", "django-insecure-av_^u=+63kru%wed(9&1gx0=_8sci@2ohd$b@l-!$u*p0xtik-"
)
SECRET_KEY = env("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = getenv("DEBUG", "").lower() in ("1", "true", "yes") or "runserver" in argv
DEBUG = env.bool("DEBUG", False)
ALLOWED_HOSTS = [] if DEBUG else getenv("ALLOWED_HOSTS", "ljg.sh,www.ljg.sh").split(",")
ALLOWED_HOSTS = [] if DEBUG else env.list("ALLOWED_HOSTS")
# Application definition
@ -83,14 +85,7 @@ WSGI_APPLICATION = "ljg.wsgi.application"
# https://docs.djangoproject.com/en/4.2/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends." + getenv("DB_ENGINE", "sqlite3"),
"NAME": getenv("DB_NAME", BASE_DIR / "db.sqlite3"),
"USER": getenv("DB_USER", ""),
"PASSWORD": getenv("DB_PASSWORD", ""),
"HOST": getenv("DB_HOST", ""),
"PORT": getenv("DB_PORT", ""),
}
"default": env.db_url(default=f"sqlite:///{BASE_DIR / 'db.sqlite3'}"),
}
@ -135,16 +130,16 @@ STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
APP_TITLE = getenv("APP_TITLE", "ljg.sh")
APP_TITLE = env.str("APP_TITLE", "ljg.sh")
AUTHENTICATION_BACKENDS = (
"django.contrib.auth.backends.ModelBackend",
"ljg.core.auth.OIDCAuthenticationBackend",
)
OIDC_OP_AUTHORIZATION_ENDPOINT = getenv("OIDC_OP_AUTHORIZATION_ENDPOINT", "")
OIDC_OP_TOKEN_ENDPOINT = getenv("OIDC_OP_TOKEN_ENDPOINT", "")
OIDC_OP_USER_ENDPOINT = getenv("OIDC_OP_USER_ENDPOINT", "")
OIDC_OP_AUTHORIZATION_ENDPOINT = env.str("OIDC_OP_AUTHORIZATION_ENDPOINT", "")
OIDC_OP_TOKEN_ENDPOINT = env.str("OIDC_OP_TOKEN_ENDPOINT", "")
OIDC_OP_USER_ENDPOINT = env.str("OIDC_OP_USER_ENDPOINT", "")
OIDC_RP_CLIENT_ID = getenv("OIDC_RP_CLIENT_ID", "")
OIDC_RP_CLIENT_SECRET = getenv("OIDC_RP_CLIENT_SECRET", "")
OIDC_RP_CLIENT_ID = env.str("OIDC_RP_CLIENT_ID", "")
OIDC_RP_CLIENT_SECRET = env.str("OIDC_RP_CLIENT_SECRET", "")

20
poetry.lock generated
View File

@ -1,4 +1,4 @@
# This file is automatically @generated by Poetry 1.5.1 and should not be changed by hand.
# This file is automatically @generated by Poetry 1.6.1 and should not be changed by hand.
[[package]]
name = "asgiref"
@ -319,6 +319,22 @@ tzdata = {version = "*", markers = "sys_platform == \"win32\""}
argon2 = ["argon2-cffi (>=19.1.0)"]
bcrypt = ["bcrypt"]
[[package]]
name = "django-environ"
version = "0.11.2"
description = "A package that allows you to utilize 12factor inspired environment variables to configure your Django application."
optional = false
python-versions = ">=3.6,<4"
files = [
{file = "django-environ-0.11.2.tar.gz", hash = "sha256:f32a87aa0899894c27d4e1776fa6b477e8164ed7f6b3e410a62a6d72caaf64be"},
{file = "django_environ-0.11.2-py2.py3-none-any.whl", hash = "sha256:0ff95ab4344bfeff693836aa978e6840abef2e2f1145adff7735892711590c05"},
]
[package.extras]
develop = ["coverage[toml] (>=5.0a4)", "furo (>=2021.8.17b43,<2021.9.dev0)", "pytest (>=4.6.11)", "sphinx (>=3.5.0)", "sphinx-notfound-page"]
docs = ["furo (>=2021.8.17b43,<2021.9.dev0)", "sphinx (>=3.5.0)", "sphinx-notfound-page"]
testing = ["coverage[toml] (>=5.0a4)", "pytest (>=4.6.11)"]
[[package]]
name = "idna"
version = "3.4"
@ -729,4 +745,4 @@ zstd = ["zstandard (>=0.18.0)"]
[metadata]
lock-version = "2.0"
python-versions = "^3.11"
content-hash = "27fbc85e1a784623c3bf3a0cab6e2a4af4e10634075d105ba149b46bcc9fb229"
content-hash = "bb962aa113b939926a9a66e86694f1b8b279c541e1c0ac38f6e9f3b10248d5d6"

View File

@ -14,6 +14,7 @@ mozilla-django-oidc = "^3.0.0"
Pillow = "^10.0.0"
psycopg2-binary = "^2.9.7"
qrcode = "^7.4.2"
django-environ = "^0.11.2"
[tool.poetry.group.dev.dependencies]
black = "^23.7.0"