junaidte14's picture
redirection loop error fix
9d9bc30
Raw History Blame Contribute Delete
6.84 kB
"""
Django settings for admin dashboard.
"""
import os
from pathlib import Path
import environ
# Build paths
BASE_DIR = Path(__file__).resolve().parent.parent
# Environment variables
env = environ.Env(
DEBUG=(bool, False),
ALLOWED_HOSTS=(list, ['localhost', '127.0.0.1', 'junaidte14-django-admin-dashboard.hf.space']),
)
environ.Env.read_env(BASE_DIR / '.env')
import platform
# Automatically choose Windows path or Linux path depending on environment
if platform.system() == "Windows":
NPM_BIN_PATH = r"C:\Program Files\nodejs\npm.cmd"
else:
NPM_BIN_PATH = "/usr/bin/npm" # Standard location inside our Docker container
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY', default='django-insecure-change-this-in-production')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG')
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=[
'localhost',
'127.0.0.1',
'junaidte14-django-admin-dashboard.hf.space',
'.hf.space'
])
# Application definition
INSTALLED_APPS = [
# Django apps
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Third-party apps
'corsheaders',
'crispy_forms',
'crispy_tailwind',
'import_export',
'django_extensions',
'tailwind',
# Local apps
'apps.core',
'apps.rag',
'apps.agents',
'apps.dashboard',
'theme'
]
if DEBUG:
INSTALLED_APPS += [
'debug_toolbar',
'django_browser_reload'
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
# KEEP THIS
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'csp.middleware.CSPMiddleware',
# Custom JWT middleware
'apps.core.middleware.JWTAuthMiddleware',
]
if DEBUG:
MIDDLEWARE.insert(0, 'debug_toolbar.middleware.DebugToolbarMiddleware')
MIDDLEWARE.append('django_browser_reload.middleware.BrowserReloadMiddleware')
ROOT_URLCONF = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [BASE_DIR / 'templates'],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
# REQUIRED
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
DATABASES = {
'default': env.db(
'DATABASE_URL',
default=f"postgres://{env('DB_USER', default='postgres')}:"
f"{env('DB_PASSWORD', default='postgres')}@"
f"{env('DB_HOST', default='localhost')}:"
f"{env('DB_PORT', default='5432')}/"
f"{env('DB_NAME', default='chatbot_db')}"
)
}
# Cache (Redis)
CACHES = {
"default": {
"BACKEND": "django.core.cache.backends.dummy.DummyCache",
}
}
# Internationalization
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC'
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
STATICFILES_DIRS = [BASE_DIR / 'static']
STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
WHITENOISE_MANIFEST_STRICT = False
# Media files
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'
# Default primary key field type
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Custom User Model (points to FastAPI's users table)
AUTH_USER_MODEL = 'core.User'
AUTHENTICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
]
# Authentication
LOGIN_URL = '/auth/login/'
LOGIN_REDIRECT_URL = '/dashboard/'
LOGOUT_REDIRECT_URL = '/auth/login/'
# Session
SESSION_COOKIE_AGE = 86400 # 24 hours
SESSION_COOKIE_HTTPONLY = True
SESSION_COOKIE_SECURE = not DEBUG
SESSION_COOKIE_SAMESITE = 'Lax'
# CSRF
CSRF_COOKIE_HTTPONLY = False
CSRF_COOKIE_SECURE = not DEBUG
CSRF_COOKIE_SAMESITE = 'Lax'
CSRF_TRUSTED_ORIGINS = [
'https://junaidte14-django-admin-dashboard.hf.space',
'https://*.hf.space',
]
# Security Headers
SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True
X_FRAME_OPTIONS = 'DENY'
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
if not DEBUG:
SECURE_SSL_REDIRECT = True
SECURE_HSTS_SECONDS = 31536000
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_HSTS_PRELOAD = True
# CORS (if needed for API calls from admin)
CORS_ALLOWED_ORIGINS = env.list('CORS_ORIGINS', default=[
'http://localhost:3000',
'http://localhost:8000',
])
# CSP (Content Security Policy)
CSP_DEFAULT_SRC = ("'self'",)
CSP_SCRIPT_SRC = (
"'self'",
"'unsafe-inline'",
"'unsafe-eval'",
)
CSP_STYLE_SRC = (
"'self'",
"'unsafe-inline'",
)
CSP_IMG_SRC = (
"'self'",
"data:",
)
CSP_CONNECT_SRC = (
"'self'",
"https://junaidte14-aiverse.hf.space",
"http://localhost:8000"
)
# Crispy Forms (Tailwind)
CRISPY_ALLOWED_TEMPLATE_PACKS = "tailwind"
CRISPY_TEMPLATE_PACK = "tailwind"
# Tailwind
TAILWIND_APP_NAME = 'theme'
INTERNAL_IPS = ['127.0.0.1']
# Django Admin
ADMIN_SITE_HEADER = "Admin Dashboard"
ADMIN_SITE_TITLE = "Admin Dashboard"
ADMIN_INDEX_TITLE = "Welcome to Admin Dashboard"
# Logging
LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"console": {
"class": "logging.StreamHandler",
},
},
"root": {
"handlers": ["console"],
"level": "INFO",
},
"loggers": {
"": {
"handlers": ["console"],
"level": "INFO",
"propagate": False,
},
},
}
# FastAPI Integration
FASTAPI_BASE_URL = env('FASTAPI_BASE_URL', default='https://junaidte14-aiverse.hf.space/api/v1')
FASTAPI_TIMEOUT = 300 * 60 # 300 minute
# Pagination
ITEMS_PER_PAGE = 50
# Sentry (Production)
if not DEBUG and env('SENTRY_DSN', default=None):
import sentry_sdk
from sentry_sdk.integrations.django import DjangoIntegration
sentry_sdk.init(
dsn=env('SENTRY_DSN'),
integrations=[DjangoIntegration()],
traces_sample_rate=0.1,
send_default_pii=True,
environment=env('ENVIRONMENT', default='production'),
)