From d57ea3309a28f3009d9e5972cbb1472aadc5353b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?P=C4=93teris=20Caune?= <cuu508@gmail.com>
Date: Wed, 23 Aug 2023 11:19:58 +0300
Subject: [PATCH] Split report and reminder (nag) templates

---
 hc/accounts/models.py                         |  5 ++-
 hc/api/tests/test_sendreports.py              |  1 -
 hc/lib/emails.py                              |  5 +++
 templates/emails/nag-body-html.html           | 34 ++++++++++++++++++
 templates/emails/nag-body-text.html           | 10 ++++++
 templates/emails/nag-subject.html             |  1 +
 ...ummary-html.html => nag-summary-html.html} |  0
 templates/emails/report-body-html.html        | 36 ++++---------------
 templates/emails/report-body-text.html        |  4 +--
 templates/emails/report-subject.html          |  7 +---
 ...mes-html.html => report-summary-html.html} |  0
 11 files changed, 60 insertions(+), 43 deletions(-)
 create mode 100644 templates/emails/nag-body-html.html
 create mode 100644 templates/emails/nag-body-text.html
 create mode 100644 templates/emails/nag-subject.html
 rename templates/emails/{summary-html.html => nag-summary-html.html} (100%)
 rename templates/emails/{summary-downtimes-html.html => report-summary-html.html} (100%)

diff --git a/hc/accounts/models.py b/hc/accounts/models.py
index 7ca701df..ecaaa1fc 100644
--- a/hc/accounts/models.py
+++ b/hc/accounts/models.py
@@ -239,10 +239,10 @@ class Profile(models.Model):
             for check in checks:
                 check.past_downtimes = check.downtimes_by_boundary(boundaries)[:-1]
 
-            ctx["nag"] = False
             ctx["checks"] = checks
             ctx["boundaries"] = boundaries[:-1]
             ctx["monthly_or_weekly"] = self.reports
+            emails.report(self.user.email, ctx, headers)
 
         if nag:
             # For nags, only show checks that are currently down
@@ -251,10 +251,9 @@ class Profile(models.Model):
                 return False
             ctx["checks"] = checks
             ctx["num_down"] = len(checks)
-            ctx["nag"] = True
             ctx["nag_period"] = self.nag_period.total_seconds()
+            emails.nag(self.user.email, ctx, headers)
 
-        emails.report(self.user.email, ctx, headers)
         return True
 
     def sms_sent_this_month(self):
diff --git a/hc/api/tests/test_sendreports.py b/hc/api/tests/test_sendreports.py
index d1cc78f9..b8a54db1 100644
--- a/hc/api/tests/test_sendreports.py
+++ b/hc/api/tests/test_sendreports.py
@@ -21,7 +21,6 @@ MOCK_NOW = Mock(return_value=CURRENT_TIME)
 NAG_TEXT = """Hello,
 
 This is a hourly reminder sent by Mychecks.
-
 One check is currently DOWN:
 
 
diff --git a/hc/lib/emails.py b/hc/lib/emails.py
index 07cd85f1..c27ba77b 100644
--- a/hc/lib/emails.py
+++ b/hc/lib/emails.py
@@ -103,6 +103,11 @@ def report(to, ctx, headers={}):
     send(m, block=True)
 
 
+def nag(to, ctx, headers={}):
+    m = make_message("nag", to, ctx, headers=headers)
+    send(m, block=True)
+
+
 def deletion_notice(to, ctx, headers={}):
     m = make_message("deletion-notice", to, ctx, headers=headers)
     send(m, block=True)
diff --git a/templates/emails/nag-body-html.html b/templates/emails/nag-body-html.html
new file mode 100644
index 00000000..a5f077ae
--- /dev/null
+++ b/templates/emails/nag-body-html.html
@@ -0,0 +1,34 @@
+{% extends "emails/base.html" %}
+{% load humanize hc_extras %}
+
+{% block content %}
+Hello,<br />
+
+This is a
+{% if nag_period == 3600 %}hourly{% endif %}
+{% if nag_period == 86400 %}daily{% endif %}
+reminder sent by <a href="{% site_root %}">{% site_name %}</a>.<br />
+
+{% if num_down == 1%}
+One check is currently <strong>DOWN</strong>:<br />
+{% else %}
+{{ num_down }} checks are currently <strong>DOWN</strong>:<br />
+{% endif %}
+
+{% include "emails/nag-summary-html.html" %}
+
+<strong>Too many notifications?</strong>
+Visit the <a href="{{ notifications_url }}">Email Reports</a>
+page on {% site_name %} to set your notification preferences.
+<br /><br />
+
+Cheers,<br>
+The {% site_name %} Team
+{% endblock %}
+
+{% block unsub %}
+<br>
+<a href="{{ unsub_link }}" target="_blank" style="color: #666666; text-decoration: underline;">
+    Unsubscribe
+</a>
+{% endblock %}
diff --git a/templates/emails/nag-body-text.html b/templates/emails/nag-body-text.html
new file mode 100644
index 00000000..bfb10eb6
--- /dev/null
+++ b/templates/emails/nag-body-text.html
@@ -0,0 +1,10 @@
+{% load hc_extras %}Hello,
+
+This is a {% if nag_period == 3600 %}hourly {% endif %}{% if nag_period == 86400 %}daily {% endif %}reminder sent by {% site_name %}.
+{% if num_down == 1%}One check is currently DOWN:{% else %}{{ num_down }} checks are currently DOWN:{% endif %}
+{% include 'emails/summary-text.html' %}
+
+
+--
+Cheers,
+{% site_name %}
diff --git a/templates/emails/nag-subject.html b/templates/emails/nag-subject.html
new file mode 100644
index 00000000..59ef830d
--- /dev/null
+++ b/templates/emails/nag-subject.html
@@ -0,0 +1 @@
+Reminder: {{ num_down }} check{{ num_down|pluralize }} still down
\ No newline at end of file
diff --git a/templates/emails/summary-html.html b/templates/emails/nag-summary-html.html
similarity index 100%
rename from templates/emails/summary-html.html
rename to templates/emails/nag-summary-html.html
diff --git a/templates/emails/report-body-html.html b/templates/emails/report-body-html.html
index 023d97ff..36335c2c 100644
--- a/templates/emails/report-body-html.html
+++ b/templates/emails/report-body-html.html
@@ -4,38 +4,14 @@
 {% block content %}
 Hello,<br />
 
-{% if nag %}
-    This is a
-    {% if nag_period == 3600 %}hourly{% endif %}
-    {% if nag_period == 86400 %}daily{% endif %}
-    reminder sent by <a href="{% site_root %}">{% site_name %}</a>.<br />
+This is a {{ monthly_or_weekly }} report sent by
+<a href="{% site_root %}">{% site_name %}</a>.<br />
 
-    {% if num_down == 1%}
-        One check is currently <strong>DOWN</strong>:
-    {% else %}
-        {{ num_down }} checks are currently <strong>DOWN</strong>:
-    {% endif %}
-
-    <br />
-    {% include "emails/summary-html.html" %}
-
-    <strong>Too many notifications?</strong>
-    Visit the <a href="{{ notifications_url }}">Email Reports</a>
-    page on {% site_name %} to set your notification preferences.
-
-{% else %}
-    This is a {{ monthly_or_weekly }} report
-    sent by <a href="{% site_root %}">{% site_name %}</a>.
-
-    <br />
-    {% include "emails/summary-downtimes-html.html" %}
-
-    <strong>Just one more thing to check:</strong>
-    Do you have more cron jobs,
-    not yet on this list, that would benefit from monitoring?
-    Get the ball rolling by adding one more!
-{% endif %}
+{% include "emails/report-summary-html.html" %}
 
+<strong>Just one more thing to check:</strong>
+Do you have more cron jobs, not yet on this list, that would benefit from monitoring?
+Get the ball rolling by adding one more!
 <br /><br />
 
 Cheers,<br>
diff --git a/templates/emails/report-body-text.html b/templates/emails/report-body-text.html
index 163b47b6..c3172cc8 100644
--- a/templates/emails/report-body-text.html
+++ b/templates/emails/report-body-text.html
@@ -1,8 +1,6 @@
 {% load hc_extras %}Hello,
 
-{% if nag %}This is a {% if nag_period == 3600 %}hourly {% endif %}{% if nag_period == 86400 %}daily {% endif %}reminder sent by {% site_name %}.
-
-{% if num_down == 1%}One check is currently DOWN:{% else %}{{ num_down }} checks are currently DOWN:{% endif %}{% else %}This is a {{ monthly_or_weekly }} report sent by {% site_name %}.{% endif %}
+This is a {{ monthly_or_weekly }} report sent by {% site_name %}.
 {% include 'emails/summary-text.html' %}
 
 
diff --git a/templates/emails/report-subject.html b/templates/emails/report-subject.html
index 0412f585..6819fe78 100644
--- a/templates/emails/report-subject.html
+++ b/templates/emails/report-subject.html
@@ -1,6 +1 @@
-{% if nag %}
-    Reminder: {{ num_down }} check{{ num_down|pluralize }} still down
-{% else %}
-    {{ monthly_or_weekly|capfirst }} Report
-{% endif %}
-
+{{ monthly_or_weekly|capfirst }} Report
\ No newline at end of file
diff --git a/templates/emails/summary-downtimes-html.html b/templates/emails/report-summary-html.html
similarity index 100%
rename from templates/emails/summary-downtimes-html.html
rename to templates/emails/report-summary-html.html