ucast/ucast_project/settings.py
2022-07-04 12:10:40 +02:00

236 lines
6.2 KiB
Python

"""
Django settings for ucast project.
Generated by 'django-admin startproject' using Django 4.0.4.
For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""
import os
import time
from importlib import resources
from pathlib import Path
import dotenv
VAR_PREFIX = "UCAST_"
def get_env(name, default=None):
val_raw = os.environ.get(VAR_PREFIX + name, default)
if default is not None:
return type(default)(val_raw)
return val_raw
def get_env_path(name, default=None):
raw_env = get_env(name)
if not raw_env:
folder = default
else:
folder = Path(raw_env).absolute()
os.makedirs(folder, exist_ok=True)
return folder
def get_env_list(name):
raw_env = get_env(name)
if not raw_env:
return []
return [i.strip() for i in raw_env.split(",")]
def _load_dotenv() -> Path:
"""
Look for a .env file in the current working directory or
its parent directories.
The directory containing the .env file becomes the application
working directory, if no ``UCAST_WORKDIR`` environment variable
is present.
:return: Application working directory
"""
dotenv_path = dotenv.find_dotenv()
default_workdir = Path().resolve()
if dotenv_path:
dotenv.load_dotenv(dotenv_path)
default_workdir = Path(dotenv_path).resolve().parent
os.chdir(default_workdir)
env_workdir = get_env("WORKDIR")
if env_workdir:
env_workdir_path = Path(env_workdir).resolve()
os.makedirs(env_workdir_path, exist_ok=True)
os.chdir(env_workdir_path)
return env_workdir_path
return default_workdir
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = _load_dotenv()
# SECURITY WARNING: keep the secret key used in production secret!
# generate with openssl rand -base64 64
SECRET_KEY = get_env(
"SECRET_KEY", "django-insecure-$b25pq-s+(_zx2!2$+i+^0$kft0&y3kwmj7j5a#d_jop)$d061"
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = get_env("DEBUG", False)
ALLOWED_HOSTS = get_env_list("ALLOWED_HOSTS")
# Application definition
INSTALLED_APPS = [
"ucast",
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"bulma",
"django_htmx",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.locale.LocaleMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"django_htmx.middleware.HtmxMiddleware",
]
ROOT_URLCONF = "ucast_project.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
"ucast.template_context",
],
},
},
]
WSGI_APPLICATION = "ucast_project.wsgi.application"
def _get_db_config() -> dict:
db_name = get_env("DB_NAME", "db")
db_engine = get_env("DB_ENGINE", "sqlite")
if db_engine == "sqlite":
return {
"ENGINE": "django.db.backends.sqlite3",
"NAME": DB_DIR / f"{db_name}.sqlite",
"OPTIONS": {
"timeout": 20,
},
}
db_port = get_env("DB_PORT")
if not db_port:
if db_engine == "postgresql":
db_port = "5432"
elif db_engine == "mysql":
db_port = "3306"
return {
"ENGINE": f"django.db.backends.{db_engine}",
"NAME": db_name,
"USER": get_env("DB_USER"),
"PASSWORD": get_env("DB_PASSWORD"),
"HOST": get_env("DB_HOST", "127.0.0.1"),
"PORT": db_port,
}
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/
STATIC_URL = "static/"
STATIC_ROOT = get_env_path("STATIC_ROOT", BASE_DIR / "static")
DOWNLOAD_ROOT = get_env_path("DOWNLOAD_ROOT", BASE_DIR / "data")
CACHE_ROOT = get_env_path("CACHE_ROOT", BASE_DIR / "cache")
DB_DIR = get_env_path("DB_DIR", BASE_DIR / "db")
STATICFILES_DIRS = [resources.path("ucast", "static")]
# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases
DATABASES = {
"default": _get_db_config(),
}
# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators
AUTH_PASSWORD_VALIDATORS = [
{
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
},
{
"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator",
},
{
"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator",
},
{
"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator",
},
]
AUTH_USER_MODEL = "ucast.user"
LOGIN_REDIRECT_URL = "/"
LOGOUT_REDIRECT_URL = "/"
# Internationalization
# https://docs.djangoproject.com/en/4.0/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = get_env("TZ", time.tzname[0])
USE_I18N = True
USE_TZ = True
# Default primary key field type
# https://docs.djangoproject.com/en/4.0/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
REDIS_URL = get_env("REDIS_URL", "redis://localhost:6379")
REDIS_QUEUE_TIMEOUT = get_env("REDIS_QUEUE_TIMEOUT", 600)
REDIS_QUEUE_RESULT_TTL = 600
YT_UPDATE_INTERVAL = get_env("YT_UPDATE_INTERVAL", 900)
FEED_MAX_ITEMS = get_env("FEED_MAX_ITEMS", 100)
INTERNAL_FILES_ROOT = get_env("INTERNAL_FILES_ROOT", "internal_files")
INTERNAL_REDIRECT_HEADER = get_env("INTERNAL_REDIRECT_HEADER", "X-Accel-Redirect")