From b55a17a9f72490681d84abf45e18ec4422cc0ff1 Mon Sep 17 00:00:00 2001
From: Muhammad Adeel Tajamul
<77053848+muhammadadeeltajamul@users.noreply.github.com>
Date: Thu, 21 Nov 2024 11:55:09 +0500
Subject: [PATCH] feat: truncated number of notifications in email (#35738)
---
.../djangoapps/notifications/email/events.py | 12 +++++++++-
.../djangoapps/notifications/email/tasks.py | 2 +-
.../notifications/email/tests/test_utils.py | 14 +++++++++--
.../djangoapps/notifications/email/utils.py | 23 +++++++++++++++----
.../notifications/digest_content.html | 10 +++++++-
5 files changed, 51 insertions(+), 10 deletions(-)
diff --git a/openedx/core/djangoapps/notifications/email/events.py b/openedx/core/djangoapps/notifications/email/events.py
index 165539a018..a575d78eae 100644
--- a/openedx/core/djangoapps/notifications/email/events.py
+++ b/openedx/core/djangoapps/notifications/email/events.py
@@ -12,19 +12,29 @@ from openedx.core.djangoapps.notifications.base_notification import COURSE_NOTIF
EMAIL_DIGEST_SENT = "edx.notifications.email_digest"
-def send_user_email_digest_sent_event(user, cadence_type, notifications):
+def send_user_email_digest_sent_event(user, cadence_type, notifications, message_context):
"""
Sends tracker and segment email for user email digest
"""
notification_breakdown = {key: 0 for key in COURSE_NOTIFICATION_APPS.keys()}
for notification in notifications:
notification_breakdown[notification.app_name] += 1
+
+ truncated_count = {}
+ email_content = message_context.get("email_content", [])
+ for app in email_content:
+ truncated_count[app.get("title", "")] = {
+ "total": app.get("total", -1),
+ "remaining_count": app.get("remaining_count", -1),
+ }
+
event_data = {
"username": user.username,
"email": user.email,
"cadence_type": cadence_type,
"total_notifications_count": len(notifications),
"count_breakdown": notification_breakdown,
+ "truncated_count": truncated_count,
"notification_ids": [notification.id for notification in notifications],
"send_at": str(datetime.datetime.now())
}
diff --git a/openedx/core/djangoapps/notifications/email/tasks.py b/openedx/core/djangoapps/notifications/email/tasks.py
index 0d450fe9a9..c2e0a2fa37 100644
--- a/openedx/core/djangoapps/notifications/email/tasks.py
+++ b/openedx/core/djangoapps/notifications/email/tasks.py
@@ -103,7 +103,7 @@ def send_digest_email_to_user(user, cadence_type, start_date, end_date, course_l
).personalize(recipient, course_language, message_context)
message = add_headers_to_email_message(message, message_context)
ace.send(message)
- send_user_email_digest_sent_event(user, cadence_type, notifications)
+ send_user_email_digest_sent_event(user, cadence_type, notifications, message_context)
logger.info(f' Email sent to {user.username} ==Temp Log==')
diff --git a/openedx/core/djangoapps/notifications/email/tests/test_utils.py b/openedx/core/djangoapps/notifications/email/tests/test_utils.py
index 1f3da983a0..6c7f5b7144 100644
--- a/openedx/core/djangoapps/notifications/email/tests/test_utils.py
+++ b/openedx/core/djangoapps/notifications/email/tests/test_utils.py
@@ -148,8 +148,18 @@ class TestContextFunctions(ModuleStoreTestCase):
{'title': 'Updates', 'count': 1},
]
expected_email_content = [
- {'title': 'Discussion', 'help_text': '', 'help_text_url': '', 'notifications': [discussion_notification]},
- {'title': 'Updates', 'help_text': '', 'help_text_url': '', 'notifications': [update_notification]}
+ {
+ 'title': 'Discussion', 'help_text': '', 'help_text_url': '',
+ 'notifications': [discussion_notification],
+ 'total': 1, 'show_remaining_count': False, 'remaining_count': 0,
+ 'url': 'http://learner-home-mfe/?showNotifications=true&app=discussion'
+ },
+ {
+ 'title': 'Updates', 'help_text': '', 'help_text_url': '',
+ 'notifications': [update_notification],
+ 'total': 1, 'show_remaining_count': False, 'remaining_count': 0,
+ 'url': 'http://learner-home-mfe/?showNotifications=true&app=updates'
+ }
]
assert context['start_date'] == expected_start_date
assert context['end_date'] == 'Sunday, Mar 24'
diff --git a/openedx/core/djangoapps/notifications/email/utils.py b/openedx/core/djangoapps/notifications/email/utils.py
index d855494012..9fd761785e 100644
--- a/openedx/core/djangoapps/notifications/email/utils.py
+++ b/openedx/core/djangoapps/notifications/email/utils.py
@@ -130,17 +130,29 @@ def create_email_digest_context(app_notifications_dict, username, start_date, en
}
for key, value in app_notifications_dict.items()
])
- email_content = [
- {
+
+ email_content = []
+ notifications_in_app = 5
+ for key, value in app_notifications_dict.items():
+ total = value['count']
+ app_content = {
'title': value['title'],
'help_text': value.get('help_text', ''),
'help_text_url': value.get('help_text_url', ''),
'notifications': add_additional_attributes_to_notifications(
value.get('notifications', []), courses_data=courses_data
- )
+ ),
+ 'total': total,
+ 'show_remaining_count': False,
+ 'remaining_count': 0,
+ 'url': f'{settings.LEARNER_HOME_MICROFRONTEND_URL}/?showNotifications=true&app={key}'
}
- for key, value in app_notifications_dict.items()
- ]
+ if total > notifications_in_app:
+ app_content['notifications'] = app_content['notifications'][:notifications_in_app]
+ app_content['show_remaining_count'] = True
+ app_content['remaining_count'] = total - notifications_in_app
+ email_content.append(app_content)
+
context.update({
"start_date": start_date_str,
"end_date": end_date_str,
@@ -295,6 +307,7 @@ def filter_notification_with_email_enabled_preferences(notifications, preference
for notification in notifications:
if notification.notification_type in enabled_course_prefs[notification.course_id]:
filtered_notifications.append(notification)
+ filtered_notifications.sort(key=lambda elem: elem.created, reverse=True)
return filtered_notifications
diff --git a/openedx/core/djangoapps/notifications/templates/notifications/digest_content.html b/openedx/core/djangoapps/notifications/templates/notifications/digest_content.html
index d482cd0c44..51966f96f5 100644
--- a/openedx/core/djangoapps/notifications/templates/notifications/digest_content.html
+++ b/openedx/core/djangoapps/notifications/templates/notifications/digest_content.html
@@ -56,5 +56,13 @@
-
+
+ {% if notification_app.show_remaining_count %}
+
+
+ + {{ notification_app.remaining_count }} more
+
+
+ {% endif %}
+
{% endfor %}