From f3f35efa205c5166fc4682f9707d024c4d1f7304 Mon Sep 17 00:00:00 2001 From: muhammadadeeltajamul Date: Tue, 29 Oct 2024 13:37:16 +0500 Subject: [PATCH] feat: truncated number of notifications in email --- .../djangoapps/notifications/email/events.py | 12 +++++++++- .../djangoapps/notifications/email/tasks.py | 2 +- .../djangoapps/notifications/email/utils.py | 23 +++++++++++++++---- .../notifications/digest_content.html | 10 +++++++- 4 files changed, 39 insertions(+), 8 deletions(-) diff --git a/openedx/core/djangoapps/notifications/email/events.py b/openedx/core/djangoapps/notifications/email/events.py index 165539a018cb..a575d78eaeb5 100644 --- a/openedx/core/djangoapps/notifications/email/events.py +++ b/openedx/core/djangoapps/notifications/email/events.py @@ -12,19 +12,29 @@ 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 0d450fe9a917..c2e0a2fa375d 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/utils.py b/openedx/core/djangoapps/notifications/email/utils.py index 582e867d629d..824f62fcf0ac 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 f2e239bb7e6e..a8dad31d8310 100644 --- a/openedx/core/djangoapps/notifications/templates/notifications/digest_content.html +++ b/openedx/core/djangoapps/notifications/templates/notifications/digest_content.html @@ -55,5 +55,13 @@

+

+ {% if notification_app.show_remaining_count %} +

+ + + {{ notification_app.remaining_count }} more + +

+ {% endif %} +

{% endfor %}