28 lines
1.2 KiB
Python
28 lines
1.2 KiB
Python
from functools import wraps
|
|
|
|
from app.db.crud import create_operationlog
|
|
from app.db.schemas import OperationCreate
|
|
from fastapi.encoders import jsonable_encoder
|
|
|
|
def log_operation(operation):
|
|
def decorator(func):
|
|
@wraps(func)
|
|
async def wrapper(*args,**kwargs):
|
|
db = kwargs.get('db')
|
|
request = kwargs.get('request')
|
|
result = await func(*args,**kwargs)
|
|
detial = f"Request: {request.method} {request.url}\
|
|
Request Headers: {request.headers}\
|
|
Request Body: {request.body()}\
|
|
Result: {jsonable_encoder(result,exclude_unset=True)}"
|
|
function = func.__name__
|
|
db_operation = OperationCreate(tenantid ="t",
|
|
domainurl="d",
|
|
userid=3,
|
|
operation=operation,
|
|
function=function,
|
|
detail=detial)
|
|
create_operationlog(db,db_operation)
|
|
return result
|
|
return wrapper
|
|
return decorator |