137 lines
4.7 KiB
Python
137 lines
4.7 KiB
Python
"""系统配置的读取、保存和启动种子。间隔与验证码选项按次读数据库。"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from admin.identity import identity
|
|
from admin.models import SystemConfig
|
|
from euniv.crypto import encrypt_secret
|
|
|
|
_INITIAL_USERNAME = "admin"
|
|
_INITIAL_PASSWORD = "242520"
|
|
|
|
|
|
class SettingsError(ValueError):
|
|
"""配置不符合要求。"""
|
|
|
|
|
|
def load_config(db: Session) -> SystemConfig:
|
|
row = db.get(SystemConfig, 1)
|
|
if row is None:
|
|
row = db.scalars(select(SystemConfig).limit(1)).first()
|
|
if row is None:
|
|
raise RuntimeError("系统配置不存在")
|
|
return row
|
|
|
|
|
|
def ensure_admin_seed() -> None:
|
|
"""仅在还没有配置行时写入初始管理员。已有配置则保持不变。"""
|
|
from euniv.db import SessionLocal
|
|
|
|
with SessionLocal() as db:
|
|
if db.scalars(select(SystemConfig).limit(1)).first() is not None:
|
|
return
|
|
db.add(
|
|
SystemConfig(
|
|
id=1,
|
|
refresh_interval_minutes=30,
|
|
show_manual_jigsaw=True,
|
|
manual_preference=False,
|
|
auto_jigsaw_attempts=3,
|
|
admin_username=_INITIAL_USERNAME,
|
|
admin_password_cipher=encrypt_secret(_INITIAL_PASSWORD),
|
|
)
|
|
)
|
|
db.commit()
|
|
|
|
|
|
def load_admin_identity() -> None:
|
|
"""从设置表重新加载管理员用户名和密码密文。"""
|
|
from euniv.db import SessionLocal
|
|
|
|
with SessionLocal() as db:
|
|
row = load_config(db)
|
|
identity.replace(row.admin_username, row.admin_password_cipher)
|
|
|
|
|
|
def refresh_interval_seconds(db: Session | None = None) -> int:
|
|
if db is None:
|
|
from euniv.db import SessionLocal
|
|
|
|
with SessionLocal() as owned:
|
|
return int(load_config(owned).refresh_interval_minutes) * 60
|
|
return int(load_config(db).refresh_interval_minutes) * 60
|
|
|
|
|
|
def auto_attempt_count(db: Session) -> int:
|
|
return int(load_config(db).auto_jigsaw_attempts)
|
|
|
|
|
|
def settings_view(row: SystemConfig) -> dict:
|
|
return {
|
|
"refresh_interval_minutes": int(row.refresh_interval_minutes),
|
|
"show_manual_jigsaw": bool(row.show_manual_jigsaw),
|
|
"manual_preference": bool(row.manual_preference),
|
|
"auto_jigsaw_attempts": int(row.auto_jigsaw_attempts),
|
|
"admin_username": row.admin_username,
|
|
}
|
|
|
|
|
|
def login_config_view(row: SystemConfig) -> dict:
|
|
show = bool(row.show_manual_jigsaw)
|
|
checked = bool(row.manual_preference) if show else False
|
|
return {"show_manual_checkbox": show, "manual_checkbox_checked": checked}
|
|
|
|
|
|
def jigsaw_plan(db: Session, requested_manual: bool) -> tuple[bool, int]:
|
|
"""返回(是否直接手动,自动尝试次数)。"""
|
|
row = load_config(db)
|
|
attempts = int(row.auto_jigsaw_attempts)
|
|
if bool(row.show_manual_jigsaw):
|
|
return bool(requested_manual), attempts
|
|
if bool(row.manual_preference):
|
|
return False, attempts
|
|
return True, attempts
|
|
|
|
|
|
def _as_int(value: object) -> int | None:
|
|
if isinstance(value, bool) or not isinstance(value, int):
|
|
return None
|
|
return value
|
|
|
|
|
|
def save_settings(db: Session, payload: dict) -> SystemConfig:
|
|
username = str(payload.get("admin_username") or "").strip()
|
|
if not username:
|
|
raise SettingsError("请填写管理员用户名")
|
|
if len(username) > 64:
|
|
raise SettingsError("管理员用户名不能超过 64 个字符")
|
|
interval = _as_int(payload.get("refresh_interval_minutes"))
|
|
attempts = _as_int(payload.get("auto_jigsaw_attempts"))
|
|
if interval is None or not 1 <= interval <= 1440:
|
|
raise SettingsError("定时刷新间隔须为 1 到 1440 的整数")
|
|
if attempts is None or not 1 <= attempts <= 10:
|
|
raise SettingsError("自动滑动次数须为 1 到 10 的整数")
|
|
password = payload.get("admin_password")
|
|
if password is not None and password != "" and len(str(password)) > 128:
|
|
raise SettingsError("管理员密码过长")
|
|
|
|
row = load_config(db)
|
|
previous_interval = int(row.refresh_interval_minutes)
|
|
row.refresh_interval_minutes = interval
|
|
row.show_manual_jigsaw = bool(payload.get("show_manual_jigsaw"))
|
|
row.manual_preference = bool(payload.get("manual_preference"))
|
|
row.auto_jigsaw_attempts = attempts
|
|
row.admin_username = username
|
|
if password is not None and password != "":
|
|
row.admin_password_cipher = encrypt_secret(str(password))
|
|
db.commit()
|
|
identity.replace(row.admin_username, row.admin_password_cipher)
|
|
if interval != previous_interval:
|
|
from euniv.service import notify_refresh_interval_changed
|
|
|
|
notify_refresh_interval_changed()
|
|
return row
|