Skip to content

Commit 4abc717

Browse files
committed
[chore] New changes
1 parent 58bdc9d commit 4abc717

File tree

3 files changed

+212
-149
lines changed

3 files changed

+212
-149
lines changed

openwisp_notifications/handlers.py

+34-27
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,36 @@ def notify_handler(**kwargs):
162162
return notification_list
163163

164164

165+
def send_single_email(instance):
166+
try:
167+
subject = instance.email_subject
168+
except NotificationRenderException:
169+
# Do not send email if notification is malformed.
170+
return
171+
url = instance.data.get('url', '') if instance.data else None
172+
description = instance.message
173+
if url:
174+
target_url = url
175+
elif instance.target:
176+
target_url = instance.redirect_view_url
177+
else:
178+
target_url = None
179+
if target_url:
180+
description += _('\n\nFor more information see %(target_url)s.') % {
181+
'target_url': target_url
182+
}
183+
send_email(
184+
subject,
185+
description,
186+
instance.message,
187+
recipients=[instance.recipient.email],
188+
extra_context={
189+
'call_to_action_url': target_url,
190+
'call_to_action_text': _('Find out more'),
191+
},
192+
)
193+
194+
165195
@receiver(post_save, sender=Notification, dispatch_uid='send_email_notification')
166196
def send_email_notification(sender, instance, created, **kwargs):
167197
# Abort if a new notification is not created
@@ -190,23 +220,7 @@ def send_email_notification(sender, instance, created, **kwargs):
190220
if not (email_preference and instance.recipient.email and email_verified):
191221
return
192222

193-
try:
194-
subject = instance.email_subject
195-
except NotificationRenderException:
196-
# Do not send email if notification is malformed.
197-
return
198-
url = instance.data.get('url', '') if instance.data else None
199-
description = instance.message
200-
if url:
201-
target_url = url
202-
elif instance.target:
203-
target_url = instance.redirect_view_url
204-
else:
205-
target_url = None
206-
if target_url:
207-
description += _('\n\nFor more information see %(target_url)s.') % {
208-
'target_url': target_url
209-
}
223+
send_single_email(instance)
210224

211225
recipient_id = instance.recipient.id
212226
cache_key = f'email_batch_{recipient_id}'
@@ -232,16 +246,9 @@ def send_email_notification(sender, instance, created, **kwargs):
232246

233247
cache_data['last_email_sent_time'] = timezone.now()
234248
cache.set(cache_key, cache_data, timeout=EMAIL_BATCH_INTERVAL)
235-
send_email(
236-
subject,
237-
description,
238-
instance.message,
239-
recipients=[instance.recipient.email],
240-
extra_context={
241-
'call_to_action_url': target_url,
242-
'call_to_action_text': _('Find out more'),
243-
},
244-
)
249+
250+
send_single_email(instance)
251+
245252
# flag as emailed
246253
instance.emailed = True
247254
# bulk_update is used to prevent emitting post_save signal

openwisp_notifications/tasks.py

+51-29
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
from celery import shared_task
44
from django.contrib.auth import get_user_model
55
from django.contrib.contenttypes.models import ContentType
6+
from django.contrib.sites.models import Site
67
from django.core.cache import cache
78
from django.db.models import Q
89
from django.db.utils import OperationalError
910
from django.template.loader import render_to_string
1011
from django.utils import timezone
1112
from django.utils.html import strip_tags
1213

14+
from openwisp_notifications import handlers
1315
from openwisp_notifications import settings as app_settings
1416
from openwisp_notifications import types
1517
from openwisp_notifications.swapper import load_model, swapper_load_model
@@ -222,36 +224,56 @@ def batch_email_notification(email_id):
222224
return
223225

224226
unsent_notifications = Notification.objects.filter(id__in=ids)
225-
alerts = []
226-
227-
for notification in unsent_notifications:
228-
url = notification.data.get('url', '') if notification.data else None
229-
if url:
230-
target_url = url
231-
elif notification.target:
232-
target_url = notification.redirect_view_url
233-
else:
234-
target_url = None
235-
alerts.append(
236-
{
237-
'level': notification.level,
238-
'message': notification.message,
239-
'description': notification.message,
240-
'timestamp': notification.timestamp,
241-
'url': target_url,
227+
notifications_count = unsent_notifications.count()
228+
current_site = Site.objects.get_current()
229+
230+
if notifications_count == 1:
231+
instance = unsent_notifications.first()
232+
handlers.send_single_email(instance)
233+
else:
234+
alerts = []
235+
for notification in unsent_notifications:
236+
url = notification.data.get('url', '') if notification.data else None
237+
if url:
238+
target_url = url
239+
elif notification.target:
240+
target_url = notification.redirect_view_url
241+
else:
242+
target_url = None
243+
244+
description = notification.message if notifications_count <= 5 else None
245+
alerts.append(
246+
{
247+
'level': notification.level,
248+
'message': notification.message,
249+
'description': description,
250+
'timestamp': notification.timestamp,
251+
'url': target_url,
252+
}
253+
)
254+
255+
context = {
256+
'alerts': alerts,
257+
'notifications_count': notifications_count,
258+
'site_name': current_site.name,
259+
'site_domain': current_site.domain,
260+
}
261+
html_content = render_to_string('emails/batch_email.html', context)
262+
plain_text_content = strip_tags(html_content)
263+
264+
extra_context = {}
265+
if notifications_count > 5:
266+
extra_context = {
267+
'call_to_action_url': f"https://{current_site.domain}/admin",
268+
'call_to_action_text': 'View all Notifications',
242269
}
243-
)
244270

245-
context = {
246-
'alerts': alerts,
247-
}
248-
html_content = render_to_string('emails/batch_email.html', context)
249-
plain_text_content = strip_tags(html_content)
250-
send_email(
251-
subject='Summary of Notifications',
252-
body_text=plain_text_content,
253-
body_html=html_content,
254-
recipients=[email_id],
255-
)
271+
send_email(
272+
subject=f'Summary of {notifications_count} Notifications',
273+
body_text=plain_text_content,
274+
body_html=html_content,
275+
recipients=[email_id],
276+
extra_context=extra_context,
277+
)
256278
unsent_notifications.update(emailed=True)
257279
Notification.objects.bulk_update(unsent_notifications, ['emailed'])

0 commit comments

Comments
 (0)