2024-06-26 17:33:29 +08:00
|
|
|
from contextlib import contextmanager
|
|
|
|
from datetime import datetime
|
|
|
|
|
|
|
|
from extensions.ext_database import db
|
|
|
|
from models.model import Message
|
|
|
|
|
|
|
|
|
|
|
|
def filter_none_values(data: dict):
|
2024-09-21 22:56:37 +08:00
|
|
|
new_data = {}
|
2024-06-26 17:33:29 +08:00
|
|
|
for key, value in data.items():
|
|
|
|
if value is None:
|
|
|
|
continue
|
|
|
|
if isinstance(value, datetime):
|
2024-09-21 22:56:37 +08:00
|
|
|
new_data[key] = value.isoformat()
|
|
|
|
else:
|
|
|
|
new_data[key] = value
|
|
|
|
return new_data
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def get_message_data(message_id):
|
|
|
|
return db.session.query(Message).filter(Message.id == message_id).first()
|
|
|
|
|
|
|
|
|
|
|
|
@contextmanager
|
|
|
|
def measure_time():
|
2024-09-10 17:00:20 +08:00
|
|
|
timing_info = {"start": datetime.now(), "end": None}
|
2024-06-26 17:33:29 +08:00
|
|
|
try:
|
|
|
|
yield timing_info
|
|
|
|
finally:
|
2024-09-10 17:00:20 +08:00
|
|
|
timing_info["end"] = datetime.now()
|
2024-06-26 17:33:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
def replace_text_with_content(data):
|
|
|
|
if isinstance(data, dict):
|
|
|
|
new_data = {}
|
|
|
|
for key, value in data.items():
|
2024-09-10 17:00:20 +08:00
|
|
|
if key == "text":
|
|
|
|
new_data["content"] = value
|
2024-06-26 17:33:29 +08:00
|
|
|
else:
|
|
|
|
new_data[key] = replace_text_with_content(value)
|
|
|
|
return new_data
|
|
|
|
elif isinstance(data, list):
|
|
|
|
return [replace_text_with_content(item) for item in data]
|
|
|
|
else:
|
|
|
|
return data
|