romeotools/classes/message.py

30 lines
1.1 KiB
Python

from .database import Database
from .attachment import Attachment
class Message:
def __init__(self, sender, recipient, id, date, text, attachments=[]):
self.sender = sender
self.recipient = recipient
self.id = id
self.date = date
self.text = text
self.attachments = attachments
def to_database(self, db=None, exceptions=False):
try:
db = db or Database()
db.execute("INSERT INTO message VALUES (?, ?, ?, ?, ?);",
(self.id, self.sender, self.recipient, self.date, self.text))
for attachment in self.attachments:
attachment.to_database(db)
except:
if not exceptions:
return
raise
@classmethod
def from_dict(cls, message):
if not "attachments" in message.keys():
message["attachments"] = []
return cls(message["from"], message["to"], message["id"], message["date"], message["text"], [Attachment.from_dict(attachment, message["id"]) for attachment in message["attachments"] if attachment["type"] == "IMAGE"])