HR-ATS-Portal/backend/inbox/serializers.py

44 lines
1.6 KiB
Python

from pathlib import Path
from inbox.models import Inbox_Messages
def serialize_message(message: Inbox_Messages) -> dict:
"""inbox_messages row -> the shape the #inbox Email tab renders."""
sender_name = message.message_from
full = message.full_email_response
if isinstance(full, dict):
from_block = full.get("from")
if isinstance(from_block, dict):
email_address = from_block.get("emailAddress")
if isinstance(email_address, dict):
name = email_address.get("name")
if name:
sender_name = name
attachment_name = None
if message.file_name:
attachment_name = message.file_name.split(",")[0].strip() or None
elif message.file_path:
attachment_name = Path(message.file_path.split(",")[0].strip()).name or None
return {
"id": str(message.id),
"message_id": str(message.message_id) if message.message_id else None,
"sender_name": sender_name,
"fromEmail": message.message_from,
"subject": message.message_subject,
"body": message.message_body,
"when": message.message_received_time,
"unread": not message.message_read,
"attachment": message.attachment,
"attachment_name": attachment_name,
"file_name": message.file_name,
"message_to": message.message_to,
"message_cc": message.message_cc,
"message_bcc": message.message_bcc,
"message_sent_time": message.message_sent_time,
"message_reply": message.message_reply,
"file_path": message.file_path,
}