osiris/osiris/settings.py
2026-07-17 18:04:30 +02:00

192 lines
5.8 KiB
Python

"""
Django settings for osiris project.
Generated by 'django-admin startproject' using Django 6.0.7.
For more information on this file, see
https://docs.djangoproject.com/en/6.0/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/6.0/ref/settings/
"""
import os
import sys
from pathlib import Path
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get(
"DJANGO_SECRET_KEY",
"django-insecure-&fochdl--0ywn6%mk3d!ac2o8c4-w6ck=-f&l-w)j361prk31-",
)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DJANGO_DEBUG", "1") == "1"
ALLOWED_HOSTS = os.environ.get(
"DJANGO_ALLOWED_HOSTS", "127.0.0.1,localhost,0.0.0.0,osiris.legrems.xyz"
).split(",")
# Needed when reaching the admin from your phone over the LAN or a tunnel.
CSRF_TRUSTED_ORIGINS = [
o
for o in os.environ.get(
"DJANGO_CSRF_TRUSTED_ORIGINS", "https://osiris.legrems.xyz"
).split(",")
if o
]
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django_vite",
"accounts",
"tracker",
]
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
# Directly below SecurityMiddleware and above everything else, per WhiteNoise's
# docs: it serves /static/ itself so no nginx/S3 is needed in front of Django.
"whitenoise.middleware.WhiteNoiseMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
]
ROOT_URLCONF = "osiris.urls"
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [BASE_DIR / "templates"],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
WSGI_APPLICATION = "osiris.wsgi.application"
# Database
# https://docs.djangoproject.com/en/6.0/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": BASE_DIR / "db.sqlite3",
}
}
# Password validation
# https://docs.djangoproject.com/en/6.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",
},
]
# Internationalization
# https://docs.djangoproject.com/en/6.0/topics/i18n/
LANGUAGE_CODE = "en-us"
TIME_ZONE = os.environ.get("DJANGO_TIME_ZONE", "Europe/Zurich")
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/6.0/howto/static-files/
STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "static"
# PWA icons live in assets/; frontend/dist is the Vite build output. STATIC_ROOT
# is only the collectstatic destination.
STATICFILES_DIRS = [BASE_DIR / "assets", BASE_DIR / "frontend" / "dist"]
# The Vue frontend, served through django-vite's template tags. With
# DJANGO_VITE_DEV=1 the tags point at the Vite dev server (make front-dev) for
# hot reload; otherwise they resolve hashed files from the built manifest.
DJANGO_VITE = {
"default": {
"dev_mode": os.environ.get("DJANGO_VITE_DEV", "0") == "1",
"manifest_path": BASE_DIR / "frontend" / "dist" / ".vite" / "manifest.json",
}
}
STORAGES = {
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
"staticfiles": {
# Hashes each file's contents into its name and gzip/brotli-compresses it,
# so WhiteNoise can serve it with a far-future cache header.
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
if "test" in sys.argv:
# The manifest storage refuses to serve anything collectstatic hasn't seen,
# which would make every template that names a static file fail under test.
STORAGES["staticfiles"] = {
"BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"
}
# Default primary key field type
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
AUTH_USER_MODEL = "accounts.User"
# The admin is the web UI; there is no separate login page and no registration.
LOGIN_URL = "/admin/login/"
LOGIN_REDIRECT_URL = "/admin/"
# Keep the phone logged in for a year instead of re-prompting every session.
SESSION_COOKIE_AGE = 365 * 24 * 60 * 60
SESSION_SAVE_EVERY_REQUEST = True
# Web Push (medication reminders). Generate the pair once with:
# uv run python manage.py generate_vapid_keys
# then put both values in the environment. Without them, reminders are simply
# disabled — the rest of the app works untouched.
VAPID_PUBLIC_KEY = os.environ.get("VAPID_PUBLIC_KEY", "")
VAPID_PRIVATE_KEY = os.environ.get("VAPID_PRIVATE_KEY", "")
# Push services require a contact address for the key owner.
VAPID_CLAIM_EMAIL = os.environ.get("VAPID_CLAIM_EMAIL", "flamelegrems@gmail.com")