From ca7ee1bdf426767b0a33cb3be5dcbb0125d3f8e1 Mon Sep 17 00:00:00 2001
From: David Trattnig <david.trattnig@o94.at>
Date: Sat, 31 Oct 2020 21:33:22 +0100
Subject: [PATCH] Send coordinator emails on fallback. #38

---
 src/plugins/mailer.py | 66 ++++++++++++++++++++++++++++++++++++-------
 1 file changed, 56 insertions(+), 10 deletions(-)

diff --git a/src/plugins/mailer.py b/src/plugins/mailer.py
index 75408cff..b08825b6 100644
--- a/src/plugins/mailer.py
+++ b/src/plugins/mailer.py
@@ -39,8 +39,10 @@ class AuraMailer():
     """
     Event handler to send emails to Aura administrators and programme coordinators.
     """
+    logger = None
     engine = None
     mail = None
+    previous_timeslot = None
 
     def __init__(self, engine):
         """
@@ -49,6 +51,7 @@ class AuraMailer():
         Args:
         engine (Engine):    The Engine
         """
+        self.logger = logging.getLogger("AuraEngine")
         self.engine = engine
         self.mail = MailService()
 
@@ -58,13 +61,36 @@ class AuraMailer():
     #
 
 
+    def on_fallback_active(self, timeslot, fallback_type):
+        """
+        Called when a fallback is activated for the given timeslot,
+        since no default playlist is available.
+        """
+        show = "EMPTY TIMESLOT"
+        show_id = ""
+        timeframe = ""
+        
+        if timeslot:
+            show = timeslot.show_name
+            show_id = "The ID of the show is: " + str(timeslot.show_id)
+            timeframe = SU.fmt_time(timeslot.start_unix) + " - " + SU.fmt_time(timeslot.end_unix)
+
+        subject = f"Fallback for show '{show}' activated"
+        message = "Dear programme coordinator, \n\n"
+        message += f"AURA would like to notify you, that a '{fallback_type}' fallback for show '{show}' ({timeframe}) just got activated. "
+        message += f"{show_id}\n\nStay tuned!"
+        self.logger.debug(message)
+        self.mail.notify_coordinator(subject, message)
+
+
+
     def on_sick(self, data):
         """
         Called when the engine is in some unhealthy state.
         """
         subject = "ERROR - Engine turned into some INVALID STATE!"
         message = "There's an issue with your AURA Engine '%s':\n\n%s" % (data.get("engine_id"), data.get("status"))
-        self.mail.send_admin(subject, message)
+        self.mail.notify_admin(subject, message)
             
 
 
@@ -74,7 +100,7 @@ class AuraMailer():
         """
         subject = "OK - Engine became healthy again"
         message = "Good news, things seem fine again with your AURA Engine '%s':\n\n%s" % (data.get("engine_id"), data.get("status"))
-        self.mail.send_admin(subject, message)
+        self.mail.notify_admin(subject, message)
 
 
 
@@ -83,7 +109,7 @@ class AuraMailer():
         Callend when some critical event occurs
         """
         if not data: data = ""
-        self.mail.send_admin(subject, message + "\n\n" + str(data))
+        self.mail.notify_admin(subject, message + "\n\n" + str(data))
 
 
 
@@ -96,7 +122,8 @@ class MailService():
     logger = None
     admin_mails = None
     admin_mails_enabled = None
-
+    coordinator_mails = None
+    coordinator_mails_enabled = None
 
     def __init__(self):
         """
@@ -106,16 +133,17 @@ class MailService():
         self.logger = logging.getLogger("AuraEngine")
         self.admin_mails = self.config.get("admin_mail")
         self.admin_mails_enabled = self.config.get("mail_admin_enabled")
-
+        self.coordinator_mails = self.config.get("coordinator_mail")
+        self.coordinator_mails_enabled = self.config.get("mail_coordinator_enabled")
 
     #
     #   METHODS
     #
 
 
-    def send_admin(self, subject, body):
+    def notify_admin(self, subject, body):
         """
-        Sends an email to the administrator as defined in the configuration.
+        Sends an email to the administrator(s) as defined in the configuration.
 
         Args:
             subject (String):   The email subject
@@ -132,6 +160,25 @@ class MailService():
 
 
 
+    def notify_coordinator(self, subject, body):
+        """
+        Sends an email to the programme coordinator(s) as defined in the configuration.
+
+        Args:
+            subject (String):   The email subject
+            body (String):      The email body text
+        """
+        if self.coordinator_mails_enabled == "false":
+            self.logger.warning(SU.yellow("No programme coordinator mail sent, because doing so is disabled in engine.ini!"))
+            return False
+
+        coordinator_mails = self.coordinator_mails.split()
+
+        for mail_to in coordinator_mails:
+            self.send(mail_to, subject, body)
+
+
+
     def send(self, mail_to, subject, body):
         """
         Sends an email to the given address.
@@ -140,14 +187,13 @@ class MailService():
             subject (String):   The email's subject
             body (String):      The email's body text
         """
-        # read config
         mail_server = self.config.get("mail_server")
         mail_port = self.config.get("mail_server_port")
         mail_user = self.config.get("mail_user")
         mail_pass = self.config.get("mail_pass")
         from_mail = self.config.get("from_mail")
 
-        # check settings
+        # Check settings
         if mail_server == "":
             raise MailingException("Mail Server not set")
         if mail_port == "":
@@ -159,7 +205,7 @@ class MailService():
         if from_mail == "":
             raise MailingException("From Mail not set")
 
-        # stuff the message together and ...
+        # Compile the message and ...
         msg = EmailMessage()
         msg.set_content(body)
         mailsubject_prefix = self.config.get("mailsubject_prefix")
-- 
GitLab