unis-oa/backend/scripts/check_table_column_config.py

223 lines
9.8 KiB
Python
Raw 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
"""明细列跟主表字段同权之后,后端这几道闸跟着改了什么。
PYTHONPATH=. python3 scripts/check_table_column_config.py
用户那句原话:**"明细表里面的列,我希望也可以和主表字段一样可以进行各种配置。"**
查下来跟「只读」那次一模一样:缺的主要不是运行时的判据,而是设计器里的入口。明细格子
与主表格子共用同一只录入控件(``FormFieldControl``),控件读的就是传进去那一份对象,
所以``placeholder`` / ``maxLength`` / ``pattern`` / ``min`` / ``max`` / ``accept`` /
``maxFiles`` / ``maxSizeMb`` 这些写在列上的键,界面上摆出格子来当场就生效;后端的
``value_problems`` 本来就按列判过一遍。这一趟真正动到后端的是三件事:
1. **藏起来的那一列不卡必填**。明细列上新的「隐藏此列」复用主表那一份 ``hidden``
(环节权限到不了单列那一层,粒度是整张表),语义与「只读」同一取向:那一列屏幕上
根本不出现,人也填不进东西,卡必填等于把单子钉死在这一环。口径在
:func:`column_not_for_people`,前端镜像 ``utils/formRuntime.columnNotForPeople``。
值本身照常存、照常导出、照常参与本行公式与关联带出——只管摆不摆。它同时算"这一环
改不动的那几格",所以值的格式也不在这一环判:主表那格「默认隐藏」一直就是这个口径
(``value_problems`` 只判 ``effective_perm`` 认得动的那几格),明细列不另发明第二套。
勾「只读」的那一列不一样——它还摆在屏幕上,只是锁着,值的格式照判。
2. **取值上下限(``min`` / ``max``)提交时判**,主表与明细列一起。以前界面上
``<input type="number">`` 的 ``min`` / ``max`` 只标个红,后端压根没读那两个数——
接口直传、脚本写入的值连那只框都不经过。「权威判定在提交那一步」这条规矩不能只
对字数与附件成立。
3. **格式那几问不再跟着必填走**。``_row_problems`` 里整段原本套在「这列没勾必填就跳过」
里面,于是非必填的列填了什么都没人看:字数上限、正则、上下限、月份写法、附件三个
限制,只在必填列上成立。改成必填归必填、格式归格式,判据仍是 ``value_problems`` 那一份。
另外「列宽 / 对齐 / 合计」那三样(``width`` / ``align`` / ``aggregate``)是**纯展现**:
不改任何一格的值,也不参与提交那道闸,所以后端只在保存那一刻判"配置本身认不认识"
(:func:`column_display_problems`,与前端 ``validateTableField`` 同一把尺子),运行时
一个字都不读——与 ``readonlyStyle`` 同一处置。
钉这廿一句,全程不碰库。
"""
from __future__ import annotations
import sys
from app.service import formruntime
FAILS: list[str] = []
def check(label: str, got: object, want: object) -> None:
if got != want:
FAILS.append(f"{label}:想要 {want!r},拿到 {got!r}")
def table(columns: list[dict], **rest: object) -> dict:
return {"key": "items", "label": "费用明细", "type": "table", "columns": columns, **rest}
def rows_problems(columns: list[dict], rows: list[dict]) -> list[str]:
return formruntime._row_problems(table(columns), rows)
def submit(columns: list[dict], rows: list[dict]) -> list[str]:
return formruntime.validate_submit([table(columns)], {}, {"items": rows})
def schema(columns: list[dict]) -> list[str]:
return formruntime.validate_schema([table(columns)])
HIDDEN_CODE = {"key": "code", "label": "物料编码", "type": "text", "required": True, "hidden": True}
OPEN_NOTE = {"key": "note", "label": "事由", "type": "text", "required": True}
QTY = {"key": "qty", "label": "数量", "type": "number", "min": 5, "max": 10}
PRICE = {"key": "price", "label": "单价", "type": "money", "max": 3}
# ------------------------------------------------------- 藏起来的列不卡必填
check(
"藏起来的必填列空着也过得去(屏幕上根本没有这一格)",
rows_problems([HIDDEN_CODE], [{"code": ""}]),
[],
)
check(
"同一张表里摆着的那一列照旧卡人",
rows_problems([OPEN_NOTE, HIDDEN_CODE], [{"note": "", "code": ""}]),
["「费用明细」第 1 行的「事由」还没填写"],
)
check(
"藏起来的列里躺着的值不判格式:环节改不动的那几格不由这一环背锅(与主表「默认隐藏」同一口径)",
rows_problems([{**HIDDEN_CODE, "maxLength": 2}], [{"code": "ABCDEF"}]),
[],
)
check(
"只读那一列照旧判:勾只读的列仍在屏幕上,只是锁着(这一问走的是 effective_perm)",
rows_problems([{**HIDDEN_CODE, "hidden": False, "readonly": True, "maxLength": 2}], [{"code": "ABCDEF"}]),
["「费用明细」第 1 行的「物料编码」超出字数上限(2 字,现在 6 字)"],
)
check(
"勾掉隐藏就回到从前(这一列的值归人了)",
rows_problems([{**HIDDEN_CODE, "hidden": False}], [{"code": ""}]),
["「费用明细」第 1 行的「物料编码」还没填写"],
)
check(
"提交时:只有藏着的那一列空着 → 过得去",
submit([OPEN_NOTE, HIDDEN_CODE], [{"note": "打车", "code": ""}]),
[],
)
# ------------------------------------------- 上下限:主表与明细列都判,且各行其道
check(
"明细列的数小于最小值 → 说清是哪一行哪一列",
rows_problems([QTY], [{"qty": 1}]),
["「费用明细」第 1 行的「数量」小于配好的最小值 5"],
)
check(
"明细列的数大于最大值 → 同样说清",
rows_problems([PRICE], [{"price": 9}]),
["「费用明细」第 1 行的「单价」大于配好的最大值 3"],
)
check(
"落在上下限之间不多话;空着也不归这两问答(那是必填的事)",
rows_problems([QTY, PRICE], [{"qty": 6, "price": 2}, {"qty": "", "price": None}]),
[],
)
check(
"主表那一格同样判:上下限不是明细列的专利",
formruntime.validate_submit([{"key": "days", "label": "天数", "type": "number", "min": 1, "max": 30}], {}, {"days": 45}),
["「天数」大于配好的最大值 30"],
)
check(
"评分的 max 是满星数,不被当成取值范围判",
formruntime.validate_submit([{"key": "score", "label": "评分", "type": "rate", "max": 5}], {}, {"score": 3}),
[],
)
check(
"非必填的列也判格式:这一问不跟着必填走",
rows_problems([{"key": "note", "label": "事由", "type": "text", "maxLength": 2}], [{"note": "三个字" }]),
["「费用明细」第 1 行的「事由」超出字数上限(2 字,现在 3 字)"],
)
check(
"必填那一问照旧只管没勾着的那几样:非必填且空着不催",
rows_problems([{"key": "note", "label": "事由", "type": "text"}], [{"note": ""}]),
[],
)
# ------------------------------------------------ 设计期:列上那三样纯展现的判法
check(
"列宽写成一粒米:保存这一刻挡下来",
schema([{"key": "a", "label": "数量", "type": "number", "width": 20}]),
["「费用明细」的列「数量」的列宽要在 60-640px 之间,或留「自动」"],
)
check(
"对齐与合计写成认不出的档:同样当场说",
schema([{"key": "a", "label": "数量", "type": "number", "align": "middle", "aggregate": "total"}]),
[
"「费用明细」的列「数量」的对齐方式不认识:middle",
"「费用明细」的列「数量」的合计方式不认识:total",
],
)
check(
"给文本列配求和:不是数字也不是金额,只能数个数",
schema([{"key": "a", "label": "名称", "type": "text", "aggregate": "sum"}]),
["「费用明细」的列「名称」不是数字或金额列,合计只能数个数"],
)
check(
"上下限写反了:这一列填不出合法的数",
schema([{"key": "a", "label": "数量", "type": "number", "min": 9, "max": 2}]),
["「费用明细」的列「数量」的最小值大于最大值"],
)
check(
"配得正的列一句不多说:自动宽度 + 靠右 + 计数 + 藏着",
schema(
[
{
"key": "a",
"label": "名称",
"type": "text",
"width": 160,
"align": "right",
"aggregate": "count",
"hidden": True,
"help": "从物料库带出",
}
]
),
[],
)
check(
"数字列求和与金额列平均都是正当配置",
schema(
[
{"key": "qty", "label": "数量", "type": "number", "aggregate": "sum", "min": 1, "max": 99},
{"key": "price", "label": "单价", "type": "money", "aggregate": "avg"},
]
),
[],
)
# -------------------------------------------------- 列上的限制与主表是同一份归属表
check(
"字数上限写在列上,主表那套口径与措辞原样搬过来",
formruntime.validate_schema(
[{"key": "a", "label": "事由", "type": "text", "maxLength": 999999}]
),
["「事由」的字数上限要在 1-100000 之间"],
)
check(
"正则编译不过:写在主表与写在列上同样当场挡",
formruntime.validate_schema([table([{"key": "a", "label": "工号", "type": "text", "pattern": "([A-Z"}])]),
["「费用明细」的列「工号」的校验正则「([A-Z」编译不过,检查括号与转义"],
)
def main() -> int:
for line in FAILS:
print(f"[FAIL] {line}")
if FAILS:
print(f"共 {len(FAILS)} 句不对")
return 1
print("通过:藏着的列不卡必填、上下限提交时判(主表与明细同权)、格式那几问不再跟着必填走、"
"列宽对齐合计只在设计期判(21 句)")
return 0
if __name__ == "__main__":
sys.exit(main())