From dd018277ad4030594985199905e698708efd83a1 Mon Sep 17 00:00:00 2001 From: "mula.liu" Date: Wed, 4 Nov 2026 01:31:15 +0800 Subject: [PATCH] =?UTF-8?q?refactor(api):=20=E5=90=8E=E7=AB=AF=E4=B8=80?= =?UTF-8?q?=E8=87=B4=E6=80=A7=E6=94=B6=E5=8F=A3=EF=BC=88=E5=AE=A1=E8=AE=A1?= =?UTF-8?q?=20B=20=E8=8A=82=E9=81=97=E7=95=99=EF=BC=89+=20=E8=A1=A5=204=20?= =?UTF-8?q?=E4=B8=AA=E7=BC=BA=E5=A4=B1=E5=A5=91=E7=BA=A6=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - GET /export/projects 归档默认从 all 改为 false——与页面默认口径、其余 5 个导出统一; 要整库留档 / 搜归档项目需显式 archived=all(导出跟随页面口径,不做隐式放宽)。 既有导出测试同步改写(原「默认 all:整库留档」口径作废)。 - GET /custom/projects 删掉零引用的 key_only 查询参数(前端 0 处使用;重点过滤在抽屉/看板内做)。 - 新增契约测试 4 个:/custom/meta 下拉全集键、定制项目删除(连带交付记录计数 + 404 复删)、 /export/projects 默认归档口径、schema_patch.ensure_columns 补列与幂等——全套 93 passed。 - README:API 前言写明两种请求体模式的分界(新建=Pydantic / 部分更新=dict+白名单,不混用); /custom/projects 参数表去 key_only;/export/projects 默认值更新。 --- README.md | 7 +++-- app/routers/custom.py | 3 --- app/routers/export.py | 3 ++- tests/test_ai_export.py | 8 +++--- tests/test_api_contract.py | 55 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 822dc7c..d486c31 100644 --- a/README.md +++ b/README.md @@ -596,6 +596,9 @@ docker-compose.yml 部署编排(./data 持久化、HOST_PORT、健康检查 鉴权走 **HttpOnly Cookie `unis_session`**(由 `/api/auth/login` 下发,只认 Cookie,不认自定义头); 除 `/api/auth/login` 与 `/health` 外,所有接口都要求已登录,否则 401。 写操作(POST/PATCH/PUT/DELETE)额外要求对应菜单是 `rw`,越权返回 403 并留审计。 +请求体约定:**新建实体**用 Pydantic 模型(`XxxIn`,校验失败 422 中文提示); +**部分更新**(定制项目、定制交付记录、人员、设置的 PATCH/PUT)收自由 JSON + 后端字段白名单 +(`payload: dict`:只更新带来的键、不认识的键忽略),加字段只改白名单一处,两种模式不要混用。 ``` # 登录与自助 @@ -640,7 +643,7 @@ PATCH|DELETE /api/updates/{id} # 定制项目 GET /api/custom/meta 阶段字典 + 区域列表 + 交付环节(pipeline) + 卡点阈值(aging_days) -GET /api/custom/projects?stage=®ion=&keyword=&archived=&key_only=&owner= +GET /api/custom/projects?stage=®ion=&keyword=&archived=&owner= POST /api/custom/projects PATCH|DELETE /api/custom/projects/{id} (owner 是单个人名;members 传人名数组整体替换;不再接受 owners,多负责人只属于重点项目) POST /api/custom/updates PATCH|DELETE /api/custom/updates/{id} @@ -674,7 +677,7 @@ GET|PUT /api/settings AI 配置等(PUT 需要 settings 可编 # 导出(均返回 xlsx,筛选口径与页面对应) GET /api/export/custom ?region=&origin=&status=&risk=&accept_plan=&keyword=&archived=false|true|all(末尾带合计行) GET /api/export/custom-updates ?period_id=|year&month -GET /api/export/projects ?category_id=&keyword=&archived=false|true|all(默认 all) +GET /api/export/projects ?category_id=&keyword=&archived=false|true|all(默认 false,与页面一致;搜归档项目记得 archived=all) GET /api/export/updates ?period_id=|year&month&project_id= GET /api/export/staff ?archived=false|true|all GET /api/export/focus ?archived=false|true|all&status=&keyword= diff --git a/app/routers/custom.py b/app/routers/custom.py index c35f3e6..67adfc9 100644 --- a/app/routers/custom.py +++ b/app/routers/custom.py @@ -232,7 +232,6 @@ def list_custom_projects( region: str | None = None, keyword: str | None = None, archived: str = Query("false", pattern="^(true|false|all)$"), - key_only: bool = False, owner: str | None = None, db: Session = Depends(get_db), ): @@ -247,8 +246,6 @@ def list_custom_projects( q = q.filter(CustomProject.region == region) if keyword: q = q.filter(CustomProject.name.contains(keyword)) - if key_only: - q = q.filter(CustomProject.is_key.is_(True)) if owner: q = q.filter(CustomProject.owner == owner) diff --git a/app/routers/export.py b/app/routers/export.py index d111f3b..d6a8fb2 100644 --- a/app/routers/export.py +++ b/app/routers/export.py @@ -177,7 +177,8 @@ def export_custom_updates( def export_projects( category_id: int | None = None, keyword: str | None = None, - archived: str = Query("all", pattern="^(true|false|all)$"), + # 与其余导出统一:不传时默认只导在用项目 + archived: str = Query("false", pattern="^(true|false|all)$"), db: Session = Depends(get_db), ): q = db.query(Project) diff --git a/tests/test_ai_export.py b/tests/test_ai_export.py index f44bd9c..048c71f 100644 --- a/tests/test_ai_export.py +++ b/tests/test_ai_export.py @@ -352,7 +352,9 @@ def test_项目导出含归档开关与统计列(h): _, rows = _sheet(h.admin.get("/api/export/projects")) names = [r[0] for r in rows[3:]] - assert "导出-常规项目" in names and "导出-已归档项目" in names # 默认 all:整库留档 + assert "导出-常规项目" in names and "导出-已归档项目" not in names # 默认与页面一致:不含归档 + _, rows = _sheet(h.admin.get("/api/export/projects", params={"archived": "all"})) + assert "导出-已归档项目" in [r[0] for r in rows[3:]] # 整库留档要显式 archived=all row = next(r for r in rows[3:] if r[0] == "导出-常规项目") assert row[3] == "进行中" and row[5] == "是" and row[9] == 1 assert row[10] and "第" in row[10] # 最近更新周期写了周次 @@ -360,5 +362,5 @@ def test_项目导出含归档开关与统计列(h): _, rows = _sheet(h.admin.get("/api/export/projects", params={"archived": "false"})) names = [r[0] for r in rows[3:]] assert "导出-已归档项目" not in names - _, rows = _sheet(h.admin.get("/api/export/projects", params={"keyword": "归档"})) - assert [r[0] for r in rows[3:]] == ["导出-已归档项目"] + _, rows = _sheet(h.admin.get("/api/export/projects", params={"keyword": "归档", "archived": "all"})) + assert [r[0] for r in rows[3:]] == ["导出-已归档项目"] # 搜归档项目要显式放开归档口径 diff --git a/tests/test_api_contract.py b/tests/test_api_contract.py index 5ddf3b5..3f41ec8 100644 --- a/tests/test_api_contract.py +++ b/tests/test_api_contract.py @@ -69,3 +69,58 @@ def test_录入页跨菜单依赖(h): # 连 custom 读都没有的其它菜单账号(如只有 staff rw)不享受 assist s = h.user_client("staff_only", menus={"staff": "rw"}) assert s.get("/api/custom/projects").status_code == 403 + + +def test_定制meta下拉全集(h): + """录入页 / 定制页的下拉选项唯一来源;前端不再各写一份。""" + d = h.admin.get("/api/custom/meta").json() + for key in ("stages", "regions", "region_groups", "pipeline", "aging_days", + "statuses", "origins", "placed_origins", "industries", "risks"): + assert key in d, key + assert d["stages"] and all({"code", "name", "color", "base_ratio"} <= set(s) for s in d["stages"]) + assert d["pipeline"] and all("date_field" in s for s in d["pipeline"]) + assert isinstance(d["aging_days"], int) and d["aging_days"] > 0 + + +def test_定制项目删除连带交付记录(h): + cp = h.custom("待删除的定制项目") + h.custom_update(cp["id"], h.today_period_id, content="删之前的一条记录") + r = h.admin.delete("/api/custom/projects/" + str(cp["id"])) + assert r.status_code == 200, r.text + body = r.json() + assert body["ok"] and body["name"] == "待删除的定制项目" and body["deleted_updates"] == 1 + ids = [x["id"] for x in h.admin.get("/api/custom/projects?archived=all").json()] + assert cp["id"] not in ids + assert h.admin.delete("/api/custom/projects/" + str(cp["id"])).status_code == 404 + + +def test_导出项目默认不含归档(h): + """GET /export/projects 与其余导出统一:不传 archived 时只导在用项目。""" + keep = h.project("导出默认口径-在用") + gone = h.project("导出默认口径-归档") + h.admin.patch("/api/projects/" + str(gone["id"]), json={"archived": True}) + xlsx = h.admin.get("/api/export/projects") + assert xlsx.status_code == 200 + from openpyxl import load_workbook + import io + names = {c.value for row in load_workbook(io.BytesIO(xlsx.content)).active.iter_rows(min_row=2) for c in row if isinstance(c.value, str)} + assert "导出默认口径-在用" in names and "导出默认口径-归档" not in names + both = load_workbook(io.BytesIO(h.admin.get("/api/export/projects?archived=all").content)).active + names_all = {c.value for row in both.iter_rows(min_row=2) for c in row if isinstance(c.value, str)} + assert "导出默认口径-归档" in names_all + + +def test_ensure_columns_补列与幂等(h): + """老库补列通道:缺的可空列会补上;再跑一次不重复补、不报错。""" + from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine, inspect, text + from app.schema_patch import ensure_columns + + eng = create_engine("sqlite:///:memory:") + with eng.begin() as conn: + conn.execute(text("CREATE TABLE demo (id INTEGER PRIMARY KEY, name TEXT)")) + md = MetaData() + Table("demo", md, Column("id", Integer, primary_key=True), + Column("name", String), Column("extra", String)) + assert ensure_columns(eng, md) == ["demo.extra"] + assert "extra" in {c["name"] for c in inspect(eng).get_columns("demo")} + assert ensure_columns(eng, md) == [] # 幂等