fix: Task1 质量审查修复(RuntimeError fail-fast/ALLOWED_HOSTS strip/lock 清理/测试缓存隔离)

Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
浅念
2026-09-15 11:27:25 +08:00
parent 6dbe26af0b
commit 2a7f08fd14
3 changed files with 7 additions and 9 deletions
+6 -4
View File
@@ -5,10 +5,12 @@ import os
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = os.environ.get("DJANGO_SECRET_KEY", "dev-insecure")
DEBUG = os.environ.get("DJANGO_DEBUG", "0" if os.environ.get("DJANGO_ENV") == "prod" else "1") == "1"
ALLOWED_HOSTS = os.environ.get("DJANGO_ALLOWED_HOSTS", "*").split(",")
if os.environ.get("DJANGO_ENV") == "prod": # 生产 fail-fast:漏配即启动崩溃,不带病上线
assert SECRET_KEY != "dev-insecure", "生产必须配置 DJANGO_SECRET_KEY"
assert os.environ.get("DJANGO_ALLOWED_HOSTS"), "生产必须显式配置 DJANGO_ALLOWED_HOSTS"
ALLOWED_HOSTS = [h.strip() for h in os.environ.get("DJANGO_ALLOWED_HOSTS", "*").split(",") if h.strip()]
if os.environ.get("DJANGO_ENV") == "prod": # 生产 fail-fast:漏配即启动崩溃,不带病上线(raise 而非 assert,防 -O 剥离)
if SECRET_KEY == "dev-insecure":
raise RuntimeError("生产必须配置 DJANGO_SECRET_KEY")
if not os.environ.get("DJANGO_ALLOWED_HOSTS"):
raise RuntimeError("生产必须显式配置 DJANGO_ALLOWED_HOSTS")
INSTALLED_APPS = [
"django.contrib.contenttypes",
+1
View File
@@ -1,2 +1,3 @@
from .settings import *
DATABASES = {"default": {"ENGINE": "django.db.backends.sqlite3", "NAME": ":memory:"}}
CACHES = {"default": {"BACKEND": "django.core.cache.backends.locmem.LocMemCache"}} # 内存缓存,避免文件缓存限流计数在重跑间残留
-5
View File
@@ -1,16 +1,11 @@
# 由 pip freeze 生成,剔除测试依赖;部署用 requirements.txt
asgiref==3.12.1
colorama==0.4.6
Django==5.2.17
django-cors-headers==4.9.0
djangorestframework==3.18.1
djangorestframework_simplejwt==5.5.1
gunicorn==26.2.0
iniconfig==2.3.0
packaging==26.3
pluggy==1.6.0
py-ip2region==3.0.4
Pygments==2.21.0
PyJWT==2.14.0
PyMySQL==1.2.0
sqlparse==0.6.0