Files
cmhub/config/settings.py
T

176 lines
4.9 KiB
Python
Raw Normal View History

2026-07-01 18:01:23 +08:00
"""
Django settings for config project.
Generated by 'django-admin startproject' using Django 5.2.15.
For more information on this file, see
https://docs.djangoproject.com/en/5.2/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/5.2/ref/settings/
"""
import os
from pathlib import Path
# 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.2/howto/deployment/checklist/
2026-07-02 09:07:15 +08:00
def load_dotenv(path: Path) -> None:
if not path.exists():
return
for raw_line in path.read_text(encoding="utf-8-sig").splitlines():
line = raw_line.strip()
if not line or line.startswith("#") or "=" not in line:
continue
key, value = line.split("=", 1)
os.environ.setdefault(key.strip(), value.strip().strip('"').strip("'"))
2026-07-01 18:01:23 +08:00
def env_bool(name: str, default: bool = False) -> bool:
value = os.environ.get(name)
if value is None:
return default
return value.strip().lower() in {"1", "true", "yes", "on"}
def env_list(name: str, default: str = "") -> list[str]:
value = os.environ.get(name, default)
return [item.strip() for item in value.split(",") if item.strip()]
2026-07-02 09:07:15 +08:00
def env_int(name: str, default: int) -> int:
value = os.environ.get(name)
if not value:
return default
return int(value)
load_dotenv(BASE_DIR / ".env")
2026-07-01 18:01:23 +08:00
# SECURITY WARNING: keep the secret key used in production secret.
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "django-insecure-dev-only-change-me")
2026-07-02 11:07:44 +08:00
AI_KEY_ENCRYPTION_KEY = os.environ.get("AI_KEY_ENCRYPTION_KEY", "")
2026-07-01 18:01:23 +08:00
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env_bool("DJANGO_DEBUG", True)
2026-07-02 09:46:32 +08:00
ALLOWED_HOSTS = env_list("DJANGO_ALLOWED_HOSTS", "127.0.0.1,localhost,testserver")
2026-07-01 18:01:23 +08:00
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
2026-07-02 10:17:10 +08:00
'rest_framework',
'apps.users',
'apps.portal',
'apps.billing',
'apps.ai',
'apps.api',
2026-07-01 18:01:23 +08:00
]
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 = 'config.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
WSGI_APPLICATION = 'config.wsgi.application'
2026-07-02 09:07:15 +08:00
AUTH_USER_MODEL = 'users.User'
2026-07-01 18:01:23 +08:00
# Database
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
DATABASES = {
'default': {
2026-07-02 09:07:15 +08:00
'ENGINE': 'django.db.backends.mysql',
'HOST': os.environ.get('MYSQL_HOST', '127.0.0.1'),
'PORT': env_int('MYSQL_PORT', 3306),
'NAME': os.environ.get('MYSQL_DATABASE', 'cmhub'),
'USER': os.environ.get('MYSQL_USER', ''),
'PASSWORD': os.environ.get('MYSQL_PASSWORD', ''),
'OPTIONS': {
'charset': os.environ.get('MYSQL_CHARSET', 'utf8mb4'),
2026-07-02 17:29:26 +08:00
'connect_timeout': env_int('MYSQL_CONNECT_TIMEOUT', 30),
'read_timeout': env_int('MYSQL_READ_TIMEOUT', 120),
'write_timeout': env_int('MYSQL_WRITE_TIMEOUT', 120),
2026-07-02 09:07:15 +08:00
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
2026-07-01 18:01:23 +08:00
}
}
# Password validation
# https://docs.djangoproject.com/en/5.2/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.2/topics/i18n/
2026-07-02 09:07:15 +08:00
LANGUAGE_CODE = 'zh-hans'
2026-07-01 18:01:23 +08:00
2026-07-02 09:07:15 +08:00
TIME_ZONE = os.environ.get('DJANGO_TIME_ZONE', 'Asia/Shanghai')
2026-07-01 18:01:23 +08:00
USE_I18N = True
USE_TZ = True
# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/5.2/howto/static-files/
STATIC_URL = 'static/'
# Default primary key field type
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'