romeotools/classes/message.py

29 lines
1 KiB
Python
Raw Normal View History

from .database import Database
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, exceptions=False):
try:
Database().execute("INSERT INTO message VALUES (?, ?, ?, ?, ?);",
(self.id, self.sender, self.recipient, self.date, self.text))
for attachment in self.attachments:
attachment.to_database()
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"])