Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] Batch email notifications #276

Merged
merged 25 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
2c780c8
[chore] Basic implementation
Dhanus3133 Jun 4, 2024
949ea24
[chore] Updated changes
Dhanus3133 Jun 6, 2024
305d6c8
[chore] Add tests
Dhanus3133 Jun 7, 2024
0e54696
Merge branch 'master' into feat/batch-email
Dhanus3133 Jun 7, 2024
58bdc9d
[chore] Update Readme
Dhanus3133 Jun 8, 2024
4abc717
[chore] New changes
Dhanus3133 Jun 15, 2024
caf3031
Merge branch 'master' into feat/batch-email
Dhanus3133 Jun 15, 2024
233a1e7
[chore] Improvements
Dhanus3133 Jun 17, 2024
69365bb
[chore] Fix cache delete
Dhanus3133 Jun 17, 2024
9d38db1
[chore] New changes
Dhanus3133 Jun 18, 2024
01182c4
[chore] Updates
Dhanus3133 Jun 18, 2024
483df00
[chore] Add extra tests
Dhanus3133 Jun 18, 2024
68a1604
[fix] Tests
Dhanus3133 Jun 21, 2024
6757f7b
[chore] Bump changes
Dhanus3133 Jul 6, 2024
a826d5e
[feat] Automatically open notification widget
Dhanus3133 Jul 6, 2024
7a55502
[chore] Open notifications widget with #notifications
Dhanus3133 Jul 10, 2024
67de9e5
[tests] Fixed test_without_batch_email_notification
nemesifier Jul 15, 2024
12288de
[fix] Updated
Dhanus3133 Jul 17, 2024
3760173
[chore] Update tests
Dhanus3133 Jul 17, 2024
2087b3e
Merge branch 'gsoc24' into feat/batch-email
Dhanus3133 Jul 17, 2024
56ac4cf
[chore] Update email title notifications count
Dhanus3133 Jul 17, 2024
12d19e2
[QA] Checks
Dhanus3133 Jul 17, 2024
49087b2
[chore] Update batch_email txt
Dhanus3133 Jul 18, 2024
a34920a
[fix] Use email_message instead of message
Dhanus3133 Jul 24, 2024
65dbdc1
[chore] Add email content tests
Dhanus3133 Jul 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,19 @@ The default configuration is as follows:
'max_allowed_backoff': 15,
}

``OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

+---------+-----------------------------------+
| type | ``int`` |
+---------+-----------------------------------+
| default | ``1800`` `(30 mins, in seconds)` |
+---------+-----------------------------------+

This setting defines the interval at which the email notifications are sent in batches to users within the specified interval.

If you want to send email notifications immediately, then set it to ``0``.

Exceptions
----------

Expand Down
81 changes: 55 additions & 26 deletions openwisp_notifications/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
logger = logging.getLogger(__name__)

EXTRA_DATA = app_settings.get_config()['USE_JSONFIELD']
EMAIL_BATCH_INTERVAL = app_settings.OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL

User = get_user_model()

Expand Down Expand Up @@ -164,6 +165,36 @@ def notify_handler(**kwargs):
return notification_list


def send_single_email(instance):
try:
subject = instance.email_subject
except NotificationRenderException:
# Do not send email if notification is malformed.
return
url = instance.data.get('url', '') if instance.data else None
description = instance.message
if url:
target_url = url
elif instance.target:
target_url = instance.redirect_view_url
else:
target_url = None
if target_url:
description += _('\n\nFor more information see %(target_url)s.') % {
'target_url': target_url
}
send_email(
subject,
description,
instance.message,
recipients=[instance.recipient.email],
extra_context={
'call_to_action_url': target_url,
'call_to_action_text': _('Find out more'),
},
)


@receiver(post_save, sender=Notification, dispatch_uid='send_email_notification')
def send_email_notification(sender, instance, created, **kwargs):
# Abort if a new notification is not created
Expand Down Expand Up @@ -192,34 +223,32 @@ def send_email_notification(sender, instance, created, **kwargs):
if not (email_preference and instance.recipient.email and email_verified):
return

try:
subject = instance.email_subject
except NotificationRenderException:
# Do not send email if notification is malformed.
recipient_id = instance.recipient.id
cache_key = f'email_batch_{recipient_id}'
cache_key_pks = f'{instance.recipient.email}_pks'
cache_data = cache.get(
cache_key, {'last_email_sent_time': None, 'batch_scheduled': False}
)

if cache_data['last_email_sent_time'] and EMAIL_BATCH_INTERVAL > 0:
if not cache_data['batch_scheduled']:
# Schedule batch email notification task
tasks.batch_email_notification.apply_async(
(instance.recipient.email,), countdown=EMAIL_BATCH_INTERVAL
)
cache_data['batch_scheduled'] = True
cache.set(cache_key_pks, [instance.id], timeout=EMAIL_BATCH_INTERVAL * 2)
cache.set(cache_key, cache_data, timeout=EMAIL_BATCH_INTERVAL)
else:
ids = cache.get(cache_key_pks, [])
ids.append(instance.id)
cache.set(cache_key_pks, ids, timeout=EMAIL_BATCH_INTERVAL * 2)
return
url = instance.data.get('url', '') if instance.data else None
body_text = instance.email_message
if url:
target_url = url
elif instance.target:
target_url = instance.redirect_view_url
else:
target_url = None
if target_url:
body_text += _('\n\nFor more information see %(target_url)s.') % {
'target_url': target_url
}

send_email(
subject=subject,
body_text=body_text,
body_html=instance.email_message,
recipients=[instance.recipient.email],
extra_context={
'call_to_action_url': target_url,
'call_to_action_text': _('Find out more'),
},
)
cache_data['last_email_sent_time'] = timezone.now()
cache.set(cache_key, cache_data, timeout=EMAIL_BATCH_INTERVAL)

send_single_email(instance)

# flag as emailed
instance.emailed = True
Expand Down
4 changes: 4 additions & 0 deletions openwisp_notifications/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@
'openwisp-notifications/audio/notification_bell.mp3',
)

OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL = getattr(
settings, 'EMAIL_BATCH_INTERVAL', 30 * 60 # 30 minutes
)

# Remove the leading "/static/" here as it will
# conflict with the "static()" call in context_processors.py.
# This is done for backward compatibility.
Expand Down
75 changes: 75 additions & 0 deletions openwisp_notifications/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
from celery import shared_task
from django.contrib.auth import get_user_model
from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.core.cache import cache
from django.db.models import Q
from django.db.utils import OperationalError
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.html import strip_tags

from openwisp_notifications import handlers
from openwisp_notifications import settings as app_settings
from openwisp_notifications import types
from openwisp_notifications.swapper import load_model, swapper_load_model
from openwisp_utils.admin_theme.email import send_email
from openwisp_utils.tasks import OpenwispCeleryTask

EMAIL_BATCH_INTERVAL = app_settings.OPENWISP_NOTIFICATIONS_EMAIL_BATCH_INTERVAL

User = get_user_model()

Notification = load_model('Notification')
Expand Down Expand Up @@ -202,3 +211,69 @@ def delete_ignore_object_notification(instance_id):
Deletes IgnoreObjectNotification object post it's expiration.
"""
IgnoreObjectNotification.objects.filter(id=instance_id).delete()


@shared_task(base=OpenwispCeleryTask)
def batch_email_notification(email_id):
"""
Sends a summary of notifications to the specified email address.
"""
ids = cache.get(f'{email_id}_pks', [])

if not ids:
return

unsent_notifications = Notification.objects.filter(id__in=ids)
notifications_count = unsent_notifications.count()
current_site = Site.objects.get_current()

if notifications_count == 1:
instance = unsent_notifications.first()
handlers.send_single_email(instance)
else:
alerts = []
for notification in unsent_notifications:
url = notification.data.get('url', '') if notification.data else None
if url:
target_url = url
elif notification.target:
target_url = notification.redirect_view_url
else:
target_url = None

description = notification.message if notifications_count <= 5 else None
alerts.append(
{
'level': notification.level,
'message': notification.message,
'description': description,
'timestamp': notification.timestamp,
'url': target_url,
}
)

context = {
'alerts': alerts,
'notifications_count': notifications_count,
'site_name': current_site.name,
'site_domain': current_site.domain,
}
html_content = render_to_string('emails/batch_email.html', context)
plain_text_content = strip_tags(html_content)

extra_context = {}
if notifications_count > 5:
extra_context = {
'call_to_action_url': f"https://{current_site.domain}/admin",
'call_to_action_text': 'View all Notifications',
}

send_email(
subject=f'Summary of {notifications_count} Notifications',
body_text=plain_text_content,
body_html=html_content,
recipients=[email_id],
extra_context=extra_context,
)
unsent_notifications.update(emailed=True)
Notification.objects.bulk_update(unsent_notifications, ['emailed'])
133 changes: 133 additions & 0 deletions openwisp_notifications/templates/emails/batch_email.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
{% block styles %}
<style type="text/css">
.container-alerts {
margin: 0 auto;
background-color: #fff;
padding: 20px;
border: 1px solid #ddd;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.header {
text-align: center;
padding: 10px 0;
border-bottom: 1px solid #ddd;
margin-bottom: 20px;
}
.header h1 {
margin: 0;
font-size: 24px;
}
.header p {
margin: 5px 0 0 0;
font-size: 14px;
color: #666;
}
.alert {
border: 1px solid #e0e0e0;
border-radius: 5px;
margin-bottom: 10px;
padding: 10px;
}
.alert.error {
background-color: #ffefef;
}
.alert.info {
background-color: #f0f0f0;
}
.alert.success {
background-color: #e6f9e8;
}
.alert h2 {
margin: 0 0 5px 0;
font-size: 16px;
}
.alert h2 .title {
display: inline-block;
max-width: 80%;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
vertical-align: middle;
}
.alert.error h2 {
color: #d9534f;
}
.alert.info h2 {
color: #333333;
}
.alert.success h2 {
color: #1c8828;
}
.alert p {
margin: 0;
font-size: 14px;
color: #666;
}
.alert .title p {
display: inline;
overflow: hidden;
text-overflow: ellipsis;
}
.badge {
display: inline-block;
padding: 2px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: bold;
text-transform: uppercase;
margin-right: 5px;
color: white;
}
.badge.error {
background-color: #d9534f;
}
.badge.info {
background-color: #333333;
}
.badge.success {
background-color: #1c8828;
}
.alert a {
text-decoration: none;
}
.alert.error a {
color: #d9534f;
}
.alert.info a {
color: #333333;
}
.alert.success a {
color: #1c8828;
}
.alert a:hover {
text-decoration: underline;
}
</style>
{% endblock styles %}

{% block mail_body %}
<div class="container-alerts">
<div class="header">
<h1>Summary of {{ notifications_count }} Notifications</h1>
<p>From <a href="https://{{ site_domain }}" target="_blank">{{ site_name }}</a></p>
</div>
{% for alert in alerts %}
<div class="alert {{ alert.level }}">
<h2>
<span class="badge {{ alert.level }}">{{ alert.level|upper }}</span>
<span class="title">
{% if alert.url %}
<a href="{{ alert.url }}" target="_blank">{{ alert.message }}</a>
{% else %}
{{ alert.message }}
{% endif %}
</span>
</h2>
<p>{{ alert.timestamp|date:"F j, Y, g:i a" }}</p>
{% if alert.description %}
<p>{{ alert.description }}</p>
{% endif %}
</div>
{% endfor %}
</div>
{% endblock mail_body %}
16 changes: 16 additions & 0 deletions openwisp_notifications/tests/test_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -944,6 +944,22 @@ def test_notification_for_unverified_email(self):
# we don't send emails to unverified email addresses
self.assertEqual(len(mail.outbox), 0)

@patch('openwisp_notifications.tasks.batch_email_notification.apply_async')
def test_batch_email_notification(self, mock_send_email):
notify.send(**self.notification_options)
notify.send(**self.notification_options)
notify.send(**self.notification_options)

# Check if only one mail is sent
self.assertEqual(len(mail.outbox), 1)

# Call the task
tasks.batch_email_notification(self.admin.email)

# Check if the rest of the notifications are sent in a batch
self.assertEqual(len(mail.outbox), 2)
self.assertEqual(mail.outbox[1].subject, "Summary of 2 Notifications")

def test_that_the_notification_is_only_sent_once_to_the_user(self):
first_org = self._create_org()
first_org.organization_id = first_org.id
Expand Down
Loading