feat(audit): 优化审计日志的人性化展示
This commit is contained in:
@ -118,7 +118,7 @@ def insert_audit_log(connection, action, target, details):
|
||||
target_id = str(target.bom_no)
|
||||
|
||||
# 获取目标名称(用于展示)
|
||||
target_name = target_id
|
||||
target_name = ''
|
||||
for name_field in ['name', 'title', 'material_name', 'product_name', 'display_name', 'username']:
|
||||
if hasattr(target, name_field):
|
||||
val = getattr(target, name_field)
|
||||
@ -126,6 +126,27 @@ def insert_audit_log(connection, action, target, details):
|
||||
target_name = str(val)
|
||||
break
|
||||
|
||||
# 如果当前表没名字,但它有关联的物料对象 (比如 material.name)
|
||||
if not target_name and hasattr(target, 'material') and target.material:
|
||||
target_name = getattr(target.material, 'name', '')
|
||||
|
||||
# 如果当前表有 material_id,尝试从关联的 material 表查询名称
|
||||
if not target_name and hasattr(target, 'material_id') and target.material_id:
|
||||
try:
|
||||
# 使用 connection 查询物料表获取名称
|
||||
result = connection.execute(
|
||||
text("SELECT name FROM material_base WHERE id = :id"),
|
||||
{'id': target.material_id}
|
||||
).fetchone()
|
||||
if result:
|
||||
target_name = str(result[0])
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# 如果实在找不到名字,再用 表名 + ID 兜底
|
||||
if not target_name:
|
||||
target_name = f"{tablename} ID:{target_id}"
|
||||
|
||||
user_info = get_current_user_info()
|
||||
|
||||
# 推断模块名称
|
||||
|
||||
Reference in New Issue
Block a user