While the following code works, it looks like trash. Furthermore, there are even more templates that I need to add.
Before I continue I should like to know what I can do to fix or improve the following code. I am not interested in docstrings or anything like that, give it time and I will add them. I am looking for a better way to structure this mess.
import math
import smtplib
from typing import Tuple
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
class MessageUser():
port = 587
host = 'smtp.gmail.com'
to_addr = '*****'
from_addr = '*****'
password = '*****'
model_outputs = {
'eggs': 0.0,
'milk': 0.0
}
base_t_msg_nothing_to_buy = ('Dear James,\n\nYour fridge is looking quite'
' full!\nIt looks like you are all set for'
' the upcoming days.\n\nBest regards,\nAI')
base_h_msg_nothing_to_buy = '''
<html>
<body>
<p>
Dear James,<br><br>
Your fridge is looking quite full!.<br>
It looks like you are all set for the upcoming days.<br><br>
Best regards,<br>
AI
</p>
</body>
</html>
'''
base_t_msg_suggestions = ('Dear James,\n\nYour fridge is looking ok,'
' however, I should advise you to buy the\n'
'following products as fast as possible:\n\n{}'
'\nBest regards,\nAI')
base_h_msg_suggestions = '''
<html>
<body>
<p>
Dear James,<br><br>
Your fridge is looking ok, however, I should advise you to buy the<br>
following products as fast as possible:<br><br>{}<br>
Best regards,<br>
AI
</p>
</body>
</html>
'''
base_t_msg_order_details = ()
base_h_msg_order_details = ''''''
def __init__(self, eggs_output, milk_output) -> None:
self.model_outputs['eggs'] = eggs_output
self.model_outputs['milk'] = milk_output
def make_message(self) -> Tuple[str]:
products_to_send = {}
for attr, value in self.model_outputs.items():
if math.floor(value) <= 2:
products_to_send[attr] = value
if len(products_to_send) <= 0:
return (self.base_t_msg_nothing_to_buy,
self.base_h_msg_nothing_to_buy)
elif len(products_to_send) <= 2:
text_format = ''
html_format = ''
for attr, value in products_to_send.items():
text_format += (f'\t{attr.title()}: {math.floor(value)} to'
f' {math.ceil(value)} days remaining.\n')
html_format += (f'<strong>{attr.title()}</strong>:'
f' {math.floor(value)} to {math.ceil(value)}'
' days remaining.<br>')
return (self.base_t_msg_suggestions.format(text_format),
self.base_h_msg_suggestions.format(html_format))
else:
pass
def send_email(self) -> bool:
try:
the_msg = MIMEMultipart('alternative')
the_msg['Subject'] = 'Grocery List - AI'
the_msg['From'] = self.from_addr
the_msg['To'] = self.to_addr
(text_msg, html_msg) = self.make_message()
msg_part_1 = MIMEText(text_msg, 'plain')
msg_part_2 = MIMEText(html_msg, 'html')
the_msg.attach(msg_part_1)
the_msg.attach(msg_part_2)
email_conn = smtplib.SMTP(self.host, self.port)
email_conn.ehlo()
email_conn.starttls()
email_conn.login(self.from_addr, self.password)
email_conn.sendmail(
self.from_addr, self.to_addr, the_msg.as_string())
email_conn.close()
return True
except smtplib.SMTPException:
print('Could not send email.')
return False