Files
LiteOps/backend/backend/settings.py
2025-06-30 11:57:32 +08:00

199 lines
5.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pathlib import Path
import pymysql
import logging
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = 'django-insecure-5z@^6hpk^zxffj_7)&l3pvww8@ky3qsai4)m!vcog!8#=@a3&%'
DEBUG = True
ALLOWED_HOSTS = ['*'] # 允许所有主机访问
# CORS设置
CORS_ALLOW_CREDENTIALS = True
CORS_ALLOW_ALL_ORIGINS = True # 允许所有来源
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders', # 添加 cors-headers
'apps',
]
MIDDLEWARE = [
'corsheaders.middleware.CorsMiddleware',
'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 = '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 = 'backend.wsgi.application'
pymysql.install_as_MySQLdb()
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'OPTIONS': {
'read_default_file': str(BASE_DIR / 'conf' / 'config.txt'),
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
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 = 'en-us' # 修改为中文
TIME_ZONE = 'Asia/Shanghai'
USE_I18N = True
USE_L10N = True
USE_TZ = False # 修改为 True使用时区功能
# 日志配置
LOG_DIR = BASE_DIR / 'logs'
LOG_DIR.mkdir(exist_ok=True) # 确保日志目录存在
class BuildLogFilter(logging.Filter):
def filter(self, record):
# 如果是构建日志,使用简单格式
if getattr(record, 'from_builder', False):
return True
# 其他日志使用默认格式
return True
LOGGING = {
'version': 1,
'disable_existing_loggers': False,
'filters': {
'build_log_filter': {
'()': 'backend.settings.BuildLogFilter',
},
'non_build_log_filter': {
'()': lambda: type('Filter', (logging.Filter,), {
'filter': lambda self, record: not getattr(record, 'from_builder', False)
})(),
},
},
'formatters': {
'verbose': {
'format': '[{asctime}] {levelname} [{name}:{lineno}] {message}',
'style': '{',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
'simple': {
'format': '{message}',
'style': '{'
},
'console_with_time': {
'format': '[{asctime}] {levelname} {message}',
'style': '{',
'datefmt': '%Y-%m-%d %H:%M:%S'
},
},
'handlers': {
'console_build': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'simple',
'filters': ['build_log_filter'],
},
'console_normal': {
'level': 'DEBUG',
'class': 'logging.StreamHandler',
'formatter': 'console_with_time',
'filters': ['non_build_log_filter'],
},
'file': {
'level': 'INFO',
'class': 'logging.handlers.RotatingFileHandler',
'filename': str(LOG_DIR / 'django.log'),
'maxBytes': 1024 * 1024 * 5, # 5MB
'backupCount': 5,
'formatter': 'verbose',
},
'error_file': {
'level': 'ERROR',
'class': 'logging.handlers.RotatingFileHandler',
'filename': str(LOG_DIR / 'error.log'),
'maxBytes': 1024 * 1024 * 5, # 5MB
'backupCount': 5,
'formatter': 'verbose',
}
},
'loggers': {
'django': {
'handlers': ['console_normal', 'file'],
'level': 'INFO',
'propagate': True,
},
'django.request': {
'handlers': ['error_file'],
'level': 'ERROR',
'propagate': False,
},
'django.server': {
'handlers': ['console_normal', 'file'],
'level': 'INFO',
'propagate': False,
},
'apps': {
'handlers': ['console_build', 'console_normal', 'file', 'error_file'],
'level': 'DEBUG',
'propagate': False, # 设置为False以避免重复记录
},
},
'root': {
'handlers': ['console_normal', 'file', 'error_file'],
'level': 'INFO',
},
}
STATIC_URL = 'static/'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# 构建相关配置
BUILD_ROOT = Path('/Users/huk/Downloads/data') # 修改为指定目录
# BUILD_ROOT = Path('/data')
BUILD_ROOT.mkdir(exist_ok=True, parents=True) # 确保目录存在,包括父目录