Skip to content

Commit

Permalink
fix: serializer
Browse files Browse the repository at this point in the history
  • Loading branch information
GareArc committed Dec 23, 2024
1 parent 177732b commit 31757cf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 11 deletions.
17 changes: 17 additions & 0 deletions api/libs/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=(",", ":"))
6 changes: 3 additions & 3 deletions api/services/account_deletion_log_service.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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
Expand Down
18 changes: 10 additions & 8 deletions api/tasks/delete_account_task.py
Original file line number Diff line number Diff line change
@@ -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__)


Expand Down Expand Up @@ -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)

Expand All @@ -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)

0 comments on commit 31757cf

Please sign in to comment.