From 507056ed9d0e5d5982e829ad4b11a8124f6d42af Mon Sep 17 00:00:00 2001
From: Jose Semedo <jose.semedo@cern.ch>
Date: Mon, 27 Sep 2021 09:16:59 +0200
Subject: [PATCH] [#56] Add Category to mail header

---
 .../email_templates/base_email.html           | 50 +++++++++++++------
 .../processors/email/processor.py             |  7 ++-
 scripts/docker-send-email.py                  | 29 ++++++++---
 3 files changed, 63 insertions(+), 23 deletions(-)

diff --git a/notifications_consumer/email_templates/base_email.html b/notifications_consumer/email_templates/base_email.html
index 838905c..fec74f0 100644
--- a/notifications_consumer/email_templates/base_email.html
+++ b/notifications_consumer/email_templates/base_email.html
@@ -1,8 +1,21 @@
 <head>
   <style>
-    img {max-width: 100%; height: unset !important;}
+    img {
+      max-width: 100%;
+      height: unset !important;
+    }
+    #notification-header #title {
+      margin-bottom: 0;
+      padding: 0;
+    }
+    #notification-header #subtitle {
+      margin-top: 0;
+      margin-bottom: 0;
+      padding: 0;
+    }
   </style>
 </head>
+
 <body>
   <table align="center" border="0" cellpadding="0" cellspacing="0" width="100%" style="line-height: 1.7; color: #555;">
     <tr>
@@ -16,15 +29,22 @@
                 <tr>
                   <td>
                     <table border="0" cellpadding="0" cellspacing="0" style="margin: 0 auto">
-                      <tr>
+                      <tr id="notification-header">
                         <td style="padding-top: 10px; color: #395CB4; text-align: center; margin: 0 auto">
-                          <h1 style="font-size: 1.4em;">
-                            <img
-                              src="https://notifications.web.cern.ch/favicon/favicon-32x32.png"
-                              alt="{{ service_name }}"
-                              style="height: 20px; vertical-align: middle;">
-                            {{ service_name }}
-                          </h1>
+                          <h1 id="title" style="font-size: 1.4em;">
+                            <img src="https://notifications.web.cern.ch/favicon/favicon-32x32.png"
+                              alt="{{ service_name }}" style="height: 20px; vertical-align: middle;">
+                            {% if category_name %}
+                              {{category_name}} Notifications
+                              </h1>
+                              <h5 id="subtitle">
+                                POWERED BY {{ service_name }}
+                              </h5>
+                            {% else %}
+                              {{ service_name }}
+                              </h1>
+                            {% endif %}
+                            <br/>
                         </td>
                       </tr>
                       <tr>
@@ -41,12 +61,12 @@
           <tr>
             <td style="text-align: center; color: #aaa; font-size: 11px; padding-bottom: 10px;">
               <p style="padding: 10px;">
-                This notification has been sent by CERN Notifications. <br/>
+                This notification has been sent by CERN Notifications. <br />
                 <a href="{{ base_url }}/#subscriptions">Manage your subscriptions</a> |
-                <a href="{{ base_url }}/#manage">Manage your channels</a> 
-                 {% if unsubscribe_blob %} 
-                     | <a href="{{ base_url }}/unsubscribe/{{ unsubscribe_blob }}/{{ unsubscribe_mail }}">Unsubscribe</a>
-                 {% endif %}
+                <a href="{{ base_url }}/#manage">Manage your channels</a>
+                {% if unsubscribe_blob %}
+                | <a href="{{ base_url }}/unsubscribe/{{ unsubscribe_blob }}/{{ unsubscribe_mail }}">Unsubscribe</a>
+                {% endif %}
               </p>
             </td>
           </tr>
@@ -54,4 +74,4 @@
       </td>
     </tr>
   </table>
-</body>
+</body>
\ No newline at end of file
diff --git a/notifications_consumer/processors/email/processor.py b/notifications_consumer/processors/email/processor.py
index 83fb029..6023157 100644
--- a/notifications_consumer/processors/email/processor.py
+++ b/notifications_consumer/processors/email/processor.py
@@ -42,10 +42,15 @@ class EmailProcessor(Processor):
             logging.exception("Failed to process created at date")
 
         subject = f'[{kwargs["channel_name"]}] {kwargs["summary"]}'
+
+        category_name = kwargs.get("category_name")
+        service_name = category_name if category_name else Config.SERVICE_NAME
+
         context = {
             "message_body": kwargs["message_body"],
-            "sender": Config.SERVICE_NAME,
+            "sender": service_name,
             "channel_name": kwargs["channel_name"],
+            "category_name": category_name,
             "summary": kwargs["summary"],
             "channel_id": kwargs["channel_id"],
             "link": kwargs["link"],
diff --git a/scripts/docker-send-email.py b/scripts/docker-send-email.py
index 3b1daeb..4a8c054 100644
--- a/scripts/docker-send-email.py
+++ b/scripts/docker-send-email.py
@@ -1,20 +1,35 @@
 """Utility to send one message inside docker environment."""
 import stomp
+import argparse
+import json
 
+parser = argparse.ArgumentParser(description='Send a test email notification.')
+parser.add_argument('-l', action='store_true', default=False, help='use to connect to localhost instead of activemq')
+parser.add_argument('-c', action='store_true', default=False, help='use to send an email with Category')
+parser.add_argument('-e', default='user', help='use to set a target email username')
+args = parser.parse_args()
+category = args.c
+email = args.e
+localhost = args.l
 
 """ Using an ActiveMQ container """
-conn = stomp.Connection([("activemq", 61613)])
+connection_target = "localhost" if localhost else "activemq"
+conn = stomp.Connection([(connection_target, 61613)])
 conn.connect("admin", "admin", wait=True)
-message_body = r"""{
+
+message = {
 "channel_name": "My Channel",
 "channel_id": "123",
 "message_body": "<p>Dear subscribers,</p>\n\n<p>This week news follows.&nbsp;</p>\n\n<p>&nbsp;</p>\n\n<p><strong>Topic 1</strong></p>\n\n<ul>\n\t<li>bla&nbsp;</li>\n\t<li>bla</li>\n\t<li>bla</li>\n</ul>\n\n<p>&nbsp;</p>\n\n<p><strong>Topic 2</strong></p>\n\n<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</p>\n\n<p>&nbsp;</p>\n\n<p>See you next week,</p>\n\n<p>Myself.</p>\n",
 "summary": "This week news!",
-"email": "user@cern.ch",
-"created_at": "12/01/2021, 13:59:40",
-"notification_id": "bd19eea4-9dca-48d9-a577-5de9d2bf374a",
+"created_at": "07/21/2021, 16:10:09",
+"notification_id": "BD19EEA4-9DCA-48D9-A577-5DE9D2BF374A",
 "link": "http://toto.cern.ch",
 "priority": "important"
-} """
-conn.send(body=message_body, destination="/queue/np.email", headers={"persistent": "true"})
+}
+
+message["email"] = email + "@cern.ch"
+if category: message["category_name"] = "Super Duper Category" 
+
+conn.send(body=json.dumps(message, indent = 4), destination="/queue/np.email", headers={"persistent": "true"})
 conn.disconnect()
-- 
GitLab