refactor(settings.py): use django-environ [CI SKIP]

pull/1/head
Luca 8 months ago
parent d4234279ff
commit 4b6bcbb9f3

@ -3,6 +3,7 @@
.dockerignore .dockerignore
.drone.yml .drone.yml
.editorconfig .editorconfig
.env*
.git* .git*
.idea .idea
Dockerfile Dockerfile

@ -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=

@ -11,6 +11,8 @@
* `python -m venv env` * `python -m venv env`
* `. env/bin/activate` * `. env/bin/activate`
* `pip install -r requirements.txt` * `pip install -r requirements.txt`
* `cp .env.example .env`
* `sed -i '/^ENVIRONMENT=/c ENVIRONMENT=development' .env`
* `./manage.py migrate` * `./manage.py migrate`
* `./manage.py createsuperuser` * `./manage.py createsuperuser`
* `./manage.py runserver` * `./manage.py runserver`

@ -13,6 +13,7 @@ click-repl==0.2.0
Deprecated==1.2.13 Deprecated==1.2.13
Django==5.0.4 Django==5.0.4
django-dynamic-preferences==1.16.0 django-dynamic-preferences==1.16.0
django-environ==0.11.2
django-phonenumber-field==7.3.0 django-phonenumber-field==7.3.0
icalendar==5.0.12 icalendar==5.0.12
idna==3.3 idna==3.3

@ -10,30 +10,38 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/ https://docs.djangoproject.com/en/4.0/ref/settings/
""" """
from os import getenv
from pathlib import Path from pathlib import Path
import environ
import sentry_sdk import sentry_sdk
from django.contrib.messages import constants as messages from django.contrib.messages import constants as messages
from sentry_sdk.integrations.celery import CeleryIntegration from sentry_sdk.integrations.celery import CeleryIntegration
from sentry_sdk.integrations.django import DjangoIntegration from sentry_sdk.integrations.django import DjangoIntegration
env = environ.Env()
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
environ.Env.read_env(BASE_DIR / ".env")
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/ # 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! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = getenv( SECRET_KEY = env(
"SECRET_KEY", "django-insecure-pdgzgd_!w&&cfqc%r&!v_^6pgf!sza=2wim67()!(kaf7_6-5)" "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! ALLOWED_HOSTS = env.list("ALLOWED_HOSTS", default=[])
DEBUG = getenv("ENVIRONMENT", "development") == "development"
ALLOWED_HOSTS = list(filter(lambda s: s != "", getenv("ALLOWED_HOSTS", "").split(",")))
# Application definition # Application definition
@ -100,14 +108,7 @@ WSGI_APPLICATION = "shiftregister.wsgi.application"
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases # https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = { DATABASES = {
"default": { "default": env.db_url(default=f"sqlite:///{BASE_DIR / 'db.sqlite3'}"),
"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", ""),
}
} }
@ -153,38 +154,40 @@ STATIC_URL = "static/"
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" 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 = { CELERY_BEAT_SCHEDULE = {
"import-shifts-every-60-seconds": { "import-shifts-every-60-seconds": {
"task": "shiftregister.importer.tasks.import_shifts", "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": { "send-messages-every-120-seconds": {
"task": "shiftregister.app.tasks.send_messages", "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": { "send-reminders-every-300-seconds": {
"task": "shiftregister.app.tasks.send_reminders", "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": { "receive-messages-every-300-seconds": {
"task": "shiftregister.team.tasks.receive_messages", "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": { "deactivate-fallbacks-every-300-seconds": {
"task": "shiftregister.fallback.tasks.deactivate_fallbacks", "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") CELERY_BEAT_SCHEDULE_FILENAME = str(BASE_DIR / "storage" / "celerybeat-schedule")
if getenv("SENTRY_DSN"): if env("SENTRY_DSN", default=None):
sentry_sdk.init( sentry_sdk.init(
dsn=getenv("SENTRY_DSN"), dsn=env.url("SENTRY_DSN"),
integrations=[CeleryIntegration(), DjangoIntegration()], integrations=[CeleryIntegration(), DjangoIntegration()],
auto_session_tracking=False, auto_session_tracking=False,
traces_sample_rate=0, traces_sample_rate=0,
@ -196,12 +199,12 @@ MESSAGE_TAGS = {
messages.ERROR: "danger", 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)

Loading…
Cancel
Save