Files
胡绍康andClaude Opus 4.6 f7a8817970 style: 使用 Black 和 isort 格式化代码
- 使用 Black 格式化 47 个 Python 文件
- 使用 isort 修复 import 排序
- 修复 Code Quality CI/CD 检查失败问题

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-04 13:12:16 +08:00

49 lines
1.5 KiB
Python
Raw Permalink 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 __future__ import annotations
from typing import Callable, Optional, Tuple
try:
from flask_wtf.csrf import CSRFProtect
from flask_wtf.csrf import generate_csrf as _generate_csrf # type: ignore
CSRF_AVAILABLE = True
except ImportError: # pragma: no cover
CSRFProtect = None # type: ignore
_generate_csrf = None # type: ignore
CSRF_AVAILABLE = False
print("Warning: flask-wtf not installed. CSRF protection is disabled. Install with: pip install flask-wtf")
def generate_csrf():
"""生成 CSRF token(模块级别导出)"""
if _generate_csrf is not None:
return _generate_csrf()
return None
def init_csrf(app) -> Tuple[Optional[object], Callable, Optional[Callable]]:
"""
初始化 CSRF 保护(如果可用)
返回:csrf 实例、csrf_exempt 装饰器、generate_csrf 函数(可能为 None)
"""
if CSRF_AVAILABLE:
csrf = CSRFProtect(app) # type: ignore[misc]
app.config["WTF_CSRF_TIME_LIMIT"] = None # CSRF token 不过期
app.config["WTF_CSRF_SSL_STRICT"] = False # 允许非HTTPS环境(开发环境)
print("CSRF protection enabled")
def csrf_exempt(f):
return csrf.exempt(f)
return csrf, csrf_exempt, _generate_csrf
# 显式禁用 CSRF 保护
app.config["WTF_CSRF_ENABLED"] = False
app.config["WTF_CSRF_CHECK_DEFAULT"] = False
print("CSRF protection disabled")
def csrf_exempt(f):
return f
return None, csrf_exempt, None