Skip to content

Commit

Permalink
feat: added email headers for notification cadence email (openedx#35078)
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadadeeltajamul authored Jul 4, 2024
1 parent 7da377e commit a763fd8
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 6 deletions.
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/notifications/email/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)
from .message_type import EmailNotificationMessageType
from .utils import (
add_headers_to_email_message,
create_app_notifications_dict,
create_email_digest_context,
filter_notification_with_email_enabled_preferences,
Expand Down Expand Up @@ -98,6 +99,7 @@ def send_digest_email_to_user(user, cadence_type, course_language='en', courses_
message = EmailNotificationMessageType(
app_label="notifications", name="email_digest"
).personalize(recipient, course_language, message_context)
message = add_headers_to_email_message(message, message_context)
ace.send(message)
logger.info(f'<Email Cadence> Email sent to {user.username} ==Temp Log==')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"""
import datetime
import ddt
import pytest

from django.http.response import Http404
from itertools import product
from pytz import utc
from waffle import get_waffle_flag_model # pylint: disable=invalid-django-waffle-import
Expand Down Expand Up @@ -429,5 +431,5 @@ def test_preference_not_updated_if_invalid_username(self):
username = f"{self.user.username}-updated"
enc_username = encrypt_string(username)
enc_patch = encrypt_object({"value": True})
with self.assertNumQueries(1):
with pytest.raises(Http404):
update_user_preferences_from_patch(enc_username, enc_patch)
28 changes: 27 additions & 1 deletion openedx/core/djangoapps/notifications/email/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
import json

from django.conf import settings
from django.contrib.auth import get_user_model
from django.shortcuts import get_object_or_404
from django.urls import reverse
from pytz import utc
from waffle import get_waffle_flag_model # pylint: disable=invalid-django-waffle-import

from common.djangoapps.student.models import CourseEnrollment
from lms.djangoapps.branding.api import get_logo_url_for_email
from lms.djangoapps.discussion.notification_prefs.views import UsernameCipher
from openedx.core.djangoapps.notifications.base_notification import (
Expand All @@ -23,6 +26,9 @@
from .notification_icons import NotificationTypeIcons


User = get_user_model()


def is_email_notification_flag_enabled(user=None):
"""
Returns if waffle flag is enabled for user or not
Expand Down Expand Up @@ -146,6 +152,16 @@ def create_email_digest_context(app_notifications_dict, username, start_date, en
return context


def add_headers_to_email_message(message, context):
"""
Add headers to email message
"""
if context.get('unsubscribe_url'):
message.headers['List-Unsubscribe-Post'] = f"<{context['unsubscribe_url']}>"
message.headers['List-Unsubscribe'] = f"<{context['unsubscribe_url']}>"
return message


def get_start_end_date(cadence_type):
"""
Returns start_date and end_date for email digest
Expand Down Expand Up @@ -319,8 +335,9 @@ def update_user_preferences_from_patch(encrypted_username, encrypted_patch):
type_value = patch.get("notification_type")
channel_value = patch.get("channel")
pref_value = bool(patch.get("value", False))
user = get_object_or_404(User, username=username)

kwargs = {'user__username': username}
kwargs = {'user': user}
if 'course_id' in patch.keys():
kwargs['course_id'] = patch['course_id']

Expand All @@ -346,7 +363,16 @@ def get_default_cadence_value(app_name, notification_type):
return COURSE_NOTIFICATION_APPS[app_name]['core_email_cadence']
return COURSE_NOTIFICATION_TYPES[notification_type]['email_cadence']

course_ids = CourseEnrollment.objects.filter(user=user).values_list('course_id', flat=True)
CourseNotificationPreference.objects.bulk_create(
[
CourseNotificationPreference(user=user, course_id=course_id)
for course_id in course_ids
],
ignore_conflicts=True
)
preferences = CourseNotificationPreference.objects.filter(**kwargs)

# pylint: disable=too-many-nested-blocks
for preference in preferences:
preference_json = preference.notification_preference_config
Expand Down
3 changes: 2 additions & 1 deletion requirements/edx/base.txt
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ drf-yasg==1.21.7
# via
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.8.0
edx-ace==1.9.1
# via -r requirements/edx/kernel.in
edx-api-doc-tools==1.8.0
# via
Expand Down Expand Up @@ -425,6 +425,7 @@ edx-django-utils==5.14.2
# via
# -r requirements/edx/kernel.in
# django-config-models
# edx-ace
# edx-drf-extensions
# edx-enterprise
# edx-event-bus-kafka
Expand Down
3 changes: 2 additions & 1 deletion requirements/edx/development.txt
Original file line number Diff line number Diff line change
Expand Up @@ -640,7 +640,7 @@ drf-yasg==1.21.7
# -r requirements/edx/testing.txt
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.8.0
edx-ace==1.9.1
# via
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
Expand Down Expand Up @@ -698,6 +698,7 @@ edx-django-utils==5.14.2
# -r requirements/edx/doc.txt
# -r requirements/edx/testing.txt
# django-config-models
# edx-ace
# edx-drf-extensions
# edx-enterprise
# edx-event-bus-kafka
Expand Down
3 changes: 2 additions & 1 deletion requirements/edx/doc.txt
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ drf-yasg==1.21.7
# -r requirements/edx/base.txt
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.8.0
edx-ace==1.9.1
# via -r requirements/edx/base.txt
edx-api-doc-tools==1.8.0
# via
Expand Down Expand Up @@ -497,6 +497,7 @@ edx-django-utils==5.14.2
# via
# -r requirements/edx/base.txt
# django-config-models
# edx-ace
# edx-drf-extensions
# edx-enterprise
# edx-event-bus-kafka
Expand Down
3 changes: 2 additions & 1 deletion requirements/edx/testing.txt
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ drf-yasg==1.21.7
# -r requirements/edx/base.txt
# django-user-tasks
# edx-api-doc-tools
edx-ace==1.8.0
edx-ace==1.9.1
# via -r requirements/edx/base.txt
edx-api-doc-tools==1.8.0
# via
Expand Down Expand Up @@ -530,6 +530,7 @@ edx-django-utils==5.14.2
# via
# -r requirements/edx/base.txt
# django-config-models
# edx-ace
# edx-drf-extensions
# edx-enterprise
# edx-event-bus-kafka
Expand Down

0 comments on commit a763fd8

Please sign in to comment.