Files
thrillwiki_django_no_react/backend/config/django/local.py
2025-08-28 11:37:24 -04:00

236 lines
6.3 KiB
Python

"""
Local development settings for thrillwiki project.
"""
from ..settings import database
import logging
import os
from decouple import config
import re
from .base import (
BASE_DIR,
INSTALLED_APPS,
MIDDLEWARE,
STATIC_ROOT,
STATIC_URL,
ROOT_URLCONF,
AUTH_PASSWORD_VALIDATORS,
AUTH_USER_MODEL,
TEMPLATES,
SECRET_KEY,
SPECTACULAR_SETTINGS,
REST_FRAMEWORK,
)
SECRET_KEY = SECRET_KEY
SPECTACULAR_SETTINGS = SPECTACULAR_SETTINGS
REST_FRAMEWORK = REST_FRAMEWORK
TEMPLATES = TEMPLATES
ROOT_URLCONF = ROOT_URLCONF
AUTH_PASSWORD_VALIDATORS = AUTH_PASSWORD_VALIDATORS
AUTH_USER_MODEL = AUTH_USER_MODEL
STATIC_ROOT = STATIC_ROOT
STATIC_URL = STATIC_URL
# Import database configuration
DATABASES = database.DATABASES
# Development-specific settings
DEBUG = True
# For local development, allow all hosts
ALLOWED_HOSTS = ["*"]
# CSRF trusted origins for local development
CSRF_TRUSTED_ORIGINS = [
"http://localhost:8000",
"http://127.0.0.1:8000",
"https://beta.thrillwiki.com",
]
CORS_ALLOWED_ORIGIN_REGEXES = [
# Matches http://localhost:3000, http://localhost:3001, etc.
r"^http://localhost:\d+$",
# Matches http://127.0.0.1:3000, http://127.0.0.1:8080, etc.
r"^http://127\.0\.0\.1:\d+$",
]
CORS_ALLOW_HEADERS = [
'accept',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'x-nextjs-data', # Next.js specific header
]
if DEBUG:
CORS_ALLOW_ALL_ORIGINS = True # ⚠️ Only for development!
else:
CORS_ALLOW_ALL_ORIGINS = False
GDAL_LIBRARY_PATH = "/opt/homebrew/lib/libgdal.dylib"
GEOS_LIBRARY_PATH = "/opt/homebrew/lib/libgeos_c.dylib"
# Local cache configuration
LOC_MEM_CACHE_BACKEND = "django.core.cache.backends.locmem.LocMemCache"
CACHES = {
"default": {
"BACKEND": LOC_MEM_CACHE_BACKEND,
"LOCATION": "unique-snowflake",
"TIMEOUT": 300, # 5 minutes
"OPTIONS": {"MAX_ENTRIES": 1000},
},
"sessions": {
"BACKEND": LOC_MEM_CACHE_BACKEND,
"LOCATION": "sessions-cache",
"TIMEOUT": 86400, # 24 hours (same as SESSION_COOKIE_AGE)
"OPTIONS": {"MAX_ENTRIES": 5000},
},
"api": {
"BACKEND": LOC_MEM_CACHE_BACKEND,
"LOCATION": "api-cache",
"TIMEOUT": 300, # 5 minutes
"OPTIONS": {"MAX_ENTRIES": 2000},
},
}
# Development-friendly cache settings
CACHE_MIDDLEWARE_SECONDS = 1 # Very short cache for development
CACHE_MIDDLEWARE_KEY_PREFIX = "thrillwiki_dev"
# Development email backend
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
# Security settings for development
SECURE_SSL_REDIRECT = False
SESSION_COOKIE_SECURE = False
CSRF_COOKIE_SECURE = False
# Development monitoring tools
DEVELOPMENT_APPS = [
"silk",
"nplusone.ext.django",
"django_extensions",
"widget_tweaks",
]
# Add development apps if available
for app in DEVELOPMENT_APPS:
if app not in INSTALLED_APPS:
INSTALLED_APPS.append(app)
# Development middleware
DEVELOPMENT_MIDDLEWARE = [
"silk.middleware.SilkyMiddleware",
"nplusone.ext.django.NPlusOneMiddleware",
"core.middleware.performance_middleware.PerformanceMiddleware",
"core.middleware.performance_middleware.QueryCountMiddleware",
]
# Add development middleware
for middleware in DEVELOPMENT_MIDDLEWARE:
if middleware not in MIDDLEWARE:
MIDDLEWARE.insert(1, middleware) # Insert after security middleware
# Debug toolbar configuration
INTERNAL_IPS = ["127.0.0.1", "::1"]
# Silk configuration for development
# Disable profiler to avoid silk_profile installation issues
SILKY_PYTHON_PROFILER = False
SILKY_PYTHON_PROFILER_BINARY = False # Disable binary profiler
SILKY_PYTHON_PROFILER_RESULT_PATH = (
BASE_DIR / "profiles"
) # Not needed when profiler is disabled
SILKY_AUTHENTICATION = True # Require login to access Silk
SILKY_AUTHORISATION = True # Enable authorization
SILKY_MAX_REQUEST_BODY_SIZE = -1 # Don't limit request body size
# Limit response body size to 1KB for performance
SILKY_MAX_RESPONSE_BODY_SIZE = 1024
SILKY_META = True # Record metadata about requests
# NPlusOne configuration
NPLUSONE_LOGGER = logging.getLogger("nplusone")
NPLUSONE_LOG_LEVEL = logging.WARN
# Enhanced development logging
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"formatters": {
"verbose": {
"format": "{levelname} {asctime} {module} {process:d} {thread:d} {message}",
"style": "{",
},
"json": {
"()": "pythonjsonlogger.jsonlogger.JsonFormatter",
"format": (
"%(levelname)s %(asctime)s %(module)s %(process)d "
"%(thread)d %(message)s"
),
},
},
"handlers": {
"console": {
"class": "logging.StreamHandler",
"formatter": "verbose",
},
"file": {
"class": "logging.handlers.RotatingFileHandler",
"filename": BASE_DIR / "logs" / "thrillwiki.log",
"maxBytes": 1024 * 1024 * 10, # 10MB
"backupCount": 5,
"formatter": "json",
},
"performance": {
"class": "logging.handlers.RotatingFileHandler",
"filename": BASE_DIR / "logs" / "performance.log",
"maxBytes": 1024 * 1024 * 10, # 10MB
"backupCount": 5,
"formatter": "json",
},
},
"root": {
"level": "INFO",
"handlers": ["console"],
},
"loggers": {
"django": {
"handlers": ["file"],
"level": "INFO",
"propagate": False,
},
"django.db.backends": {
"handlers": ["console"],
"level": "DEBUG",
"propagate": False,
},
"thrillwiki": {
"handlers": ["console", "file"],
"level": "DEBUG",
"propagate": False,
},
"performance": {
"handlers": ["performance"],
"level": "INFO",
"propagate": False,
},
"query_optimization": {
"handlers": ["console", "file"],
"level": "WARNING",
"propagate": False,
},
"nplusone": {
"handlers": ["console"],
"level": "WARNING",
"propagate": False,
},
},
}