unis-oa/backend/scripts/check_read_batch.py

457 lines
27 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#!/usr/bin/env python3
"""守批量登记待阅这一条:``POST /read/view/batch`` 逐条成事、口径与单条一字不差。
待阅一屏十几条,一条条点「标记已阅」是要点十几下的体力活,所以加了一个批量口子。
批量最容易坏的是两件事,这里逐条钉住:
1. **不许把「需处理」批量洗成看过**:勾选里混着 ``requires_action`` 那几条时,
「标记已读」那一趟(``handling=False``)原样留着(回 ``skipped`` 说清是哪几条),
只有「批量处理」那一趟才把它们办掉——替人按下那一颗钮,比少一个批量按钮严重得多。
(处理意见**可以留空**:2026-11 用户提的,那一句改成选填,空格子也算表过态。)
2. **一条失败不许带走整批**:勾 5 条,其中 1 条是别人的、1 条 id 根本不存在,
那两条各自报错,剩下 3 条照旧登记并真的提交;界面拿得到逐条原因。
顺带钉住四件配套的小事:空勾选按 400 回;同一个 id 传两次只算一条;附件只跟着
真正被处理的那几条走(普通登记不收附件,与单条同一口径);登记完 ``/read/processing/count``
那个角标要跟着降下来,不然右上角一直红着。
办完的行归哪一屏另有一支判据守着(``check_done_cc_split.py``:需处理算已办、
不需处理算已阅),这一支只管批量这一路逐条成事。
单据是真发的、待阅是真落在收件人名下,收尾按 job 与标题清干净
(复用 ``check_write_payload.purge``),附件行按 id 删。缺流程或缺账号时报「跳过」。::
PYTHONPATH=. python3 scripts/check_read_batch.py
"""
from __future__ import annotations
import os
import pathlib
import sys
from datetime import datetime
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent.parent))
os.environ.setdefault("OA_SCHEDULER", "off")
from sqlalchemy import select # noqa: E402
from starlette.testclient import TestClient # noqa: E402
from app import models, schemas # noqa: E402
from app.db import SessionLocal # noqa: E402
from app.main import app # noqa: E402
from app.security import create_access_token # noqa: E402
from app.service import personref # noqa: E402
from check_write_payload import purge # noqa: E402
PROCESS_NAME = "备用金申请流程"
DATA = {
"loanType": "personalLoan", "loanReason": "批量待阅校验",
"loanItems": [{"loanNote": "校验", "loanType": "personalLoan", "loanTypeName": "校验",
"loanAmount": 1000, "returnDate": "2099-12-31"}],
"loanAmountTotal": 1000,
"payItems": [{"payee": "康文靖", "bankName": "校验银行", "bankAccount": "6222020200009999",
"payAmount": 1000, "payWay": "bank"}],
}
STAMP = datetime.now().strftime("%m%d%H%M%S")
FAILS: list[str] = []
PASSED = 0
def check(label: str, passed: bool, detail: str = "") -> bool:
"""报一条结果;``passed`` 为假就记进失败清单。返回它本身,好让调用方就此收尾。"""
global PASSED
if passed:
PASSED += 1
print(f"[通过] {label}")
else:
FAILS.append(label)
print(f"[FAIL] {label}" + (f" -> {detail}" if detail else ""))
return passed
class Api:
"""薄壳:把一次接口调用折成 ``(status, data, message)``。"""
def __init__(self) -> None:
self._client = TestClient(app, raise_server_exceptions=False)
@staticmethod
def token(person: models.User) -> str:
return create_access_token(str(person.id))[0]
def call(self, method: str, path: str, *, token: str, json: dict | None = None):
response = self._client.request(
method, f"/api/v1{path}",
headers={"Authorization": f"Bearer {token}"}, json=json)
try:
body = response.json()
except ValueError:
body = {}
body = body if isinstance(body, dict) else {}
return response.status_code, body.get("data"), str(body.get("message") or "")
def post(self, path: str, *, token: str, json: dict | None = None):
return self.call("POST", path, token=token, json=json)
def get(self, path: str, *, token: str):
return self.call("GET", path, token=token)
# 每一次批量真办成几条(marked + handled)都记在这里:最后那条角标断言按总数对账,
# 不写死数字——库里本来就可能留着几条与本次无关的待阅。
TALLY: list[int] = []
#: 单次批量上限,与 ``app/api/runtime.py`` 里的 ``BATCH_LIMIT`` 是同一个数:
#: 那里改了而这里没跟着改,下面「勾过上限」那一条就会红给你看
BATCH_LIMIT = 50
def batch(api: Api, token: str, ids: list[str], **kwargs):
"""打一次批量登记;``opinion`` / ``handling`` / ``attachments`` 都走 kwargs。"""
result = api.post(
"/read/view/batch", token=token,
json={"ids": ids, "opinion": kwargs.get("opinion", ""),
"handling": kwargs.get("handling", True),
"attachments": kwargs.get("attachments", [])})
data = result[1] or {}
TALLY.append(int(data.get("marked") or 0) + int(data.get("handled") or 0))
return result
def reads_of(db, work_id: str, identity: str) -> list[models.Read]:
db.rollback() # 接口用的是另一个 Session,快照不换就看不见刚才写的
return list(db.execute(
select(models.Read).where(models.Read.work == work_id, models.Read.identity == identity)
).scalars().all())
def dones_of(db, work_id: str, identity: str) -> list[models.ReadCompleted]:
db.rollback()
return list(db.execute(
select(models.ReadCompleted).where(
models.ReadCompleted.work == work_id, models.ReadCompleted.identity == identity)
).scalars().all())
def main() -> int: # noqa: C901 - 一条直线跑完六组断言,拆开反而要把 id 传来传去
db = SessionLocal()
process = db.execute(
select(models.Process).where(models.Process.name == PROCESS_NAME,
models.Process.enabled.is_(True))
).scalars().first()
if process is None:
print(f"[跳过] 没有已发布的「{PROCESS_NAME}」,这一项校不了")
db.close()
return 0
people = db.execute(
select(models.User).where(models.User.status == models.UserStatus.ACTIVE).order_by(models.User.id)
).scalars().all()
starter = next((row for row in people if row.role == models.ROLE_ADMIN), None)
others = [row for row in people if starter is None or row.id != starter.id]
if starter is None or len(others) < 2:
print("[跳过] 库里凑不出一个管理员加两个收件人,批量这一路校不了")
db.close()
return 0
reader, stranger = others[0], others[1]
ref = personref.ref_of
api = Api()
admin = api.token(starter)
mine = api.token(reader)
jobs: list[str] = []
titles: list[str] = [f"批量待阅门-{STAMP}"]
attachments: list[str] = []
status, data, message = api.post("/job", token=admin,
json={"process": process.id, "data": DATA, "title": titles[0]})
if status >= 400 or not data:
print(f"[跳过] 这张校验单没发起来({status} {message}),这一项校不了")
db.close()
return 0
jobs.append(str(data.get("job") or ""))
job, work_id = str(data.get("job") or ""), str(data.get("work") or "")
tasks = [row for row in (data.get("tasks") or []) if not row.get("finish_time")]
def make_reads() -> None:
"""在收件人名下摆出三条待阅:抄送需处理 / 分享 / 批注 @ 无需处理,各一条。
同一个人、同一张单、同一类待阅只留一条,所以要批量就靠三个来源各摆一条。
"""
task_id = str(tasks[0].get("id") or "")
api.post(f"/task/{task_id}/cc", token=admin,
json={"identities": [ref(reader)], "opinion": "请补一句处理意见",
"requires_action": True})
api.post(f"/work/{work_id}/share", token=admin,
json={"identities": [ref(reader)], "opinion": "分享给你看"})
api.post(f"/work/{work_id}/comments", token=admin,
json={"content": "@你看一眼就行", "mentions": [ref(reader)], "mention_action": False})
try:
if not check("这张校验单拿得到在途待办", bool(tasks), "发起没给待办"):
return 1
make_reads()
api.post(f"/work/{work_id}/share", token=admin,
json={"identities": [ref(stranger)], "opinion": "也分享给别人"})
mine_rows = {row.read_type: row for row in reads_of(db, work_id, ref(reader))}
check("收件人名下摆出三条待阅(三个来源各一条)",
{"cc", "share", "comment"} <= set(mine_rows),
f"实际 {sorted(mine_rows)}")
check("三条都还没进已阅", not dones_of(db, work_id, ref(reader)))
_, counts, _ = api.get("/read/processing/count", token=mine)
total_before = int((counts or {}).get("total") or 0)
action_id = str(getattr(mine_rows.get("cc"), "id", "") or "")
share_id = str(getattr(mine_rows.get("share"), "id", "") or "")
comment_id = str(getattr(mine_rows.get("comment"), "id", "") or "")
# 待阅的 id 一律当场抄成字符串:登记成功那一刻 ``oa_read`` 那行就没了,
# 晚一步再碰 ORM 对象是问库要一条已经不存在的记录(查账那几趟还会把快照换掉)
if not check("三条待阅都拿得到 id", bool(action_id and share_id and comment_id),
f"{action_id} / {share_id} / {comment_id}"):
raise SystemExit(1)
# --------- 1) 批量标记已读:不要求处理的直接登记,需处理的原样留着 ---------
status, data, message = batch(api, mine, [share_id, comment_id, action_id], handling=False)
check("批量标记已阅打得通", status < 400, f"{status} {message}")
check("不要求处理的那几条一次登记完",
int((data or {}).get("marked") or 0) == 2 and int((data or {}).get("handled") or 0) == 0,
str(data))
check("需处理那一条没被批量洗掉:还在待阅里、也没进已阅",
action_id in {row.id for row in reads_of(db, work_id, ref(reader))}
and action_id not in {row.read for row in dones_of(db, work_id, ref(reader))},
str(data))
skipped = (data or {}).get("skipped_items") or []
check("跳过的那条报得出是哪一条、为什么",
int((data or {}).get("skipped") or 0) == 1 and len(skipped) == 1
and skipped[0].get("id") == action_id and "处理" in str(skipped[0].get("reason")),
str(skipped))
done = [row for row in dones_of(db, work_id, ref(reader)) if row.read != action_id]
check("登记掉的两条真的进了已阅档", len(done) == 2, f"已阅 {len(done)} 条")
# --------- 2) 批量处理:带着共用处理意见,把需处理的那几条一起办掉 ---------
png = models.Attachment(name=f"批量待阅门-{STAMP}.png", extension=".png", storage="",
length=8, type="attachment", person=str(reader.id))
db.add(png)
db.commit()
attachments.append(png.id)
status, data, message = batch(api, mine, [action_id], opinion="已核对,无问题",
attachments=[png.id])
handled_row = next((row for row in dones_of(db, work_id, ref(reader))
if row.read == action_id), None)
check("批量处理:需处理那条办掉了(界面上归已办,不在已阅)",
status < 400 and int((data or {}).get("handled") or 0) == 1
and handled_row is not None
and action_id not in {row.id for row in reads_of(db, work_id, ref(reader))},
f"{status} {message} {data}")
check("处理意见与随附图片记在这条已阅上(不是抄送人来意那一句)",
handled_row is not None and handled_row.opinion == "已核对,无问题"
and handled_row.handle_time is not None
and [str(item.get("id")) for item in (handled_row.attachments or [])] == [png.id],
"" if handled_row is None else f"opinion={handled_row.opinion!r} "
f"attachments={handled_row.attachments}")
claim = db.get(models.Attachment, png.id)
check("随附的附件行认到这条已阅记录与这一单",
claim is not None and claim.type == models.Attachment.KIND_READ
and claim.business_id == handled_row.id and claim.work == work_id,
"" if claim is None else f"type={claim.type} business_id={claim.business_id} "
f"work={claim.work}")
# --------- 3) 登记完角标跟着降:列表与右上角说的是同一件事 ---------
# 两头都看,但各管各的口径。这一单上的待阅要**清空**,这是列表自己说的话,precise;
# 右上角那个数数的是这个人**所有单**的待阅——同一台 dev 库上别的脚本也在往同一个
# 收件人身上抄送与清场(check_cc_entries_to_read.py 用的就是同一个 others[0]),
# 拿它做等式会莫名红一次,所以只断言「至少降掉这一趟办成的条数」。
_, counts, _ = api.get("/read/processing/count", token=mine)
total_after = int((counts or {}).get("total") or 0)
check("这一单上的待阅这一趟全登记干净了",
not reads_of(db, work_id, ref(reader)),
f"还剩 {len(reads_of(db, work_id, ref(reader)))} 条")
check("登记完待阅角标跟着降(列表与右上角说的是同一件事)",
total_after <= total_before - sum(TALLY),
f"登记前 {total_before} / 登记后 {total_after} / 这一趟办成 {sum(TALLY)} 条")
# --------- 4) 一条失败不带走整批 ---------
api.post(f"/work/{work_id}/share", token=admin,
json={"identities": [ref(reader)], "opinion": "再分享一次"})
api.post(f"/work/{work_id}/share", token=admin,
json={"identities": [ref(stranger)], "opinion": "也分享给别人"})
mine_ids = {row.id for row in reads_of(db, work_id, ref(reader))}
other_ids = {row.id for row in reads_of(db, work_id, ref(stranger))}
if check("批量失败的这一组拿得到新的一条与别人的一条",
bool(mine_ids) and bool(other_ids), f"自己的 {mine_ids} / 别人的 {other_ids}"):
fresh_id = next(iter(mine_ids))
other_id = next(iter(other_ids))
status, data, message = batch(api, mine, [fresh_id, other_id, f"不存在-{STAMP}"])
errors = (data or {}).get("errors") or []
check("自己的那条照常登记",
status < 400 and int((data or {}).get("marked") or 0) == 1
and fresh_id not in {row.id for row in reads_of(db, work_id, ref(reader))},
f"{status} {message} {data}")
check("别人的、与根本不存在的那两条只算它们自己失败",
int((data or {}).get("failed") or 0) == 2 and len(errors) == 2
and any(item.get("id") == other_id for item in errors)
and any(item.get("id") == f"不存在-{STAMP}" for item in errors),
str(errors))
check("不是本人名下那条挡得住,且原样留在待阅里",
other_id in {row.id for row in reads_of(db, work_id, ref(stranger))},
str(errors))
# --------- 5) 边角:空勾选、重复 id、再来一次 ---------
status, _, message = batch(api, mine, [])
check("一条都没勾按 400 回,不静悄悄成功", status >= 400 and bool(message), f"{status} {message}")
api.post(f"/work/{work_id}/share", token=admin,
json={"identities": [ref(reader)], "opinion": "第三次分享"})
again_ids = {row.id for row in reads_of(db, work_id, ref(reader))}
if check("重复摆一条出来(同一个人同一类只留一条)", len(again_ids) == 1, str(again_ids)):
again_id = next(iter(again_ids))
status, data, message = batch(api, mine, [again_id, again_id])
check("同一个 id 传两次只算一条,不多登记一次",
status < 400 and int((data or {}).get("marked") or 0) == 1,
f"{status} {message} {data}")
status, data, message = batch(api, mine, [again_id])
# 这一条按新口径归 already,不再算 failed:列表页被页签缓存住时,「刚在单据页里
# 看过的那一条」还挂在屏幕上,人勾它不算做错,报成失败等于无端端吓人一句
check("对已经登记掉的那条再点一次:算 already 不算失败,也不重复进档",
status < 400 and int((data or {}).get("marked") or 0) == 0
and int((data or {}).get("failed") or 0) == 0
and int((data or {}).get("already") or 0) == 1
and len([row for row in dones_of(db, work_id, ref(reader))
if row.read == again_id]) == 1,
f"{status} {message} {data}")
# 光有个计数不够:界面上要说得出是哪一条之前办过,人才知道自己勾回来的是哪张单。
# 顺手把理由也钉住——它和 failed 那句「待阅不存在」是两回事
already_items = (data or {}).get("already_items") or []
check("已登记过的那一条在 already_items 里点得出名:哪个 id、为什么没动",
len(already_items) == 1 and str(already_items[0].get("id")) == again_id
and "已登记为已阅" in str(already_items[0].get("reason")),
str(already_items))
# --------- 6) 混选一批:那句共用的处理意见只该跟着「需处理」的那几条走 ---------
# 列表页上一屏必然混着两种:要人表态的与只是给人看的。从前不要求处理的那几条也会
# 吃下这句共用意见(mark_read 里「先来的一句占住 opinion」),于是一句「已核对」
# 变成 N 张单子的处理意见、还顺手盖上处理时间——档案里从此分不清谁真的表过态。
mixed_task = str((tasks[0] or {}).get("id") or "")
api.post(f"/task/{mixed_task}/cc", token=admin,
json={"identities": [ref(reader)], "opinion": "再来一条要处理的",
"requires_action": True})
api.post(f"/work/{work_id}/share", token=admin,
json={"identities": [ref(reader)], "opinion": "再看一条不用处理的"})
# id 一律当场抄成字符串:登记成功那一刻 oa_read 那行就没了,晚一步再碰 ORM 对象
# 是要一条已经不存在的记录(这个坑上面那条注释里写过,别在下一段里再踩一次)
mixed = {row.read_type: str(row.id) for row in reads_of(db, work_id, ref(reader))}
if check("混选这一组摆得出(需处理与不需处理各一条)",
{"cc", "share"} <= set(mixed), f"实际 {sorted(mixed)}"):
status, data, message = batch(api, mine, [mixed["cc"], mixed["share"]],
opinion="已核对,无问题")
rows = {row.read: row for row in dones_of(db, work_id, ref(reader))}
share_row = rows.get(mixed["share"])
cc_row = rows.get(mixed["cc"])
check("混选时两种各归各:一条登记、一条按意见处理",
status < 400 and int((data or {}).get("handled") or 0) == 1
and int((data or {}).get("marked") or 0) == 1,
f"{status} {message} {data}")
check("只是登记已阅的那条没被写进这句共用意见,也没被盖上处理时间",
share_row is not None and (share_row.opinion or "") != "已核对,无问题"
and share_row.handle_time is None,
f"opinion={getattr(share_row, 'opinion', None)!r} handle_time={getattr(share_row, 'handle_time', None)}")
check("需处理那条吃的正是本人这一趟写的这句意见",
cc_row is not None and cc_row.opinion == "已核对,无问题"
and cc_row.handle_time is not None,
f"opinion={getattr(cc_row, 'opinion', None)!r}")
# --------- 7) 一次勾过单次上限:被切掉的条数要如实报,不能静悄悄 ---------
# 待阅一页最多 200 条,上限是 50:从前直接切片,界面收到「已阅 50」就以为整批都办了,
# 剩下那 10 条既不算成功也不算失败。现在单列 truncated——那几条是一条都没碰。
status, data, message = batch(api, mine, [f"没有这条-{i}" for i in range(BATCH_LIMIT + 10)])
check("勾过上限时回出 truncated,被切掉的那几条不进任何一档",
status < 400 and int((data or {}).get("truncated") or 0) == 10
and int((data or {}).get("failed") or 0) == BATCH_LIMIT
and int((data or {}).get("marked") or 0) == 0
and int((data or {}).get("handled") or 0) == 0,
f"{status} {message} {data}")
status, data, message = batch(api, mine, [])
check("没勾满上限时 truncated 是 0(不是漏字段)",
status >= 400 or int((data or {}).get("truncated") or 0) == 0, f"{status} {data}")
# --------- 7b) 一张图配一批:这一张图要跟着每一条走 ---------
# 审计发现(2026-11):批量那一路把同一份 attachments 递给每一条需处理的待阅,
# 而一条附件行只认一个 business_id,于是第二条起撞「附件已被其它批注使用」——
# 勾 5 条待阅贴一张截图,只办得成 1 条。修法是后认领的那几条各插一条指针行
# (storage 仍指同一个对象,MinIO 里一份字节),不是报错也不是静默丢图。
for row in reads_of(db, work_id, ref(reader)) + dones_of(db, work_id, ref(reader)):
db.delete(row)
db.commit()
api.post(f"/task/{mixed_task}/cc", token=admin,
json={"identities": [ref(reader)], "opinion": "要处理甲", "requires_action": True})
api.post(f"/work/{work_id}/comments", token=admin,
json={"content": "@要处理乙", "mentions": [ref(reader)], "mention_action": True})
need_two = [str(row.id) for row in reads_of(db, work_id, ref(reader))
if row.requires_action]
png_many = models.Attachment(name=f"批量待阅门-多图-{STAMP}.png", extension=".png",
storage="", length=8, type="attachment", person=str(reader.id))
db.add(png_many)
db.commit()
attachments.append(png_many.id)
if check("一张图这一组摆得出(两条需处理 + 一张图)",
len(need_two) == 2, f"实际 {len(need_two)} 条"):
status, data, message = batch(api, mine, need_two,
opinion="两张一起表个态", attachments=[png_many.id])
rows_many = {row.read: row for row in dones_of(db, work_id, ref(reader))}
check("一张图配两条需处理:两条都登记掉,不再只办成一条",
status < 400 and int((data or {}).get("handled") or 0) == 2
and int((data or {}).get("failed") or 0) == 0
and len(rows_many) == 2,
f"{status} {message} {data}")
check("两条归档各带本人那句意见与那张图(图没被第一条独吞)",
all(rows_many.get(one) is not None
and rows_many[one].opinion == "两张一起表个态"
and [str(item.get("id")) for item in (rows_many[one].attachments or [])]
!= [] for one in need_two),
str({one: getattr(rows_many.get(one), "attachments", None) for one in need_two}))
claims = list(db.execute(
select(models.Attachment).where(models.Attachment.name == png_many.name)
).scalars().all())
check("附件按条各插一条指针行,指向同一个存储对象、归属互不相同",
len(claims) == 2 and len({row.storage for row in claims}) == 1
and len({row.business_id for row in claims}) == 2
and {row.business_id for row in claims}
== {row.id for row in rows_many.values()},
f"行数 {len(claims)} storage {sorted({row.storage for row in claims})} "
f"business_id {sorted({row.business_id for row in claims})}")
# --------- 7c) 批量之外的认领口径不许被带松 ---------
# 批注那一路(同一张图抢两次)仍然是错误:允许克隆是批量这一层的事,
# 全局放开等于让一条附件同时属于两条批注,档案里从此说不清谁附的。
status, data, message = batch(api, mine, [f"不存在-{STAMP}"],
opinion="带图但没办成", attachments=[png_many.id])
check("整批一条没办成时那张图不被认领,也不留孤儿行",
status < 400 and int((data or {}).get("failed") or 0) == 1
and db.get(models.Attachment, png_many.id) is not None
and db.get(models.Attachment, png_many.id).business_id
in {row.id for row in rows_many.values()},
f"{status} {message} {data}")
# --------- 8) 请求体口径:驼峰与下划线都认 ---------
body = schemas.ReadBatchBody.model_validate({"ids": ["a1", "a2"], "opinion": "x",
"handling": False, "attachments": ["f1"]})
check("批量请求体认 ids / opinion / handling / attachments",
body.ids == ["a1", "a2"] and body.handling is False and body.attachments == ["f1"],
repr(body))
body = schemas.ReadBatchBody.model_validate({"ids": ["a1"], "attachments": ["f1"]})
check("没写 handling 时默认按「带意见一起处理」走", body.handling is True, repr(body.handling))
finally:
db.rollback()
if attachments:
db.query(models.Attachment).filter(models.Attachment.id.in_(attachments))\
.delete(synchronize_session=False)
db.commit()
purge(db, jobs, titles)
db.close()
print(f"\n合计 {PASSED + len(FAILS)} 项:通过 {PASSED} / 失败 {len(FAILS)}")
return 1 if FAILS else 0
if __name__ == "__main__":
raise SystemExit(main())