forked from erp-dev/erp
feat: added ApiAuditLog
This commit is contained in:
@@ -466,3 +466,42 @@ def sync_mdy_plate_orders(
|
||||
)
|
||||
payload["task_id"] = self.request.id
|
||||
return payload
|
||||
|
||||
|
||||
@shared_task
|
||||
def save_api_audit_log(
|
||||
url: str,
|
||||
method: str,
|
||||
request_data: dict,
|
||||
query_params: dict,
|
||||
user_id: int | None,
|
||||
username: str,
|
||||
response_status: int | None,
|
||||
):
|
||||
"""
|
||||
异步保存API审计日志。
|
||||
|
||||
通过Celery队列异步执行,避免阻塞API响应。
|
||||
|
||||
参数:
|
||||
url: 请求URL路径
|
||||
method: HTTP方法(POST等)
|
||||
request_data: 请求体数据(JSON格式)
|
||||
query_params: URL查询参数
|
||||
user_id: 操作用户ID
|
||||
username: 操作用户的用户名
|
||||
response_status: HTTP响应状态码
|
||||
"""
|
||||
try:
|
||||
api_models.ApiAuditLog.objects.create(
|
||||
url=url,
|
||||
method=method,
|
||||
request_data=request_data,
|
||||
query_params=query_params,
|
||||
user_id=user_id,
|
||||
username=username,
|
||||
response_status=response_status,
|
||||
)
|
||||
logger.debug('API审计日志已保存: %s %s (user=%s, status=%s)', method, url, username, response_status)
|
||||
except Exception as e:
|
||||
logger.error('保存API审计日志失败: %s', str(e), exc_info=True)
|
||||
|
||||
Reference in New Issue
Block a user