2
0
Fork 0

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

This commit is contained in:
Luca 2024-05-10 00:33:22 +02:00
parent d4234279ff
commit 4b6bcbb9f3
5 changed files with 50 additions and 29 deletions

View File

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

14
.env.example Normal file
View File

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

View File

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

View File

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

View File

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