#!/usr/bin/env python3 """守「管理员在干预面板改了表单,列表上那一行跟着变了没有」。 流程干预的「修正表单」改的常常就是标题那一格(`type=title` 的字段 = ``work.title``)。 待办列表、待阅列表、监控列表读的都是各行自己那一份 ``title``,不回查单据,所以 ``engine.fix_data`` 只写 ``work.data`` 就会出现用户报的那个现象:面板里明明改好了、 上面那张卡片的标题也换了,列表上一行还是老那一句。这一篇钉的就是改完之后的对齐: 1. **标题格归人填**(``titleMode=manual``):管理员改掉那一格,单据标题、在途待办、 抄送出去的待阅三处当场换成新那一句,监控列表按新标题也搜得到这张单; 2. **规则开了自动重算**(``refresh=true``):管理员改的是规则引用的字段(事由), 标题按同一条规则重算,并同步回表单里那一格(``work.data`` 与 ``work.title`` 不分家); 3. **规则没开自动重算**(``refresh=false``):改规则引用的字段**不该**动标题—— 这一条是反向闸,别把「对齐」做成「每次修正都替人重取一次名字」;但直接改标题格照改; 4. **保存前的预告**:``POST /job/{job}/title/preview`` 是干预面板上那句「保存后标题会变成 …」的来源,监控权限的人问得动(数据范围同 ``/admin/work/{job}/data`` 那一道筛), 改标题格要说「会变」,没开自动重算又只改了别的字段要说「不会变」。 5. **治老账**:这一层修好之前留下的单子,表单那一格是新的一句、``work.title`` 还是老那一句。 重新保存一次(哪怕交上来的值跟库里一模一样)就把三处对齐;对齐之后再交同样的值才算没变化。 跑法(用当前代码,不依赖开发服开没开;建的东西最后自己删干净):: PYTHONPATH=. python3 scripts/check_intervention_title.py """ from __future__ import annotations import sys from sqlalchemy import select from starlette.testclient import TestClient from app import models from app.db import SessionLocal from app.main import app PREFIX = "校验流程-干预标题同步" FORM_PREFIX = "校验表单-干预标题同步" PASSED: list[str] = [] FAILED: list[str] = [] def check(name: str, condition: bool, detail: object = "") -> bool: (PASSED if condition else FAILED).append(name) suffix = "" if condition else f" :: {str(detail)[:300]}" print(f"[{'PASS' if condition else 'FAIL'}] {name}{suffix}") return condition class Api: """薄薄一层:5xx 当场停下来说清在哪一步,业务失败由调用方自己判。""" def __init__(self, client: TestClient, token: str) -> None: self.client = client self.token = token def call(self, method: str, path: str, body: dict | None = None, params: dict | None = None) -> dict: response = self.client.request( method, path, json=body, params=params, headers={"Authorization": f"Bearer {self.token}"} ) try: payload = response.json() except ValueError: payload = {"code": response.status_code, "message": response.text[:200]} if response.status_code >= 500: raise SystemExit(f"{method} {path} 服务端出错:{payload}") return payload def must(self, name: str, method: str, path: str, body: dict | None = None): payload = self.call(method, path, body) if payload.get("code") != 0: raise SystemExit(f"{name}失败:{method} {path} -> {payload}") return payload.get("data") def items(self, name: str, path: str, query: dict | None = None) -> list[dict]: """清单接口拿那一页 ``items``;筛选条件走 query string,别塞进 json body。""" payload = self.call("GET", path, None, query) if payload.get("code") != 0: raise SystemExit(f"{name}失败:GET {path} -> {payload}") return (payload.get("data") or {}).get("items") or [] def title_field(mode: str, rule: dict | None) -> dict: """表单里那格「流程标题」:``manual`` 归人填,``rule`` 听规则的。""" field = {"key": "subject", "label": "流程标题", "type": "title", "span": 12, "row": 1, "col": 1, "titleMode": mode} if rule: field["titleRule"] = rule return field def fields(title: dict) -> list[dict]: return [ title, {"key": "reason", "label": "申请事由", "type": "text", "required": True, "span": 6, "row": 2, "col": 1}, {"key": "amount", "label": "报销金额", "type": "money", "span": 6, "row": 2, "col": 7}, ] def activities(form_id: str, pfx: str) -> list[dict]: """环节 id 是全局主键:每条流程用自己那套前缀,撞了会被后端换号。""" return [ {"id": f"{pfx}_begin", "type": "begin", "name": "开始", "x": 40, "y": 200, "form": form_id}, {"id": f"{pfx}_first", "type": "manual", "name": "一级审批", "x": 240, "y": 200, "form": form_id, # 审批人就是登录人自己,关掉「发起人自审跳过」这一环才会真的停下来 "config": {"mode": "any", "taskValues": [{"type": "creator", "data": []}], "skipStarter": False}}, {"id": f"{pfx}_end", "type": "end", "name": "结束", "x": 440, "y": 200}, ] def routes(pfx: str) -> list[dict]: return [ {"id": f"{pfx}r1", "name": "提交", "from_activity": f"{pfx}_begin", "to_activity": f"{pfx}_first"}, {"id": f"{pfx}r2", "name": "同意", "from_activity": f"{pfx}_first", "to_activity": f"{pfx}_end"}, ] def build(api: Api, suffix: str, title: dict, pfx: str) -> tuple[str, str]: """建一张自检表单 + 一条发布好的流程,回 ``(表单 id, 流程 id)``。""" form_id = api.must("建表单", "POST", "/process/forms", { "name": f"{FORM_PREFIX}-{suffix}", "description": "干预改标题自检用", "content": {"fields": fields(title), "layout": {"columns": 12, "grid": 12, "labelWidth": 100}}, })["id"] process_id = api.must("建流程", "POST", "/process/processes", { "name": f"{PREFIX}-{suffix}", "description": "干预改标题自检", })["id"] api.must("保存设计", "PUT", f"/process/processes/{process_id}/design", { "activities": activities(form_id, pfx), "routes": routes(pfx), "latest_form": form_id, "canvas": {"edge_style": "orthogonal", "edge_radius": 8}, "approval_config": {"skipStarter": False, "dedup": "none"}, }) problems = (api.call("POST", f"/process/processes/{process_id}/validate").get("data") or {}).get("problems") or [] check(f"{suffix}:流程通过发布前校验", not problems, problems) api.must("发布", "POST", f"/process/processes/{process_id}/publish", {"comment": "干预改标题自检"}) return form_id, process_id def start(api: Api, process_id: str, reason: str, subject: str = "") -> str: return api.must("发起", "POST", "/job", { "process": process_id, "title": "", "data": {"reason": reason, "subject": subject, "amount": 100}, })["job"] def detail_of(api: Api, job: str) -> dict: return api.must("读单据", "GET", f"/job/{job}") or {} def todo_of(api: Api, job: str) -> dict: items = api.items("读待办", "/task/processing", {"page_size": 200}) return next((item for item in items if item.get("job") == job), {}) def read_of(api: Api, job: str) -> dict: items = api.items("读待阅", "/read/processing", {"page_size": 200}) return next((item for item in items if item.get("job") == job), {}) def monitor_rows(api: Api, keyword: str) -> list[dict]: """监控页那张单子列表——用户口中「展示列表」最典型的一处。""" return api.items("读监控列表", "/admin/work/list", {"keyword": keyword, "page_size": 200}) def created_rows(api: Api, keyword: str) -> list[dict]: return api.items("读我发起的", "/work/created", {"keyword": keyword, "page_size": 200}) def cc_to_self(api: Api, job: str) -> None: """抄送给自己,落一条待阅:待阅列表读的是 ``oa_read.title``,也得跟着改名。""" task = todo_of(api, job) if not task: raise SystemExit(f"{job} 手上没有待办,抄不了待阅") api.must("抄送", "POST", f"/task/{task['id']}/cc", {"identities": ["localadmin"], "opinion": "看一下"}) def fix(api: Api, job: str, data: dict, opinion: str = "管理员自检修正") -> dict: return api.must("修正表单", "POST", f"/admin/work/{job}/data", {"data": data, "opinion": opinion}) def purge(api: Api) -> None: """上一轮跑剩下的流程、表单与在途单先清掉,别在库里堆同名垃圾。""" for row in api.must("读流程", "GET", "/process/processes") or []: if not str(row.get("name") or "").startswith(PREFIX): continue process_id = row["id"] for status in ("running", "completed"): items = api.items("读在途单", "/work/created", {"process": process_id, "status": status}) for item in items: api.call("POST", f"/job/{item.get('job') or item.get('id')}/terminate", {"opinion": "自检数据重置"}) api.call("DELETE", f"/process/processes/{process_id}") for row in api.must("读表单", "GET", "/process/forms") or []: if str(row.get("name") or "").startswith(FORM_PREFIX): api.call("DELETE", f"/process/forms/{row['id']}") def case_manual_field(api: Api) -> None: print("\n--- 标题格归人填:管理员改掉那一格,三处列表一起改名 ---") _, process_id = build(api, "人填", title_field("manual", None), "ia") job = start(api, process_id, "10月差旅", subject="2026-10旧的那句报销") detail = detail_of(api, job) cc_to_self(api, job) check("人填:发起回来的标题就是那一格写的", detail.get("title") == "2026-10旧的那句报销", detail.get("title")) check("人填:待办那一行带着同一句标题", todo_of(api, job).get("title") == detail.get("title"), todo_of(api, job).get("title")) check("人填:抄送出去的待阅也带着同一句标题", read_of(api, job).get("title") == detail.get("title"), read_of(api, job).get("title")) fixed = fix(api, job, {"subject": "2026年10月吕志威VDI项目差旅费报销费用流程"}) check("人填:修正接口回来的单据已经是新标题", fixed.get("title") == "2026年10月吕志威VDI项目差旅费报销费用流程", fixed.get("title")) detail = detail_of(api, job) check("人填:详情页标题跟着换", detail.get("title") == fixed.get("title"), detail.get("title")) check("人填:表单里那一格与新标题一字不差", (detail.get("data") or {}).get("subject") == fixed.get("title"), (detail.get("data") or {}).get("subject")) check("人填:待办列表那一行改名的同步换(不是只有详情变了)", todo_of(api, job).get("title") == fixed.get("title"), todo_of(api, job).get("title")) check("人填:待阅列表那一行也换", read_of(api, job).get("title") == fixed.get("title"), read_of(api, job).get("title")) check("人填:监控列表按新标题搜得到这张单", any(item.get("job") == job for item in monitor_rows(api, "VDI项目差旅费")), [item.get("title") for item in monitor_rows(api, "VDI项目差旅费")]) check("人填:监控列表不再留着老那一句", not any(item.get("job") == job for item in monitor_rows(api, "2026-10旧的那句报销")), [item.get("title") for item in monitor_rows(api, "2026-10旧的那句报销")]) check("人填:我发起的列表也是新那一句", any(item.get("job") == job for item in created_rows(api, "VDI项目差旅费")), [item.get("title") for item in created_rows(api, "VDI项目差旅费")]) logs = [str(item.get("from_opinion") or "") for item in detail.get("work_logs") or []] check("人填:流转记录写清了旧值 → 新值", any("2026-10旧的那句报销" in line and "VDI项目差旅费" in line for line in logs), logs[:4]) def case_rule_refresh(api: Api) -> None: print("\n--- 规则开了自动重算:改规则引用的字段,标题与那一格一起换 ---") rule = {"mode": "template", "template": "{发起人}|{reason}", "editable": True, "refresh": True} _, process_id = build(api, "自动重算", title_field("rule", rule), "ib") job = start(api, process_id, "10月差旅") before = detail_of(api, job) check("自动重算:发起时按规则算出一句,并写回标题那一格", before.get("title") == (before.get("data") or {}).get("subject") and str(before.get("title") or "").endswith("|10月差旅"), (before.get("title"), (before.get("data") or {}).get("subject"))) fixed = fix(api, job, {"reason": "11月差旅(改过)"}) check("自动重算:改的是事由,标题按同一条规则重算", str(fixed.get("title") or "").endswith("|11月差旅(改过)"), fixed.get("title")) detail = detail_of(api, job) check("自动重算:表单里那一格同步换成重算出来的那句", (detail.get("data") or {}).get("subject") == detail.get("title"), ((detail.get("data") or {}).get("subject"), detail.get("title"))) check("自动重算:在途待办那一行跟着改名", todo_of(api, job).get("title") == detail.get("title"), todo_of(api, job).get("title")) def case_rule_frozen(api: Api) -> None: print("\n--- 规则没开自动重算:改字段不许替人重取名字,改标题格照改 ---") rule = {"mode": "template", "template": "{发起人}|{reason}", "editable": True, "refresh": False} _, process_id = build(api, "不自动重算", title_field("rule", rule), "ic") job = start(api, process_id, "10月差旅") title = detail_of(api, job).get("title") untouched = fix(api, job, {"reason": "11月差旅(refresh 关着)"}) check("不自动重算:只改事由时标题原样留着(refresh=false 说了算)", untouched.get("title") == title, (untouched.get("title"), title)) renamed = fix(api, job, {"subject": "管理员直接改的标题"}) detail = detail_of(api, job) check("不自动重算:直接改标题那一格照样算改标题", renamed.get("title") == "管理员直接改的标题", renamed.get("title")) check("不自动重算:待办那一行也照改", todo_of(api, job).get("title") == detail.get("title"), (todo_of(api, job).get("title"), detail.get("title"))) check("不自动重算:标题没变的那次修正不该顺手改名单(对照上面 untouched)", detail.get("title") == "管理员直接改的标题", detail.get("title")) def case_preview(api: Api) -> None: print("\n--- 保存前的预告:面板上那句「保存后标题会变成 …」 ---") rule = {"mode": "template", "template": "{发起人}|{reason}", "editable": True, "refresh": True} _, process_id = build(api, "预告", title_field("rule", rule), "id") job = start(api, process_id, "10月差旅") current = detail_of(api, job).get("title") got = api.must("预告改标题格", "POST", f"/job/{job}/title/preview", {"data": {"subject": "将要换的那一句"}}) check("预告:改标题格会说「会变」,并给出会变成的那一句", got.get("changed") is True and got.get("title") == "将要换的那一句", got) check("预告:说的是当前这句与新句的对照", got.get("current") == current, got.get("current")) got = api.must("预告改规则引用的字段", "POST", f"/job/{job}/title/preview", {"data": {"reason": "12月差旅"}}) check("预告:改了规则引用的字段、且规则开了自动重算 → 也会变", got.get("changed") is True and str(got.get("title") or "").endswith("|12月差旅"), got) frozen_rule = {"mode": "template", "template": "{发起人}|{reason}", "editable": True, "refresh": False} _, frozen = build(api, "预告不重算", title_field("rule", frozen_rule), "ie") quiet = start(api, frozen, "10月差旅") got = api.must("预告(refresh 关着)", "POST", f"/job/{quiet}/title/preview", {"data": {"reason": "12月差旅"}}) check("预告:规则没开自动重算时不该给一个不兑现的预告", got.get("changed") is False and got.get("refresh") is False, got) check("预告:没这张单可见范围的人问不动(数据范围与干预接口同一道筛)", api.call("POST", "/job/no-such-job/title/preview", {"data": {}}).get("code") != 0) def case_repair(api: Api) -> None: print("\n--- 治老账:那一格与标题对不上时,重新保存一次就对齐 ---") _, process_id = build(api, "对齐老账", title_field("manual", None), "if") job = start(api, process_id, "10月差旅", subject="发起时写的那句") cc_to_self(api, job) drift = "歪在表单里的那一句" # 直接改库绕过引擎,还原成「修正表单不同步标题」那一版留下的样子 with SessionLocal() as db: row = db.execute(select(models.Work).where(models.Work.job == job)).scalar_one() data = dict(row.data or {}) data["subject"] = drift row.data = data db.commit() detail = detail_of(api, job) check("老账:库里那一格是新的一句,标题还是老那一句(这一层修好之前的样子)", (detail.get("data") or {}).get("subject") == drift and detail.get("title") == "发起时写的那句", ((detail.get("data") or {}).get("subject"), detail.get("title"))) fixed = fix(api, job, {"subject": drift}) check("老账:交一份与库里一样的值也认(有标题要对齐,不该回「没有变化」)", fixed.get("title") == drift, fixed) check("老账:单据、待办、待阅三处一起对齐", detail_of(api, job).get("title") == drift and todo_of(api, job).get("title") == drift and read_of(api, job).get("title") == drift, (todo_of(api, job).get("title"), read_of(api, job).get("title"))) logs = [str(item.get("from_opinion") or "") for item in detail_of(api, job).get("work_logs") or []] check("老账:留痕写清了旧标题 → 新标题", any("发起时写的那句" in line and drift in line for line in logs), logs[:4]) again = api.call("POST", f"/admin/work/{job}/data", {"data": {"subject": drift}, "opinion": "再交一次"}) check("老账:对齐之后再交同样的值,才真的算没变化(那一句挡回来)", again.get("code") != 0 and "没有变化" in str(again.get("message") or ""), again) def main() -> int: with TestClient(app, base_url="http://testserver/api/v1") as client: login = client.post("/auth/login", json={"account": "localadmin", "password": "admin123"}).json() if login.get("code") != 0: raise SystemExit(f"登录失败(本地管理员账号口令与 config 里的 admin_password 对不上?):{login}") api = Api(client, login["data"]["access_token"]) print(f"=== 干预改标题自检:{PREFIX} ===") purge(api) case_manual_field(api) case_rule_refresh(api) case_rule_frozen(api) case_preview(api) case_repair(api) print("\n--- 收尾:清掉本轮建的东西 ---") purge(api) print(f"\n通过 {len(PASSED)} / 失败 {len(FAILED)}") for item in FAILED: print(f" FAIL {item}") return 1 if FAILED else 0 if __name__ == "__main__": sys.exit(main())