From c3a18ce221952f174c5b2670d13cb089cdc5a4c4 Mon Sep 17 00:00:00 2001
From: Caetan Tojeiro Carpente <caetan.tojeiro.carpente@cern.ch>
Date: Mon, 3 Oct 2022 14:41:53 +0200
Subject: [PATCH] [#74 #82] Bugfix: Feeds total notifications and feed text
 template

---
 .../processors/email_feed/processor.py        | 12 ++-
 .../templates/feed_notification.html          | 98 +++++++++----------
 .../templates/feed_notification.txt           | 16 +--
 3 files changed, 68 insertions(+), 58 deletions(-)

diff --git a/notifications_consumer/processors/email_feed/processor.py b/notifications_consumer/processors/email_feed/processor.py
index 69dde9a..cdd7f65 100644
--- a/notifications_consumer/processors/email_feed/processor.py
+++ b/notifications_consumer/processors/email_feed/processor.py
@@ -62,15 +62,21 @@ class EmailFeedProcessor(Processor):
         )
 
         def prepare_notifications(notifications):
-            for notification in notifications:
+            limit = 3
+
+            for notification in notifications[0:limit]:
                 notification.update({("body", BeautifulSoup(notification["body"], "lxml").get_text(" "))})
-            return notifications
+
+            return {
+                "notifications": notifications[0:limit],
+                "remainder": len(notifications) - limit if len(notifications) > 3 else 0,
+            }
 
         context = {
             "title": title,
             "notification_priority": NotificationPriority,
             # Jinja2 Renderer Fix https://stackoverflow.com/questions/6906593/itertools-groupby-in-a-django-template
-            "channel_notifications": [(k, prepare_notifications(list(g)[0:3])) for k, g in grouped_notifications],
+            "channel_notifications": [(k, prepare_notifications(list(g))) for k, g in grouped_notifications],
             "sender": Config.SERVICE_NAME,
         }
 
diff --git a/notifications_consumer/processors/email_feed/templates/feed_notification.html b/notifications_consumer/processors/email_feed/templates/feed_notification.html
index cc593bc..b50fc7c 100644
--- a/notifications_consumer/processors/email_feed/templates/feed_notification.html
+++ b/notifications_consumer/processors/email_feed/templates/feed_notification.html
@@ -1,57 +1,57 @@
 {% extends 'templates/base_email.html' %}
 
 {% block content %}
-<table border="0" cellpadding="0" cellspacing="0" style="border-radius: 5px; min-width: 550px; padding: 10px">
-    <tr>
-        <td>
-            <h3 style="text-align : center; font-weight: normal;">{{ title }}</h3>
-        </td>
-    </tr>
+    <table border="0" cellpadding="0" cellspacing="0" style="border-radius: 5px; min-width: 550px; padding: 10px">
+        <tr>
+            <td>
+                <h3 style="text-align : center; font-weight: normal;">{{ title }}</h3>
+            </td>
+        </tr>
 
-    {% for key, notifications in channel_notifications %}
-    <tr>
-        <td>
-            <table width="100%">
-                <tr style="background: #3071AB;text-align: center;color:white;">
-                    <td style="padding:10px">
-                        <a style="text-decoration:none" href="{{ base_url }}/channels/{{ key[0] }}/notifications">
-                            <h3 style="color: white; margin: 0;">
-                                {{ key[1] }}
-                            </h3>
-                        </a>
-                    </td>
-                </tr>
-                {% for notification in notifications %}
-                <tr>
-                    <td style="padding:10px">
-                        <p><b>{% if notification.priority and notification.priority.upper() ==
+        {% for key, notifications in channel_notifications %}
+            <tr>
+                <td>
+                    <table width="100%">
+                        <tr style="background: #3071AB;text-align: center;color:white;">
+                            <td style="padding:10px">
+                                <a style="text-decoration:none"
+                                   href="{{ base_url }}/channels/{{ key[0] }}/notifications">
+                                    <h3 style="color: white; margin: 0;">
+                                        {{ key[1] }}
+                                    </h3>
+                                </a>
+                            </td>
+                        </tr>
+                        {% for notification in notifications["notifications"] %}
+                            <tr>
+                                <td style="padding:10px">
+                                    <p><b>{% if notification.priority and notification.priority.upper() ==
                                 notification_priority.IMPORTANT %} <span
-                                    style="color: #FF5144; font-size: 1.5em; vertical-align: middle;">
+                                            style="color: #FF5144; font-size: 1.5em; vertical-align: middle;">
                                     &#9888;</span> {% endif %}
-                                <span style="color: #3071AB; font-size: 1.2em; vertical-align: middle;">{{ notification.summary }}</span></b>
-                            <br>
-                            {{ notification.body|trim|truncate(250, True) }}
-                            <a style="text-decoration:none"
-                                href="{{ base_url }}/channels/{{ key[0] }}/notifications/{{ notification.id }}">Read
-                                More</a>
-                            </b>
-                        </p>
-                    </td>
-                </tr>
-                {% endfor %}
+                                        <span style="color: #3071AB; font-size: 1.2em; vertical-align: middle;">{{ notification.summary }}</span></b>
+                                        <br>
+                                        {{ notification.body|trim|truncate(250, True) }}
+                                        <a style="text-decoration:none"
+                                           href="{{ base_url }}/channels/{{ key[0] }}/notifications/{{ notification.id }}">Read
+                                            More</a>
+                                        </b>
+                                    </p>
+                                </td>
+                            </tr>
+                        {% endfor %}
 
-                {% if (notifications|length - 3) > 0 %}
-                <tr>
-                    <td style="padding:10px; text-align: center;">
-                        <a href="{{ base_url }}/channels/{{ key[0] }}/notifications">See
-                            {{ notifications|length - 3 }} more
-                            notifications</a>
-                    </td>
-                </tr>
-                {% endif %}
-            </table>
-        </td>
-    </tr>
-    {% endfor %}
-</table>
+                        {% if notifications["remainder"] > 0 %}
+                            <tr>
+                                <td style="padding:10px; text-align: center;">
+                                    <a href="{{ base_url }}/channels/{{ key[0] }}/notifications">See
+                                        {{ notifications["remainder"] }} more notifications</a>
+                                </td>
+                            </tr>
+                        {% endif %}
+                    </table>
+                </td>
+            </tr>
+        {% endfor %}
+    </table>
 {% endblock %}
diff --git a/notifications_consumer/processors/email_feed/templates/feed_notification.txt b/notifications_consumer/processors/email_feed/templates/feed_notification.txt
index 0728318..8f1019b 100644
--- a/notifications_consumer/processors/email_feed/templates/feed_notification.txt
+++ b/notifications_consumer/processors/email_feed/templates/feed_notification.txt
@@ -1,14 +1,18 @@
-{% macro generate_notification_text(x) %}
-    {{ x.body|striptags|truncate(250, True) }}...
-    Read More Here: {{redirect_link}}/notification/{{x.id}}
+{% macro generate_notification_text(notification, base_url, channel_key) %}
+    {{ notification.body|striptags|truncate(250, True) }}...
+    Read More Here: {{ base_url }}/channels/{{ channel_key }}/notification/{{notification.id}}
 {% endmacro %}
 
-{% for key, group in channel_notifications %}
+{% for key, notifications in channel_notifications %}
     {{ key[1] }}
-    {% for notification in group %}
-        {{ generate_notification_text(notification) }}
+    {% for notification in notifications["notifications"] %}
+        {{ generate_notification_text(notification, base_url, key[0]) }}
     {% endfor %}
 
+    {% if notifications["remainder"] > 0 %}
+        See {{ notifications["remainder"] }} more notifications here: {{ base_url }}/channels/{{ key[0] }}/notifications
+    {% endif %}
+
 {% endfor %}
 -
 This notification has been sent by CERN Notifications.
-- 
GitLab