import os from pathlib import Path from dotenv import load_dotenv load_dotenv() BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-key-shiftplan-change-me-in-production') DEBUG = os.environ.get('DEBUG', 'True') == 'True' ALLOWED_HOSTS = [h.strip() for h in os.environ.get('ALLOWED_HOSTS', '*').split(',') if h.strip()] # Reverse Proxy & SSL Configuration for Coolify / Traefik SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') USE_X_FORWARDED_HOST = True USE_X_FORWARDED_PORT = True # CSRF & Trusted Origins Configuration CSRF_TRUSTED_ORIGINS = [ 'http://localhost', 'http://127.0.0.1', 'https://*.op3n.link', 'http://*.op3n.link', ] raw_csrf = os.environ.get('CSRF_TRUSTED_ORIGINS', '') if raw_csrf: for origin in raw_csrf.split(','): o = origin.strip() if o and o not in CSRF_TRUSTED_ORIGINS: CSRF_TRUSTED_ORIGINS.append(o) for h in ALLOWED_HOSTS: if h and h != '*': if not h.startswith('http://') and not h.startswith('https://'): http_origin = f'http://{h}' https_origin = f'https://{h}' if http_origin not in CSRF_TRUSTED_ORIGINS: CSRF_TRUSTED_ORIGINS.append(http_origin) if https_origin not in CSRF_TRUSTED_ORIGINS: CSRF_TRUSTED_ORIGINS.append(https_origin) INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', # Third party 'rest_framework', 'rest_framework.authtoken', 'corsheaders', # Local apps 'apps.users', 'apps.events', 'apps.branding', ] AUTH_USER_MODEL = 'users.User' MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', '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 = 'shiftplan_backend.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', ], }, }, ] WSGI_APPLICATION = 'shiftplan_backend.wsgi.application' # Database configuration: Postgres with SQLite fallback DB_ENGINE = os.environ.get('POSTGRES_DB', None) if DB_ENGINE: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': os.environ.get('POSTGRES_DB', 'shiftplan_db'), 'USER': os.environ.get('POSTGRES_USER', 'shiftplan_user'), 'PASSWORD': os.environ.get('POSTGRES_PASSWORD', 'shiftplan_password'), 'HOST': os.environ.get('POSTGRES_HOST', 'db'), 'PORT': os.environ.get('POSTGRES_PORT', '5432'), } } else: DATABASES = { 'default': { 'ENGINE': 'django.db.backends.sqlite3', 'NAME': BASE_DIR / 'db.sqlite3', } } 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'}, ] LANGUAGE_CODE = 'de-de' TIME_ZONE = 'Europe/Berlin' USE_I18N = True USE_TZ = True STATIC_URL = '/static/' STATIC_ROOT = BASE_DIR / 'staticfiles' STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage' DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField' REST_FRAMEWORK = { 'DEFAULT_AUTHENTICATION_CLASSES': [ 'rest_framework.authentication.TokenAuthentication', 'rest_framework.authentication.SessionAuthentication', ], 'DEFAULT_PERMISSION_CLASSES': [ 'rest_framework.permissions.IsAuthenticatedOrReadOnly', ], } CORS_ALLOW_ALL_ORIGINS = True CORS_ALLOW_CREDENTIALS = True