feat(oa): add owner person/dept fields with initiator defaults
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
3b46b10604
commit
4ebc2dbe82
18 files changed
+791
-15
No files matched your search
@@ -125,11 +125,17 @@ def _validate_dept_rule(rule: dict, data: Dict[str, Any]) -> List[str]:
|
||||
|
||||
|
||||
def validate_dept_form_values(schema: Any, form_data: Optional[Dict[str, Any]]) -> List[str]:
|
||||
"""校验 form_data 中部门选择控件的值格式,返回中文错误列表(空表示通过)。"""
|
||||
"""校验 form_data 中部门 / 归属部门控件的值格式,返回中文错误列表(空表示通过)。"""
|
||||
data = form_data or {}
|
||||
errors: List[str] = []
|
||||
for rule in _schema_rules(schema):
|
||||
if str(rule.get("type") or "") != "dept":
|
||||
continue
|
||||
errors.extend(_validate_dept_rule(rule, data))
|
||||
typ = str(rule.get("type") or "")
|
||||
if typ == "dept":
|
||||
errors.extend(_validate_dept_rule(rule, data))
|
||||
elif typ == "ownerDept":
|
||||
forced = dict(rule)
|
||||
props = dict(_rule_props(rule))
|
||||
props["multiple"] = False
|
||||
forced["props"] = props
|
||||
errors.extend(_validate_dept_rule(forced, data))
|
||||
return errors
|
||||
@@ -0,0 +1,81 @@
|
||||
"""OA 归属人 / 归属部门:只读强制回填与校验辅助"""
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Any, Dict, List, Optional
|
||||
|
||||
|
||||
def _walk_rules(rules: Any, out: List[dict]) -> None:
|
||||
if not isinstance(rules, list):
|
||||
return
|
||||
for r in rules:
|
||||
if not isinstance(r, dict):
|
||||
continue
|
||||
out.append(r)
|
||||
children = r.get("children") or (r.get("props") or {}).get("rule") or []
|
||||
if isinstance(children, list):
|
||||
_walk_rules(children, out)
|
||||
|
||||
|
||||
def _schema_rules(schema: Any) -> List[dict]:
|
||||
if isinstance(schema, list):
|
||||
out: List[dict] = []
|
||||
_walk_rules(schema, out)
|
||||
return out
|
||||
if not isinstance(schema, dict):
|
||||
return []
|
||||
out: List[dict] = []
|
||||
_walk_rules(schema.get("rule") or schema.get("rules") or [], out)
|
||||
return out
|
||||
|
||||
|
||||
def _rule_props(rule: dict) -> dict:
|
||||
props = rule.get("props")
|
||||
return props if isinstance(props, dict) else {}
|
||||
|
||||
|
||||
def _allow_modify(rule: dict) -> bool:
|
||||
return _rule_props(rule).get("allowModify") is True
|
||||
|
||||
|
||||
def apply_owner_field_defaults(
|
||||
schema: Any,
|
||||
form_data: Optional[Dict[str, Any]],
|
||||
*,
|
||||
user_id: str,
|
||||
user_name: str,
|
||||
dept_id: Optional[str],
|
||||
dept_name: Optional[str],
|
||||
) -> Dict[str, Any]:
|
||||
"""
|
||||
归属字段处理:
|
||||
- allowModify 为假:强制写入当前发起人 / 主部门(防篡改)
|
||||
- allowModify 为真且字段为空:补默认值
|
||||
"""
|
||||
data = dict(form_data or {})
|
||||
uid = (user_id or "").strip()
|
||||
uname = (user_name or "").strip() or uid
|
||||
did = (dept_id or "").strip()
|
||||
dname = (dept_name or "").strip() or did
|
||||
|
||||
person_val = {"id": uid, "name": uname} if uid else None
|
||||
dept_val = {"id": did, "name": dname} if did else None
|
||||
|
||||
for rule in _schema_rules(schema):
|
||||
typ = str(rule.get("type") or "")
|
||||
field = str(rule.get("field") or "").strip()
|
||||
if not field:
|
||||
continue
|
||||
allow = _allow_modify(rule)
|
||||
cur = data.get(field)
|
||||
empty = cur is None or cur == "" or cur == [] or (
|
||||
isinstance(cur, dict) and not str(cur.get("id") or "").strip()
|
||||
)
|
||||
|
||||
if typ == "ownerPerson" and person_val:
|
||||
if not allow or empty:
|
||||
data[field] = dict(person_val)
|
||||
elif typ == "ownerDept" and dept_val:
|
||||
if not allow or empty:
|
||||
data[field] = dict(dept_val)
|
||||
|
||||
return data
|
||||
@@ -125,13 +125,20 @@ def _validate_person_rule(rule: dict, data: Dict[str, Any]) -> List[str]:
|
||||
|
||||
|
||||
def validate_person_form_values(schema: Any, form_data: Optional[Dict[str, Any]]) -> List[str]:
|
||||
"""校验 form_data 中人员选择控件的值格式,返回中文错误列表(空表示通过)。"""
|
||||
"""校验 form_data 中人员 / 归属人控件的值格式,返回中文错误列表(空表示通过)。"""
|
||||
data = form_data or {}
|
||||
errors: List[str] = []
|
||||
for rule in _schema_rules(schema):
|
||||
if str(rule.get("type") or "") != "person":
|
||||
continue
|
||||
errors.extend(_validate_person_rule(rule, data))
|
||||
typ = str(rule.get("type") or "")
|
||||
if typ == "person":
|
||||
errors.extend(_validate_person_rule(rule, data))
|
||||
elif typ == "ownerPerson":
|
||||
# 归属人固定单选
|
||||
forced = dict(rule)
|
||||
props = dict(_rule_props(rule))
|
||||
props["multiple"] = False
|
||||
forced["props"] = props
|
||||
errors.extend(_validate_person_rule(forced, data))
|
||||
return errors
|
||||
|
||||
|
||||
|
||||
@@ -104,7 +104,7 @@ def _format_value(typ: str, raw: Any) -> Tuple[str, bool]:
|
||||
else:
|
||||
n = 0
|
||||
return f"已关联 {n} 条", False
|
||||
if (typ == "dept" or typ == "person" or typ == "post"):
|
||||
if (typ == "dept" or typ == "person" or typ == "post" or typ == "ownerDept" or typ == "ownerPerson"):
|
||||
if isinstance(raw, list):
|
||||
names = []
|
||||
for x in raw:
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import unittest
|
||||
|
||||
from app.services.oa_dept_select import validate_dept_form_values
|
||||
from app.services.oa_owner_fields import apply_owner_field_defaults
|
||||
from app.services.oa_person_select import validate_person_form_values
|
||||
|
||||
|
||||
class TestOaOwnerFields(unittest.TestCase):
|
||||
def test_force_overwrite_when_not_allow_modify(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "ownerPerson",
|
||||
"field": "op1",
|
||||
"title": "归属人",
|
||||
"props": {"allowModify": False},
|
||||
},
|
||||
{
|
||||
"type": "ownerDept",
|
||||
"field": "od1",
|
||||
"title": "归属部门",
|
||||
"props": {"allowModify": False},
|
||||
},
|
||||
]
|
||||
}
|
||||
data = apply_owner_field_defaults(
|
||||
schema,
|
||||
{
|
||||
"op1": {"id": "hack", "name": "黑客"},
|
||||
"od1": {"id": "d-hack", "name": "假部门"},
|
||||
},
|
||||
user_id="u1",
|
||||
user_name="张三",
|
||||
dept_id="d1",
|
||||
dept_name="研发部",
|
||||
)
|
||||
self.assertEqual(data["op1"], {"id": "u1", "name": "张三"})
|
||||
self.assertEqual(data["od1"], {"id": "d1", "name": "研发部"})
|
||||
|
||||
def test_fill_empty_when_allow_modify(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "ownerPerson",
|
||||
"field": "op1",
|
||||
"props": {"allowModify": True},
|
||||
}
|
||||
]
|
||||
}
|
||||
data = apply_owner_field_defaults(
|
||||
schema,
|
||||
{},
|
||||
user_id="u1",
|
||||
user_name="张三",
|
||||
dept_id="d1",
|
||||
dept_name="研发部",
|
||||
)
|
||||
self.assertEqual(data["op1"], {"id": "u1", "name": "张三"})
|
||||
|
||||
def test_keep_user_value_when_allow_modify(self):
|
||||
schema = {
|
||||
"rule": [
|
||||
{
|
||||
"type": "ownerPerson",
|
||||
"field": "op1",
|
||||
"props": {"allowModify": True},
|
||||
}
|
||||
]
|
||||
}
|
||||
data = apply_owner_field_defaults(
|
||||
schema,
|
||||
{"op1": {"id": "u2", "name": "李四"}},
|
||||
user_id="u1",
|
||||
user_name="张三",
|
||||
dept_id="d1",
|
||||
dept_name="研发部",
|
||||
)
|
||||
self.assertEqual(data["op1"], {"id": "u2", "name": "李四"})
|
||||
|
||||
def test_validate_owner_person_single(self):
|
||||
schema = {
|
||||
"rule": [{"type": "ownerPerson", "field": "op1", "title": "归属人"}]
|
||||
}
|
||||
self.assertEqual(
|
||||
validate_person_form_values(
|
||||
schema, {"op1": {"id": "u1", "name": "张三"}}
|
||||
),
|
||||
[],
|
||||
)
|
||||
errs = validate_person_form_values(
|
||||
schema, {"op1": [{"id": "u1", "name": "张三"}]}
|
||||
)
|
||||
self.assertTrue(any("单个" in e for e in errs))
|
||||
|
||||
def test_validate_owner_dept_single(self):
|
||||
schema = {
|
||||
"rule": [{"type": "ownerDept", "field": "od1", "title": "归属部门"}]
|
||||
}
|
||||
self.assertEqual(
|
||||
validate_dept_form_values(schema, {"od1": {"id": "d1", "name": "研发"}}),
|
||||
[],
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user