From 14244e2f48104a44a0cb2b28598b92ff0244d2b5 Mon Sep 17 00:00:00 2001 From: Daniel Schick Date: Mon, 16 Dec 2024 16:25:52 +0100 Subject: [PATCH] EMail notifications work in progress --- src/server/BreCal/msg/msg_types.json | 2 +- .../BreCal/msg/notification_element.html | 2 +- .../BreCal/msg/notification_template.html | 2 +- src/server/BreCal/schemas/defs.py | 14 ++++- .../BreCal/services/schedule_routines.py | 57 ++++++++++++------- 5 files changed, 52 insertions(+), 25 deletions(-) diff --git a/src/server/BreCal/msg/msg_types.json b/src/server/BreCal/msg/msg_types.json index a4bdd04..1cdc90e 100644 --- a/src/server/BreCal/msg/msg_types.json +++ b/src/server/BreCal/msg/msg_types.json @@ -29,7 +29,7 @@ }, { "type" : 5, - "color" : "#f8f8f8", + "color" : "#a8a8a8", "name" : "unassigned", "link" : "https://www.bremen-calling.de/", "msg_text" : "Nominierung abgewählt" diff --git a/src/server/BreCal/msg/notification_element.html b/src/server/BreCal/msg/notification_element.html index 6a69d76..3a095e8 100644 --- a/src/server/BreCal/msg/notification_element.html +++ b/src/server/BreCal/msg/notification_element.html @@ -6,7 +6,7 @@ - [[text]] + [[text]] diff --git a/src/server/BreCal/msg/notification_template.html b/src/server/BreCal/msg/notification_template.html index dbc5e11..c66d158 100644 --- a/src/server/BreCal/msg/notification_template.html +++ b/src/server/BreCal/msg/notification_template.html @@ -1,5 +1,5 @@ - + diff --git a/src/server/BreCal/schemas/defs.py b/src/server/BreCal/schemas/defs.py index 8e8ccea..89f83af 100644 --- a/src/server/BreCal/schemas/defs.py +++ b/src/server/BreCal/schemas/defs.py @@ -5,4 +5,16 @@ NOTIFICATION_COOLDOWN = 600 # 10 minutes until a notification gets real NOTIFICATION_MAX_AGE_DAYS = 3 # 3 days until a notification gets deleted # Placeholder for the email credentials filled by startup logic -email_credentials = dict() \ No newline at end of file +email_credentials = dict() + +# Holding var for global message notification type info +message_types = dict() + +# Constants for the email display + +shipcall_types = { + 1: "Arrival", + 2: "Departure", + 3: "Shifting" +} + diff --git a/src/server/BreCal/services/schedule_routines.py b/src/server/BreCal/services/schedule_routines.py index fe6d9db..d751c69 100644 --- a/src/server/BreCal/services/schedule_routines.py +++ b/src/server/BreCal/services/schedule_routines.py @@ -1,6 +1,8 @@ import logging import pydapper import smtplib +import json +import os from email.message import EmailMessage from BreCal.schemas import model, defs @@ -82,14 +84,41 @@ def SendEmails(email_dict): conn.ehlo() conn.login(defs.email_credentials["sender"], defs.email_credentials["password_send"]) - for user, message in email_dict.items(): + current_path = os.path.dirname(os.path.abspath(__file__)) + + if not defs.message_types: + f = open(os.path.join(current_path,"../msg/msg_types.json"), encoding='utf-8'); + defs.message_types = json.load(f) + f.close() + + for user, notifications in email_dict.items(): msg = EmailMessage() msg["Subject"] = '[Bremen calling] Notification' msg["From"] = defs.email_credentials["sender"] msg["To"] = user.user_email - # TODO: pretty-print and format message according to template - msg.set_content(message) + with open(os.path.join(current_path,'../msg/notification_template.html'), mode="r", encoding="utf-8") as file: + body = file.read() + + replacement = "" + + for notification in notifications: + + message_type = defs.message_types[notification.type] + + with open(os.path.join(current_path,'../msg/notification_element.html'), mode="r", encoding="utf-8") as file: + element = file.read() + element = element.replace("[[color]]", message_type["color"]) + element = element.replace("[[link]]", message_type["link"]) + + # We want to show the following information for each notification: + # Ship-name, Arr/Dep/Shift, ETA/ETD, berth + + element = element.replace("[[text]]", message_type["msg_text"]) + replacement += element + + body = body.replace("[[NOTIFICATION_ELEMENTS]]", replacement) + msg.set_content(body, subtype='html', charset='utf-8', cte='8bit') conn.sendmail(defs.email_credentials["sender"], user.user_email, msg.as_string()) @@ -136,23 +165,8 @@ def SendNotifications(): # send notification to user if user.notify_email: if user not in email_dict: - email_dict[user] = "" - match notification.type: - case 1: # assignment - email_dict[user] += "\n" - email_dict[user] += "You have been assigned to a new shipcall: " + str(notification.message or "") - case 2: # next 24 hours - email_dict[user] += "\n" - email_dict[user] += "A shipcall is scheduled for the next 24 hours: " + str(notification.message or "") - case 3: # Time conflict - email_dict[user] += "\n" - email_dict[user] += "There is a time conflict in a shipcall: " + str(notification.message or "") - case 4: # Time conflict resolved - email_dict[user] += "\n" - email_dict[user] += "A time conflict in a shipcall has been resolved: " + str(notification.message or "") - case 5: # unassigned - email_dict[user] += "\n" - email_dict[user] += "You have been unassigned from a shipcall: " + str(notification.message or "") + email_dict[user] = [] + email_dict[user].append(notification) if user.notify_whatsapp: # TBD pass @@ -164,7 +178,7 @@ def SendNotifications(): # send emails (if any) if len(email_dict) > 0: - SendEmails(email_dict) + SendEmails(email_dict) except Exception as ex: logging.error(ex) @@ -232,6 +246,7 @@ def setup_schedule(update_shipcalls_interval_in_minutes:int=60): schedule.every().day.at("09:00").do(eval_next_24_hrs) + SendNotifications() add_function_to_schedule_send_notifications(1) # TODO: Add schedule function to evaluate all notifications in level 1 and create actions