Skip to content

Commit 8dea29f

Browse files
committed
[change] Improved batch email template
- Improved formatting of date time - Fixed semantics in HTML template
1 parent 0d816d4 commit 8dea29f

File tree

4 files changed

+39
-30
lines changed

4 files changed

+39
-30
lines changed

openwisp_notifications/tasks.py

+5-9
Original file line numberDiff line numberDiff line change
@@ -265,19 +265,15 @@ def send_batched_email_notifications(instance_id):
265265

266266
unsent_notifications.append(notification)
267267

268-
starting_time = (
269-
cache_data.get('start_time')
270-
.strftime('%B %-d, %Y, %-I:%M %p')
271-
.lower()
272-
.replace('am', 'a.m.')
273-
.replace('pm', 'p.m.')
274-
) + ' UTC'
268+
start_time = timezone.localtime(cache_data.get('start_time')).strftime(
269+
'%B %-d, %Y, %-I:%M %p %Z'
270+
)
275271

276272
context = {
277273
'notifications': unsent_notifications[:display_limit],
278274
'notifications_count': notifications_count,
279275
'site_name': current_site.name,
280-
'start_time': starting_time,
276+
'start_time': start_time,
281277
}
282278

283279
extra_context = {}
@@ -293,7 +289,7 @@ def send_batched_email_notifications(instance_id):
293289
notifications_count = min(notifications_count, display_limit)
294290

295291
send_email(
296-
subject=f'[{current_site.name}] {notifications_count} new notifications since {starting_time}',
292+
subject=f'[{current_site.name}] {notifications_count} new notifications since {start_time}',
297293
body_text=plain_text_content,
298294
body_html=html_content,
299295
recipients=[email_id],

openwisp_notifications/templates/emails/batch_email.html

+19-9
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@
33
.alert {
44
border: 1px solid #e0e0e0;
55
border-radius: 5px;
6-
margin-bottom: 10px;
76
padding: 10px;
87
}
8+
.alert + .alert {
9+
margin-top: 10px;
10+
}
911
.alert.error {
1012
background-color: #ffefef;
1113
}
@@ -60,22 +62,30 @@
6062
background-color: #1c8828;
6163
}
6264
.badge.warning {
63-
background-color: #f0ad4e;
65+
background-color: #ec942c;
6466
}
6567
.alert a {
6668
text-decoration: none;
6769
}
68-
.alert.error a, .alert.error h2 {
70+
.alert.error a,
71+
.alert.error h2,
72+
.alert.error p {
6973
color: #d9534f;
7074
}
71-
.alert.info a, .alert.info h2 {
75+
.alert.info a,
76+
.alert.info h2,
77+
.alert.info p {
7278
color: #333333;
7379
}
74-
.alert.success a, .alert.success h2 {
80+
.alert.success a,
81+
.alert.success h2,
82+
.alert.success p {
7583
color: #1c8828;
7684
}
77-
.alert.warning a, .alert.warning h2 {
78-
color: #f0ad4e;
85+
.alert.warning a,
86+
.alert.warning h2,
87+
.alert.warning p {
88+
color: #ec942c;
7989
}
8090
.alert a:hover {
8191
text-decoration: underline;
@@ -91,13 +101,13 @@ <h2>
91101
<span class="badge {{ notification.level }}">{{ notification.level|upper }}</span>
92102
<span class="title">
93103
{% if notification.url %}
94-
<a href="{{ notification.url }}" target="_blank">{{ notification.email_message }}</a>
104+
<a href="{{ notification.url }}" target="_blank"><p>{{ notification.email_message|striptags }}</p></a>
95105
{% else %}
96106
{{ notification.email_message }}
97107
{% endif %}
98108
</span>
99109
</h2>
100-
<p>{{ notification.timestamp|date:"F j, Y, g:i a" }}</p>
110+
<p>{{ notification.timestamp|date:"F j, Y, g:i A e" }}</p>
101111
{% if notification.rendered_description %}
102112
<p>{{ notification.rendered_description|safe }}</p>
103113
{% endif %}

openwisp_notifications/templates/emails/batch_email.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
{% for notification in notifications %}
66
- {{ notification.email_message }}{% if notification.rendered_description %}
77
{% translate "Description" %}: {{ notification.rendered_description }}{% endif %}
8-
{% translate "Date & Time" %}: {{ notification.timestamp|date:"F j, Y, g:i a" }}{% if notification.url %}
8+
{% translate "Date & Time" %}: {{ notification.timestamp|date:"F j, Y, g:i A e" }}{% if notification.url %}
99
{% translate "URL" %}: {{ notification.url }}{% endif %}
1010
{% endfor %}
1111

openwisp_notifications/tests/test_notifications.py

+14-11
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from django.urls import reverse
1717
from django.utils import timezone
1818
from django.utils.timesince import timesince
19+
from freezegun import freeze_time
1920

2021
from openwisp_notifications import settings as app_settings
2122
from openwisp_notifications import tasks
@@ -950,11 +951,15 @@ def test_notification_for_unverified_email(self):
950951
# we don't send emails to unverified email addresses
951952
self.assertEqual(len(mail.outbox), 0)
952953

954+
# @override_settings(TIME_ZONE='UTC')
953955
@patch('openwisp_notifications.tasks.send_batched_email_notifications.apply_async')
954956
def test_batch_email_notification(self, mock_send_email):
955-
fixed_datetime = datetime(2024, 7, 26, 11, 40)
957+
fixed_datetime = timezone.localtime(
958+
datetime(2024, 7, 26, 11, 40, tzinfo=timezone.utc)
959+
)
960+
datetime_str = fixed_datetime.strftime('%B %-d, %Y, %-I:%M %p %Z')
956961

957-
with patch.object(timezone, 'now', return_value=fixed_datetime):
962+
with freeze_time(fixed_datetime):
958963
for _ in range(5):
959964
notify.send(recipient=self.admin, **self.notification_options)
960965

@@ -967,31 +972,29 @@ def test_batch_email_notification(self, mock_send_email):
967972
# Check if the rest of the notifications are sent in a batch
968973
self.assertEqual(len(mail.outbox), 2)
969974

970-
expected_subject = (
971-
'[example.com] 4 new notifications since july 26, 2024, 11:40 a.m. UTC'
972-
)
973-
expected_body = """
974-
[example.com] 4 new notifications since july 26, 2024, 11:40 a.m. UTC
975+
expected_subject = f'[example.com] 4 new notifications since {datetime_str}'
976+
expected_body = f"""
977+
[example.com] 4 new notifications since {datetime_str}
975978
976979
977980
- Test Notification
978981
Description: Test Notification
979-
Date & Time: July 26, 2024, 11:40 a.m.
982+
Date & Time: {datetime_str}
980983
URL: https://localhost:8000/admin
981984
982985
- Test Notification
983986
Description: Test Notification
984-
Date & Time: July 26, 2024, 11:40 a.m.
987+
Date & Time: {datetime_str}
985988
URL: https://localhost:8000/admin
986989
987990
- Test Notification
988991
Description: Test Notification
989-
Date & Time: July 26, 2024, 11:40 a.m.
992+
Date & Time: {datetime_str}
990993
URL: https://localhost:8000/admin
991994
992995
- Test Notification
993996
Description: Test Notification
994-
Date & Time: July 26, 2024, 11:40 a.m.
997+
Date & Time: {datetime_str}
995998
URL: https://localhost:8000/admin
996999
"""
9971000

0 commit comments

Comments
 (0)