(
admin_password: str,
site_name: str | None = None,
setup_options: dict | None = None,
)
| 80 | |
| 81 | |
| 82 | async def initialize_system( |
| 83 | admin_password: str, |
| 84 | site_name: str | None = None, |
| 85 | setup_options: dict | None = None, |
| 86 | ) -> None: |
| 87 | password = str(admin_password or "").strip() |
| 88 | if len(password) < MIN_ADMIN_PASSWORD_LENGTH: |
| 89 | raise ValueError(f"管理员密码至少需要 {MIN_ADMIN_PASSWORD_LENGTH} 位") |
| 90 | |
| 91 | async with db_startup_lock(): |
| 92 | config_record = await KeyValue.filter(key="settings").first() |
| 93 | current_config = { |
| 94 | **DEFAULT_CONFIG, |
| 95 | **(config_record.value if config_record and config_record.value else {}), |
| 96 | } |
| 97 | if is_config_initialized(current_config): |
| 98 | raise ValueError("系统已经初始化,请直接登录后台") |
| 99 | |
| 100 | next_config = dict(current_config) |
| 101 | for key, value in (setup_options or {}).items(): |
| 102 | if key in SETUP_CONFIG_FIELDS: |
| 103 | next_config[key] = value |
| 104 | |
| 105 | normalized_site_name = str(site_name or "").strip() |
| 106 | if normalized_site_name: |
| 107 | next_config["name"] = normalized_site_name[:80] |
| 108 | |
| 109 | next_config["admin_token"] = hash_password(password) |
| 110 | if not is_valid_jwt_secret(next_config.get("jwt_secret")): |
| 111 | next_config["jwt_secret"] = generate_jwt_secret() |
| 112 | |
| 113 | await KeyValue.update_or_create(key="settings", defaults={"value": next_config}) |
| 114 | await refresh_settings() |
no test coverage detected