From 31757cfde297c3b9533eedb43b013dfe18cbdadc Mon Sep 17 00:00:00 2001 From: GareArc Date: Mon, 23 Dec 2024 01:39:55 -0500 Subject: [PATCH] fix: serializer --- api/libs/helper.py | 17 +++++++++++++++++ api/services/account_deletion_log_service.py | 6 +++--- api/tasks/delete_account_task.py | 18 ++++++++++-------- 3 files changed, 30 insertions(+), 11 deletions(-) diff --git a/api/libs/helper.py b/api/libs/helper.py index 349a75d73e6737..38244b4cc18247 100644 --- a/api/libs/helper.py +++ b/api/libs/helper.py @@ -301,3 +301,20 @@ def increment_rate_limit(self, email: str): def get_current_datetime(): return datetime.now(tz.utc).replace(tzinfo=None) + + +def serialize_sqlalchemy(obj): + """ + Serializes an SQLAlchemy object into a JSON string. + """ + data = {} + for column in obj.__table__.columns: + value = getattr(obj, column.name) + if isinstance(value, datetime): + data[column.name] = value.isoformat() # ISO 8601 format for datetime + elif isinstance(value, uuid.UUID): + data[column.name] = str(value) # String representation for UUID + else: + data[column.name] = value + + return json.dumps(data, separators=(",", ":")) diff --git a/api/services/account_deletion_log_service.py b/api/services/account_deletion_log_service.py index 19c385ed41ee99..4a44e3d90fd7f6 100644 --- a/api/services/account_deletion_log_service.py +++ b/api/services/account_deletion_log_service.py @@ -1,10 +1,10 @@ +import json from datetime import timedelta from configs import dify_config from extensions.ext_database import db -from flask import jsonify -from libs.helper import get_current_datetime +from libs.helper import get_current_datetime, serialize_sqlalchemy from models.account import Account, AccountDeletionLog @@ -15,7 +15,7 @@ def create_account_deletion_log(account: Account, reason): account_deletion_log.email = account.email account_deletion_log.reason = reason account_deletion_log.account_id = account.id - account_deletion_log.snapshot = jsonify(account) + account_deletion_log.snapshot = json.dumps(serialize_sqlalchemy(account), separators=(",", ":")) account_deletion_log.updated_at = get_current_datetime() return account_deletion_log diff --git a/api/tasks/delete_account_task.py b/api/tasks/delete_account_task.py index ab997dba5d6599..087e48ad5d82b4 100644 --- a/api/tasks/delete_account_task.py +++ b/api/tasks/delete_account_task.py @@ -1,16 +1,18 @@ +import json import logging import time import click from celery import shared_task from extensions.ext_database import db -from flask import jsonify from models.account import (Account, AccountDeletionLogDetail, TenantAccountJoin, TenantAccountJoinRole) from services.account_deletion_log_service import AccountDeletionLogService from services.billing_service import BillingService from tasks.mail_account_deletion_task import send_deletion_success_task +from api.libs.helper import serialize_sqlalchemy + logger = logging.getLogger(__name__) @@ -84,10 +86,10 @@ def _handle_owner_tenant_deletion(ta: TenantAccountJoin): account_deletion_log_detail = AccountDeletionLogDetail() account_deletion_log_detail.account_id = ta.account_id account_deletion_log_detail.tenant_id = tenant_id - account_deletion_log_detail.snapshot = jsonify({ - "tenant_account_join_info": ta, - "dismissed_members": members_to_dismiss - }) + account_deletion_log_detail.snapshot = json.dumps({ + "tenant_account_join_info": serialize_sqlalchemy(ta), + "dismissed_members": [serialize_sqlalchemy(member) for member in members_to_dismiss] + }, separators=(",", ":")) account_deletion_log_detail.role = ta.role db.session.add(account_deletion_log_detail) @@ -102,8 +104,8 @@ def _remove_account_from_tenant(ta, email): account_deletion_log_detail = AccountDeletionLogDetail() account_deletion_log_detail.account_id = ta.account_id account_deletion_log_detail.tenant_id = tenant_id - account_deletion_log_detail.snapshot = jsonify({ - "tenant_account_join_info": ta - }) + account_deletion_log_detail.snapshot = json.dumps({ + "tenant_account_join_info": serialize_sqlalchemy(ta), + }, separators=(",", ":")) account_deletion_log_detail.role = ta.role db.session.add(account_deletion_log_detail)