121 lines
5.0 KiB
Python
121 lines
5.0 KiB
Python
from datetime import datetime
|
|
|
|
import RawMail
|
|
import User
|
|
from DBConn import DBConn, get_unix_timestamp
|
|
|
|
|
|
class Mail:
|
|
"""Class for holding the data in the mail table of the database"""
|
|
def __init__(self):
|
|
self.__mail_id = None
|
|
self.__raw_id = None
|
|
self.__from_id = None
|
|
self.__to_id = None
|
|
self.__mail_subject = None
|
|
self.__mail_body = None
|
|
self.__mail_read = None
|
|
self.__mail_date_sent = None
|
|
|
|
# Holding variables for the from, to and raw mail class objects
|
|
self.__from_user: User.User or None = None
|
|
self.__to_user: User.User or None = None
|
|
self.__raw_mail: RawMail.RawMail or None = None
|
|
|
|
self.__is_valid = False
|
|
|
|
def load_mail(self, mail_id: int):
|
|
"""Method to load the mail with the id given"""
|
|
self.__mail_id = mail_id
|
|
self.__load_mail()
|
|
|
|
def __load_mail(self) -> None:
|
|
"""Private method to load the mail from the database using the mail_id"""
|
|
db_connection = DBConn()
|
|
mail_details = db_connection.do_select("mail_store", "row", "*", "mail_id = :mail_id",
|
|
[
|
|
{
|
|
"field": "mail_id",
|
|
"value": self.__mail_id
|
|
}
|
|
])
|
|
|
|
if mail_details is not None and len(mail_details) > 0:
|
|
self.__mail_id = mail_details['mail_id']
|
|
self.__raw_id = mail_details['raw_id']
|
|
self.__from_id = mail_details['from_id']
|
|
self.__to_id = mail_details['to_id']
|
|
self.__mail_subject = mail_details['mail_subject']
|
|
self.__mail_body = mail_details['mail_body']
|
|
self.__mail_read = mail_details['mail_read']
|
|
self.__mail_date_sent = mail_details['mail_date_sent']
|
|
self.__is_valid = True
|
|
|
|
def mail_is_valid(self):
|
|
"""Method to check if we have loaded a valid mail from the DB"""
|
|
return self.__is_valid
|
|
|
|
def get_mail_id(self):
|
|
"""Method to return the mail id"""
|
|
return self.__mail_id
|
|
|
|
def get_subject(self, trim_len: int = 0):
|
|
"""Method to return the subject line, if given a number it will truncate the string to that len"""
|
|
if trim_len > 0:
|
|
return self.__mail_subject[0:trim_len]
|
|
else:
|
|
return self.__mail_subject
|
|
|
|
def get_date_sent(self, format_string: str = "%d/%m/%y %H:%M"):
|
|
"""Method to get the sent date with the given date format"""
|
|
date_time_string = datetime.utcfromtimestamp(self.__mail_date_sent).strftime(format_string)
|
|
return date_time_string
|
|
|
|
def get_from_user(self) -> User.User or None:
|
|
"""Method to load a user object into the from user variable"""
|
|
if self.__from_id:
|
|
# Preload the user into the object if not loaded beforehand
|
|
if not self.__from_user:
|
|
self.__from_user = User.User(self.__from_id)
|
|
return self.__from_user
|
|
else:
|
|
return None
|
|
|
|
def get_to_user(self) -> User.User or None:
|
|
"""Method to load a user object into the to user variable"""
|
|
if self.__to_id:
|
|
# Preload the user into the object if not loaded beforehand
|
|
if not self.__to_user:
|
|
self.__to_user = User.User(self.__to_id)
|
|
return self.__to_user
|
|
else:
|
|
return None
|
|
|
|
def get_raw_mail(self):
|
|
"""Method to load a raw mail object into the variable"""
|
|
if not self.__raw_mail:
|
|
self.__raw_mail = RawMail.RawMail()
|
|
self.__raw_mail.load_raw_mail(self.__raw_id)
|
|
return self.__raw_mail
|
|
|
|
def create_mail(self, raw_id: int, from_id: int, to_id: int, subject: str, body: str) -> bool:
|
|
"""Method to add the mail to the database"""
|
|
result = False
|
|
if not self.__is_valid:
|
|
if raw_id and from_id and to_id and len(subject) > 0 or len(body) > 0:
|
|
db_connection = DBConn()
|
|
self.__mail_id = db_connection.do_insert("mail_store",
|
|
[{"field": "raw_id", "value": raw_id},
|
|
{"field": "from_id", "value": from_id},
|
|
{"field": "to_id", "value": to_id},
|
|
{"field": "mail_subject", "value": subject},
|
|
{"field": "mail_body", "value": body},
|
|
{"field": "mail_read", "value": 0},
|
|
{"field": "mail_date_sent", "value": get_unix_timestamp()}],
|
|
True)
|
|
if self.__mail_id:
|
|
self.__load_mail()
|
|
result = True
|
|
|
|
return result
|