|
3 | 3 | from celery import shared_task
|
4 | 4 | from django.contrib.auth import get_user_model
|
5 | 5 | from django.contrib.contenttypes.models import ContentType
|
| 6 | +from django.contrib.sites.models import Site |
6 | 7 | from django.core.cache import cache
|
7 | 8 | from django.db.models import Q
|
8 | 9 | from django.db.utils import OperationalError
|
9 | 10 | from django.template.loader import render_to_string
|
10 | 11 | from django.utils import timezone
|
11 | 12 | from django.utils.html import strip_tags
|
12 | 13 |
|
| 14 | +from openwisp_notifications import handlers |
13 | 15 | from openwisp_notifications import settings as app_settings
|
14 | 16 | from openwisp_notifications import types
|
15 | 17 | from openwisp_notifications.swapper import load_model, swapper_load_model
|
@@ -222,36 +224,56 @@ def batch_email_notification(email_id):
|
222 | 224 | return
|
223 | 225 |
|
224 | 226 | 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', |
242 | 269 | }
|
243 |
| - ) |
244 | 270 |
|
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 | + ) |
256 | 278 | unsent_notifications.update(emailed=True)
|
257 | 279 | Notification.objects.bulk_update(unsent_notifications, ['emailed'])
|
0 commit comments