From 4b6bcbb9f3955b60b4d6c1be0e56f2903e89e22f Mon Sep 17 00:00:00 2001 From: Luca Date: Fri, 10 May 2024 00:33:22 +0200 Subject: [PATCH] refactor(settings.py): use django-environ [CI SKIP] --- .dockerignore | 1 + .env.example | 14 +++++++++ README.md | 2 ++ requirements.txt | 1 + shiftregister/settings.py | 61 ++++++++++++++++++++------------------- 5 files changed, 50 insertions(+), 29 deletions(-) create mode 100644 .env.example diff --git a/.dockerignore b/.dockerignore index f9a628e..a593984 100644 --- a/.dockerignore +++ b/.dockerignore @@ -3,6 +3,7 @@ .dockerignore .drone.yml .editorconfig +.env* .git* .idea Dockerfile diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..b2578c2 --- /dev/null +++ b/.env.example @@ -0,0 +1,14 @@ +#ALLOWED_HOSTS= +#DATABASE_URL= +ENVIRONMENT=production +#SECRET_KEY= +#SENTRY_DSN= + +#CELERY_BROKER_URL= +#CELERY_RESULT_BACKEND= + +#FALLBACK_DEACTIVATE_INTERVAL= +#MESSAGE_RECEIVE_INTERVAL= +#MESSAGE_SEND_INTERVAL= +#REMINDER_SEND_INTERVAL= +#SHIFT_IMPORT_INTERVAL= diff --git a/README.md b/README.md index 5b763eb..ea94b9b 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ * `python -m venv env` * `. env/bin/activate` * `pip install -r requirements.txt` +* `cp .env.example .env` +* `sed -i '/^ENVIRONMENT=/c ENVIRONMENT=development' .env` * `./manage.py migrate` * `./manage.py createsuperuser` * `./manage.py runserver` diff --git a/requirements.txt b/requirements.txt index 528ff53..32fa1bd 100644 --- a/requirements.txt +++ b/requirements.txt @@ -13,6 +13,7 @@ click-repl==0.2.0 Deprecated==1.2.13 Django==5.0.4 django-dynamic-preferences==1.16.0 +django-environ==0.11.2 django-phonenumber-field==7.3.0 icalendar==5.0.12 idna==3.3 diff --git a/shiftregister/settings.py b/shiftregister/settings.py index 86b5833..169b8f8 100644 --- a/shiftregister/settings.py +++ b/shiftregister/settings.py @@ -10,30 +10,38 @@ For the full list of settings and their values, see https://docs.djangoproject.com/en/4.0/ref/settings/ """ -from os import getenv from pathlib import Path +import environ import sentry_sdk from django.contrib.messages import constants as messages from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.django import DjangoIntegration +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.0/howto/deployment/checklist/ +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = env("ENVIRONMENT") == "development" + # SECURITY WARNING: keep the secret key used in production secret! -SECRET_KEY = getenv( - "SECRET_KEY", "django-insecure-pdgzgd_!w&&cfqc%r&!v_^6pgf!sza=2wim67()!(kaf7_6-5)" +SECRET_KEY = env( + "SECRET_KEY", + default=( + "django-insecure-pdgzgd_!w&&cfqc%r&!v_^6pgf!sza=2wim67()!(kaf7_6-5)" + if DEBUG + else env.NOTSET + ), ) -# SECURITY WARNING: don't run with debug turned on in production! -DEBUG = getenv("ENVIRONMENT", "development") == "development" - -ALLOWED_HOSTS = list(filter(lambda s: s != "", getenv("ALLOWED_HOSTS", "").split(","))) +ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[]) # Application definition @@ -100,14 +108,7 @@ WSGI_APPLICATION = "shiftregister.wsgi.application" # https://docs.djangoproject.com/en/4.0/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'}"), } @@ -153,38 +154,40 @@ STATIC_URL = "static/" DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" -CELERY_BROKER_URL = getenv("CELERY_BROKER_URL", "amqp://guest:guest@localhost:5672//") +CELERY_BROKER_URL = env.url( + "CELERY_BROKER_URL", default="amqp://guest:guest@localhost:5672//" +) -CELERY_RESULT_BACKEND = getenv("CELERY_RESULT_BACKEND", "redis://") +CELERY_RESULT_BACKEND = env.url("CELERY_RESULT_BACKEND", default="redis://") CELERY_BEAT_SCHEDULE = { "import-shifts-every-60-seconds": { "task": "shiftregister.importer.tasks.import_shifts", - "schedule": float(getenv("SHIFT_IMPORT_INTERVAL", 60.0)), # seconds + "schedule": env.float("SHIFT_IMPORT_INTERVAL", default=60.0), # seconds }, "send-messages-every-120-seconds": { "task": "shiftregister.app.tasks.send_messages", - "schedule": float(getenv("MESSAGE_SEND_INTERVAL", 120.0)), # seconds + "schedule": env.float("MESSAGE_SEND_INTERVAL", default=120.0), # seconds }, "send-reminders-every-300-seconds": { "task": "shiftregister.app.tasks.send_reminders", - "schedule": float(getenv("REMINDER_SEND_INTERVAL", 300.0)), # seconds + "schedule": env.float("REMINDER_SEND_INTERVAL", default=300.0), # seconds }, "receive-messages-every-300-seconds": { "task": "shiftregister.team.tasks.receive_messages", - "schedule": float(getenv("MESSAGE_RECEIVE_INTERVAL", 300.0)), # seconds + "schedule": env.float("MESSAGE_RECEIVE_INTERVAL", default=300.0), # seconds }, "deactivate-fallbacks-every-300-seconds": { "task": "shiftregister.fallback.tasks.deactivate_fallbacks", - "schedule": float(getenv("FALLBACK_DEACTIVATE_INTERVAL", 300.0)), # seconds + "schedule": env.float("FALLBACK_DEACTIVATE_INTERVAL", default=300.0), # seconds }, } CELERY_BEAT_SCHEDULE_FILENAME = str(BASE_DIR / "storage" / "celerybeat-schedule") -if getenv("SENTRY_DSN"): +if env("SENTRY_DSN", default=None): sentry_sdk.init( - dsn=getenv("SENTRY_DSN"), + dsn=env.url("SENTRY_DSN"), integrations=[CeleryIntegration(), DjangoIntegration()], auto_session_tracking=False, traces_sample_rate=0, @@ -196,12 +199,12 @@ MESSAGE_TAGS = { messages.ERROR: "danger", } -SIPGATE_SMS_EXTENSION = getenv("SIPGATE_SMS_EXTENSION") +SIPGATE_SMS_EXTENSION = env("SIPGATE_SMS_EXTENSION", default=None) -SIPGATE_TOKEN_ID = getenv("SIPGATE_TOKEN_ID") +SIPGATE_TOKEN_ID = env("SIPGATE_TOKEN_ID", default=None) -SIPGATE_TOKEN = getenv("SIPGATE_TOKEN") +SIPGATE_TOKEN = env("SIPGATE_TOKEN", default=None) -SIPGATE_INCOMING_TOKEN_ID = getenv("SIPGATE_INCOMING_TOKEN_ID") +SIPGATE_INCOMING_TOKEN_ID = env("SIPGATE_INCOMING_TOKEN_ID", default=None) -SIPGATE_INCOMING_TOKEN = getenv("SIPGATE_INCOMING_TOKEN") +SIPGATE_INCOMING_TOKEN = env("SIPGATE_INCOMING_TOKEN", default=None)