This commit is contained in:
hackrobot
2024-07-18 10:42:46 +08:00
parent 73b7c3104d
commit d264cc65ba
2 changed files with 9 additions and 2 deletions

View File

@@ -40,14 +40,21 @@ def handle_change(mapper, connection, target):
"""监听器函数"""
if target.__class__.__name__ == 'User': # 确保只监听User类的变动
changed_fields = []
original_value=None
new_value=None
for attr in target.__mapper__.column_attrs:
history = get_history(target, attr.key)
if history.has_changes():
changed_fields.append(attr.key)
original_value = history.deleted[0] if history.deleted else None
new_value = history.added[0] if history.added else None
if changed_fields:
print(f"检测到用户 {target.id} 的字段变化: {changed_fields}")
msg=f"检测到用户 {target.id} 的字段变化: {changed_fields}"
print(f"检测到用户 {target.nickname}-{target.remark} 的字段变化: {changed_fields}-{new_value}")
msg=f"检测到用户 {target.nickname}-{target.remark} 的字段变化: {changed_fields}-{new_value}"
sengTgmesage(msg)
# 发起HTTP请求
# requests.post("http://your-fastapi-endpoint/notify_change", json={"user_id": target.id, "changed_fields": changed_fields})