264 lines
7.6 KiB
Python
264 lines
7.6 KiB
Python
"""
|
||
Django settings for green_classroom project.
|
||
|
||
Generated by 'django-admin startproject' using Django 5.1.
|
||
|
||
For more information on this file, see
|
||
https://docs.djangoproject.com/en/5.1/topics/settings/
|
||
|
||
For the full list of settings and their values, see
|
||
https://docs.djangoproject.com/en/5.1/ref/settings/
|
||
"""
|
||
|
||
import os
|
||
from pathlib import Path
|
||
from dotenv import load_dotenv
|
||
|
||
# 加载环境变量
|
||
load_dotenv()
|
||
|
||
# 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/5.1/howto/deployment/checklist/
|
||
|
||
# SECURITY WARNING: keep the secret key used in production secret!
|
||
SECRET_KEY = os.getenv('SECRET_KEY', 'django-insecure-_kr!&5j#i!)lo(=u-&5ni+21cwxcq)j-35k!ne20)fyx!u6dnl')
|
||
|
||
# SECURITY WARNING: don't run with debug turned on in production!
|
||
DEBUG = os.getenv('DEBUG', 'True').lower() == 'true'
|
||
|
||
ALLOWED_HOSTS = os.getenv('ALLOWED_HOSTS', 'localhost,127.0.0.1,192.168.9.108,green.yuangyaa.com').split(',')
|
||
|
||
CSRF_TRUSTED_ORIGINS = os.getenv('CSRF_TRUSTED_ORIGINS', 'https://green.yuangyaa.com').split(',')
|
||
|
||
# Application definition
|
||
|
||
INSTALLED_APPS = [
|
||
'django.contrib.admin',
|
||
'django.contrib.auth',
|
||
'django.contrib.contenttypes',
|
||
'django.contrib.sessions',
|
||
'django.contrib.messages',
|
||
'django.contrib.staticfiles',
|
||
'core',
|
||
'django_celery_beat',
|
||
'django_celery_results',
|
||
'rest_framework',
|
||
'rest_framework.authtoken',
|
||
]
|
||
|
||
# 导入Admin扩展
|
||
# import core.admin_extended # 暂时注释,避免循环导入
|
||
|
||
MIDDLEWARE = [
|
||
'django.middleware.security.SecurityMiddleware',
|
||
'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 = 'green_classroom.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 = 'green_classroom.wsgi.application'
|
||
|
||
# Database
|
||
# https://docs.djangoproject.com/en/5.1/ref/settings/#databases
|
||
|
||
# 根据环境变量选择数据库
|
||
DB_ENGINE = os.getenv('DB_ENGINE', 'django.db.backends.sqlite3')
|
||
|
||
if DB_ENGINE == 'django.db.backends.postgresql':
|
||
DATABASES = {
|
||
'default': {
|
||
'ENGINE': DB_ENGINE,
|
||
'NAME': os.getenv('DB_NAME', 'green_classroom'),
|
||
'USER': os.getenv('DB_USER', 'postgres'),
|
||
'PASSWORD': os.getenv('DB_PASSWORD', ''),
|
||
'HOST': os.getenv('DB_HOST', 'localhost'),
|
||
'PORT': os.getenv('DB_PORT', '5432'),
|
||
'OPTIONS': {
|
||
'charset': 'utf8mb4',
|
||
},
|
||
}
|
||
}
|
||
else:
|
||
DATABASES = {
|
||
'default': {
|
||
'ENGINE': 'django.db.backends.sqlite3',
|
||
'NAME': BASE_DIR / 'db.sqlite3',
|
||
}
|
||
}
|
||
|
||
# Password validation
|
||
# https://docs.djangoproject.com/en/5.1/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/5.1/topics/i18n/
|
||
|
||
LANGUAGE_CODE = 'zh-Hans'
|
||
|
||
TIME_ZONE = 'UTC'
|
||
|
||
USE_I18N = True
|
||
|
||
USE_TZ = True
|
||
|
||
# Static files (CSS, JavaScript, Images)
|
||
# https://docs.djangoproject.com/en/5.1/howto/static-files/
|
||
|
||
STATIC_URL = '/static/'
|
||
STATIC_ROOT = os.getenv('STATIC_ROOT', os.path.join(BASE_DIR, 'data', 'static'))
|
||
|
||
# Default primary key field type
|
||
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
|
||
|
||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||
|
||
# 媒体文件配置
|
||
MEDIA_ROOT = os.getenv('MEDIA_ROOT', os.path.join(BASE_DIR, 'data', 'media'))
|
||
MEDIA_URL = '/media/'
|
||
|
||
# Celery配置
|
||
CELERY_BROKER_URL = os.getenv('CELERY_BROKER_URL', 'redis://127.0.0.1:6379/0')
|
||
CELERY_RESULT_BACKEND = os.getenv('CELERY_RESULT_BACKEND', 'redis://127.0.0.1:6379/0')
|
||
CELERY_ACCEPT_CONTENT = ['json']
|
||
CELERY_TASK_SERIALIZER = 'json'
|
||
CELERY_RESULT_SERIALIZER = 'json'
|
||
CELERY_TIMEZONE = TIME_ZONE
|
||
CELERY_TASK_TRACK_STARTED = True
|
||
CELERY_TASK_TIME_LIMIT = 30 * 60 # 30分钟
|
||
|
||
# Redis配置
|
||
REDIS_URL = os.getenv('REDIS_URL', 'redis://127.0.0.1:6379/0')
|
||
|
||
# 日志配置
|
||
LOGGING = {
|
||
'version': 1,
|
||
'disable_existing_loggers': False,
|
||
'formatters': {
|
||
'verbose': {
|
||
'format': '{levelname} {asctime} {module} {process:d} {thread:d} {message}',
|
||
'style': '{',
|
||
},
|
||
'simple': {
|
||
'format': '{levelname} {message}',
|
||
'style': '{',
|
||
},
|
||
},
|
||
'handlers': {
|
||
'file': {
|
||
'level': os.getenv('LOG_LEVEL', 'INFO'),
|
||
'class': 'logging.FileHandler',
|
||
'filename': os.getenv('LOG_FILE', os.path.join(BASE_DIR, 'data', 'logs', 'django.log')),
|
||
'formatter': 'verbose',
|
||
},
|
||
'console': {
|
||
'level': os.getenv('LOG_LEVEL', 'INFO'),
|
||
'class': 'logging.StreamHandler',
|
||
'formatter': 'simple',
|
||
},
|
||
},
|
||
'root': {
|
||
'handlers': ['console', 'file'],
|
||
'level': os.getenv('LOG_LEVEL', 'INFO'),
|
||
},
|
||
'loggers': {
|
||
'django': {
|
||
'handlers': ['console', 'file'],
|
||
'level': os.getenv('LOG_LEVEL', 'INFO'),
|
||
'propagate': False,
|
||
},
|
||
'core': {
|
||
'handlers': ['console', 'file'],
|
||
'level': os.getenv('LOG_LEVEL', 'INFO'),
|
||
'propagate': False,
|
||
},
|
||
},
|
||
}
|
||
|
||
# 安全设置
|
||
if not DEBUG:
|
||
SECURE_BROWSER_XSS_FILTER = True
|
||
SECURE_CONTENT_TYPE_NOSNIFF = True
|
||
X_FRAME_OPTIONS = 'DENY'
|
||
SECURE_HSTS_SECONDS = 31536000
|
||
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
|
||
SECURE_HSTS_PRELOAD = True
|
||
|
||
# 爬虫设置
|
||
CRAWLER_TIMEOUT = int(os.getenv('CRAWLER_TIMEOUT', 30))
|
||
CRAWLER_MAX_RETRIES = int(os.getenv('CRAWLER_MAX_RETRIES', 3))
|
||
CRAWLER_DELAY = int(os.getenv('CRAWLER_DELAY', 1))
|
||
|
||
# Selenium设置
|
||
SELENIUM_HEADLESS = os.getenv('SELENIUM_HEADLESS', 'True').lower() == 'true'
|
||
CHROME_DRIVER_PATH = os.getenv('CHROME_DRIVER_PATH', '/usr/bin/chromedriver')
|
||
|
||
# Sentry监控(可选)
|
||
SENTRY_DSN = os.getenv('SENTRY_DSN')
|
||
if SENTRY_DSN:
|
||
import sentry_sdk
|
||
from sentry_sdk.integrations.django import DjangoIntegration
|
||
|
||
sentry_sdk.init(
|
||
dsn=SENTRY_DSN,
|
||
integrations=[DjangoIntegration()],
|
||
traces_sample_rate=1.0,
|
||
send_default_pii=True
|
||
)
|
||
|
||
# Django REST Framework 配置
|
||
REST_FRAMEWORK = {
|
||
'DEFAULT_RENDERER_CLASSES': [
|
||
'rest_framework.renderers.JSONRenderer',
|
||
'rest_framework.renderers.BrowsableAPIRenderer',
|
||
],
|
||
'DEFAULT_PERMISSION_CLASSES': [
|
||
'rest_framework.permissions.IsAuthenticated',
|
||
],
|
||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||
'rest_framework.authentication.SessionAuthentication',
|
||
'rest_framework.authentication.TokenAuthentication',
|
||
],
|
||
}
|
||
|
||
DATA_UPLOAD_MAX_NUMBER_FIELDS = 10240
|
||
|
||
|