From 15cc48cd40460de84cb3546a8a423f6797165183 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=20=E6=9F=8F?= Date: Sun, 17 Nov 2024 18:42:22 +0900 Subject: [PATCH] test --- backend/app/core/operation.py | 28 ++++++++++++++++++++++++++++ backend/app/db/crud.py | 16 +++++++++++++++- backend/app/db/schemas.py | 10 +++++++++- 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 backend/app/core/operation.py diff --git a/backend/app/core/operation.py b/backend/app/core/operation.py new file mode 100644 index 0000000..11449f8 --- /dev/null +++ b/backend/app/core/operation.py @@ -0,0 +1,28 @@ +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 \ No newline at end of file diff --git a/backend/app/db/crud.py b/backend/app/db/crud.py index 8970081..5f5141a 100644 --- a/backend/app/db/crud.py +++ b/backend/app/db/crud.py @@ -364,4 +364,18 @@ def create_log(db: Session, error:schemas.ErrorCreate): def get_kintoneformat(db: Session): formats = db.query(models.KintoneFormat).order_by(models.KintoneFormat.id).all() - return formats \ No newline at end of file + return formats + +def create_operationlog(db: Session, log: schemas.OperationCreate): + db_log = models.OperationLog( + tenantid=log.tenantid, + domainurl=log.domainurl, + userid = log.userid, + operation = log.operation, + function = log.function, + detail = log.detail + ) + db.add(db_log) + db.commit() + db.refresh(db_log) + return db_log \ No newline at end of file diff --git a/backend/app/db/schemas.py b/backend/app/db/schemas.py index 992e406..47c3310 100644 --- a/backend/app/db/schemas.py +++ b/backend/app/db/schemas.py @@ -162,4 +162,12 @@ class Event(Base): class ErrorCreate(BaseModel): title:str location:str - content:str \ No newline at end of file + content:str + +class OperationCreate(BaseModel): + tenantid:str + domainurl:str + userid:int + operation:str + function:str + detail:str