fix(douyu): 修复查询工作台冷却筛选与 CSV 导出安全 (v0.4.27)
This commit is contained in:
@@ -25,7 +25,7 @@ from fastapi import (
|
||||
WebSocketDisconnect,
|
||||
)
|
||||
from loguru import logger
|
||||
from sqlalchemy import case, func, or_, select
|
||||
from sqlalchemy import and_, case, func, or_, select
|
||||
from sqlalchemy.orm import Session, joinedload
|
||||
from sqlalchemy.sql import Select
|
||||
from starlette.concurrency import run_in_threadpool
|
||||
@@ -672,6 +672,7 @@ def _apply_task_account_filters(
|
||||
query = query.filter(
|
||||
Account.cookie.isnot(None),
|
||||
Account.cookie != "",
|
||||
or_(Account.status.is_(None), Account.status != "invalid"),
|
||||
or_(
|
||||
Account.ck_check_status.is_(None),
|
||||
Account.ck_check_status.notin_(("valid", "invalid")),
|
||||
@@ -682,15 +683,27 @@ def _apply_task_account_filters(
|
||||
query = query.filter(
|
||||
Account.role_bind_status == BIND_BOUND,
|
||||
or_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
Account.role_change_available_at <= now,
|
||||
and_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
or_(
|
||||
Account.change_role_wait_time.is_(None),
|
||||
Account.change_role_wait_time <= 0,
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
elif ck_filter == "cooldown_waiting":
|
||||
now = datetime.now(UTC)
|
||||
query = query.filter(
|
||||
Account.role_bind_status == BIND_BOUND,
|
||||
Account.role_change_available_at > now,
|
||||
or_(
|
||||
Account.role_change_available_at > now,
|
||||
and_(
|
||||
Account.role_change_available_at.is_(None),
|
||||
Account.change_role_wait_time > 0,
|
||||
),
|
||||
),
|
||||
)
|
||||
search_text = (search or "").strip()
|
||||
if search_text:
|
||||
@@ -1211,7 +1224,7 @@ _ACCOUNT_STATUS_LABELS = {
|
||||
|
||||
|
||||
def _manual_budget_summary(db: Session) -> dict[str, Any]:
|
||||
overview = get_login_quota_usage_summary(db)
|
||||
overview = cast(dict[str, Any], get_login_quota_usage_summary(db))
|
||||
manual_cat = next(
|
||||
(
|
||||
item
|
||||
@@ -1281,6 +1294,15 @@ def _format_cooldown_duration(seconds: int) -> str:
|
||||
return f"{minutes}分"
|
||||
|
||||
|
||||
def _csv_safe_cell(value: Any) -> Any:
|
||||
"""Prevent spreadsheet apps from evaluating exported text as a formula."""
|
||||
if isinstance(value, str) and value.lstrip(" \t\r\n").startswith(
|
||||
("=", "+", "-", "@")
|
||||
):
|
||||
return f"'{value}"
|
||||
return value
|
||||
|
||||
|
||||
@router.post("/query-workbench/lookup")
|
||||
def lookup_query_workbench_accounts(
|
||||
req: DouyuQueryWorkbenchLookupRequest,
|
||||
@@ -1382,7 +1404,9 @@ def relogin_query_workbench_invalid_accounts(
|
||||
current: User = Depends(require_permission("douyu:task")),
|
||||
):
|
||||
"""从查询工作台发起失效/无 CK 账号重新登录,强制走「人工登录」预算(manual_login)。"""
|
||||
if current.role != "super_admin" and not user_has_permission(current, "login:batch"):
|
||||
if current.role != "super_admin" and not user_has_permission(
|
||||
current, "login:batch"
|
||||
):
|
||||
raise HTTPException(
|
||||
status_code=403, detail="无权限发起重登(需超级管理员或 login:batch 权限)"
|
||||
)
|
||||
@@ -1533,9 +1557,7 @@ def export_query_workbench_csv(
|
||||
)
|
||||
rows_by_id = {int(acc.id): acc for acc in visible_accounts}
|
||||
|
||||
ordered_accounts = [
|
||||
rows_by_id[aid] for aid in requested_ids if aid in rows_by_id
|
||||
]
|
||||
ordered_accounts = [rows_by_id[aid] for aid in requested_ids if aid in rows_by_id]
|
||||
now = datetime.now(UTC)
|
||||
output = io.StringIO()
|
||||
writer = csv.writer(output)
|
||||
@@ -1574,7 +1596,9 @@ def export_query_workbench_csv(
|
||||
ck_label = "未检测"
|
||||
|
||||
bind_info = bind_payload(acc)
|
||||
bind_status = str(bind_info.get("role_bind_status") or acc.role_bind_status or "")
|
||||
bind_status = str(
|
||||
bind_info.get("role_bind_status") or acc.role_bind_status or ""
|
||||
)
|
||||
bind_label = str(bind_info.get("role_bind_label") or "")
|
||||
|
||||
change_status = "未知"
|
||||
@@ -1593,43 +1617,42 @@ def export_query_workbench_csv(
|
||||
)
|
||||
if aware > now:
|
||||
change_status = "冷却中"
|
||||
cooldown_note = (
|
||||
f"剩余 {_format_cooldown_duration(int((aware - now).total_seconds()))}"
|
||||
)
|
||||
cooldown_note = f"剩余 {_format_cooldown_duration(int((aware - now).total_seconds()))}"
|
||||
else:
|
||||
change_status = "可换绑"
|
||||
cooldown_note = "已过冷却"
|
||||
elif (acc.change_role_wait_time or 0) > 0:
|
||||
change_status = "冷却中"
|
||||
cooldown_note = (
|
||||
f"剩余 {_format_cooldown_duration(int(acc.change_role_wait_time or 0))}"
|
||||
)
|
||||
cooldown_note = f"剩余 {_format_cooldown_duration(int(acc.change_role_wait_time or 0))}"
|
||||
else:
|
||||
change_status = "可换绑"
|
||||
cooldown_note = "可直接换绑"
|
||||
|
||||
writer.writerow(
|
||||
[
|
||||
acc.username or "",
|
||||
acc.uid or "",
|
||||
acc.nickname or "",
|
||||
acc.tag or "",
|
||||
acc.assigned_user.username if acc.assigned_user else "未分配",
|
||||
_ACCOUNT_STATUS_LABELS.get(acc.status or "", acc.status or ""),
|
||||
ck_label,
|
||||
_format_shanghai_dt(acc.ck_checked_at),
|
||||
acc.gold_balance if acc.gold_balance is not None else "",
|
||||
acc.exchange_balance if acc.exchange_balance is not None else "",
|
||||
acc.points if acc.points is not None else "",
|
||||
_format_shanghai_dt(acc.points_expire_at),
|
||||
bind_label,
|
||||
acc.game_name or "",
|
||||
acc.game_channel or "",
|
||||
change_status,
|
||||
_format_shanghai_dt(acc.role_change_available_at),
|
||||
_format_shanghai_dt(acc.role_change_official_at),
|
||||
cooldown_note,
|
||||
_format_shanghai_dt(acc.updated_at or acc.overview_queried_at),
|
||||
_csv_safe_cell(value)
|
||||
for value in [
|
||||
acc.username or "",
|
||||
acc.uid or "",
|
||||
acc.nickname or "",
|
||||
acc.tag or "",
|
||||
acc.assigned_user.username if acc.assigned_user else "未分配",
|
||||
_ACCOUNT_STATUS_LABELS.get(acc.status or "", acc.status or ""),
|
||||
ck_label,
|
||||
_format_shanghai_dt(acc.ck_checked_at),
|
||||
acc.gold_balance if acc.gold_balance is not None else "",
|
||||
acc.exchange_balance if acc.exchange_balance is not None else "",
|
||||
acc.points if acc.points is not None else "",
|
||||
_format_shanghai_dt(acc.points_expire_at),
|
||||
bind_label,
|
||||
acc.game_name or "",
|
||||
acc.game_channel or "",
|
||||
change_status,
|
||||
_format_shanghai_dt(acc.role_change_available_at),
|
||||
_format_shanghai_dt(acc.role_change_official_at),
|
||||
cooldown_note,
|
||||
_format_shanghai_dt(acc.updated_at or acc.overview_queried_at),
|
||||
]
|
||||
]
|
||||
)
|
||||
filename = f"douyu_query_workbench_{datetime.now(_SHANGHAI_TZ).strftime('%Y%m%d_%H%M%S')}.csv"
|
||||
|
||||
@@ -202,6 +202,17 @@ class OverviewMixin:
|
||||
def _client(self, cookie: str) -> DouyuActivityClient: ...
|
||||
def _mark_task(self, *args: Any, **kwargs: Any) -> None: ...
|
||||
def _push_log(self, level: str, message: str) -> None: ...
|
||||
def _refresh_account_gold_balance(
|
||||
self, client: DouyuActivityClient, account: Account
|
||||
) -> dict[str, Any]: ...
|
||||
def _refresh_account_points(
|
||||
self,
|
||||
client: DouyuActivityClient,
|
||||
account: Account,
|
||||
cookie: str,
|
||||
*,
|
||||
ctn: Any = None,
|
||||
) -> dict[str, Any]: ...
|
||||
def _pick_change_wait_bind_info(
|
||||
self, candidates: list[dict], config: dict
|
||||
) -> dict | None: ...
|
||||
@@ -216,6 +227,7 @@ class OverviewMixin:
|
||||
if isinstance(runtime_payload, dict):
|
||||
payload = {**payload, **runtime_payload}
|
||||
return str(payload.get("view") or "").strip()
|
||||
|
||||
def _refresh_account_gold_balance(
|
||||
self, client: DouyuActivityClient, account: Account
|
||||
) -> dict: ...
|
||||
@@ -455,11 +467,7 @@ class OverviewMixin:
|
||||
snapshot = account.overview_snapshot
|
||||
if not isinstance(snapshot, dict) or account.overview_queried_at is None:
|
||||
return None
|
||||
payload = (
|
||||
self._task_payload(task)
|
||||
if hasattr(self, "_task_payload")
|
||||
else {}
|
||||
)
|
||||
payload = self._task_payload(task) if hasattr(self, "_task_payload") else {}
|
||||
if bool(payload.get("force_refresh")):
|
||||
return None
|
||||
age = (now - account.overview_queried_at).total_seconds()
|
||||
@@ -491,9 +499,7 @@ class OverviewMixin:
|
||||
cached["view"] = view or str(cached.get("view") or "")
|
||||
message = _overview_success_message(
|
||||
cached,
|
||||
cached.get("errors")
|
||||
if isinstance(cached.get("errors"), dict)
|
||||
else {},
|
||||
cached.get("errors") if isinstance(cached.get("errors"), dict) else {},
|
||||
view=view,
|
||||
)
|
||||
self._push_log(
|
||||
@@ -788,9 +794,7 @@ class OverviewMixin:
|
||||
previous_overview.get("high_value_goods") or []
|
||||
)
|
||||
overview["pending_domains"].append(DOMAIN_STOREHOME)
|
||||
errors["limited_goods"] = (
|
||||
"本地商品快照已过期,已加入后台同步队列"
|
||||
)
|
||||
errors["limited_goods"] = "本地商品快照已过期,已加入后台同步队列"
|
||||
enqueue_account_work(
|
||||
db,
|
||||
account_id=int(account.id),
|
||||
@@ -1006,9 +1010,7 @@ class OverviewMixin:
|
||||
]
|
||||
all_records.extend((scope, r) for r in rows)
|
||||
overview[f"exchange_record_count_{scope}"] = len(rows)
|
||||
overview["domain_sources"][DOMAIN_EXCHANGE_RECORDS] = (
|
||||
"official_bootstrap"
|
||||
)
|
||||
overview["domain_sources"][DOMAIN_EXCHANGE_RECORDS] = "official_bootstrap"
|
||||
|
||||
overview["exchange_record_count"] = len(all_records)
|
||||
# 兑换记录开始前结束前一步可能留下的角色快照事务。
|
||||
@@ -1249,9 +1251,7 @@ class OverviewMixin:
|
||||
else:
|
||||
bind_label = "可换绑"
|
||||
elif (account.change_role_wait_time or 0) > 0:
|
||||
bind_label = (
|
||||
f"换绑冷却 {self._format_wait_time(account.change_role_wait_time)}"
|
||||
)
|
||||
bind_label = f"换绑冷却 {self._format_wait_time(account.change_role_wait_time)}"
|
||||
else:
|
||||
bind_label = "可换绑"
|
||||
elif account.role_bind_status == "pending":
|
||||
|
||||
Reference in New Issue
Block a user