55from celery import shared_task
66from celery .utils .log import get_task_logger
77from django .contrib .auth import get_user_model
8+ from django .utils .translation import gettext as _ , override as translation_override
89from edx_ace import ace
910from edx_ace .recipient import Recipient
1011from edx_django_utils .monitoring import set_code_owner_attribute
2425 create_email_template_context ,
2526 filter_notification_with_email_enabled_preferences ,
2627 get_course_info ,
28+ get_language_preference_for_users ,
2729 get_start_end_date ,
2830 get_text_for_notification_type ,
2931 get_unique_course_ids ,
30- is_email_notification_flag_enabled
32+ is_email_notification_flag_enabled ,
3133)
3234
3335
@@ -74,7 +76,7 @@ def get_user_preferences_for_courses(course_ids, user):
7476 return new_preferences
7577
7678
77- def send_digest_email_to_user (user , cadence_type , start_date , end_date , course_language = 'en' , courses_data = None ):
79+ def send_digest_email_to_user (user , cadence_type , start_date , end_date , user_language = 'en' , courses_data = None ):
7880 """
7981 Send [cadence_type] email to user.
8082 Cadence Type can be EmailCadence.DAILY or EmailCadence.WEEKLY
@@ -95,24 +97,26 @@ def send_digest_email_to_user(user, cadence_type, start_date, end_date, course_l
9597 if not notifications :
9698 logger .info (f'<Email Cadence> No notification for { user .username } ==Temp Log==' )
9799 return
98- course_ids = get_unique_course_ids (notifications )
99- preferences = get_user_preferences_for_courses (course_ids , user )
100- notifications = filter_notification_with_email_enabled_preferences (notifications , preferences , cadence_type )
101- if not notifications :
102- logger .info (f'<Email Cadence> No filtered notification for { user .username } ==Temp Log==' )
103- return
104- apps_dict = create_app_notifications_dict (notifications )
105- message_context = create_email_digest_context (apps_dict , user .username , start_date , end_date ,
106- cadence_type , courses_data = courses_data )
107- recipient = Recipient (user .id , user .email )
108- message = EmailNotificationMessageType (
109- app_label = "notifications" , name = "email_digest"
110- ).personalize (recipient , course_language , message_context )
111- message = add_headers_to_email_message (message , message_context )
112- message .options ['skip_disable_user_policy' ] = True
113- ace .send (message )
114- send_user_email_digest_sent_event (user , cadence_type , notifications , message_context )
115- logger .info (f'<Email Cadence> Email sent to { user .username } ==Temp Log==' )
100+
101+ with translation_override (user_language ):
102+ course_ids = get_unique_course_ids (notifications )
103+ preferences = get_user_preferences_for_courses (course_ids , user )
104+ notifications = filter_notification_with_email_enabled_preferences (notifications , preferences , cadence_type )
105+ if not notifications :
106+ logger .info (f'<Email Cadence> No filtered notification for { user .username } ==Temp Log==' )
107+ return
108+ apps_dict = create_app_notifications_dict (notifications )
109+ message_context = create_email_digest_context (apps_dict , user .username , start_date , end_date ,
110+ cadence_type , courses_data = courses_data )
111+ recipient = Recipient (user .id , user .email )
112+ message = EmailNotificationMessageType (
113+ app_label = "notifications" , name = "email_digest"
114+ ).personalize (recipient , user_language , message_context )
115+ message = add_headers_to_email_message (message , message_context )
116+ message .options ['skip_disable_user_policy' ] = True
117+ ace .send (message )
118+ send_user_email_digest_sent_event (user , cadence_type , notifications , message_context )
119+ logger .info (f'<Email Cadence> Email sent to { user .username } ==Temp Log==' )
116120
117121
118122@shared_task (ignore_result = True )
@@ -123,11 +127,14 @@ def send_digest_email_to_all_users(cadence_type):
123127 """
124128 logger .info (f'<Email Cadence> Sending cadence email of type { cadence_type } ' )
125129 users = get_audience_for_cadence_email (cadence_type )
130+ language_prefs = get_language_preference_for_users ([user .id for user in users ])
126131 courses_data = {}
127132 start_date , end_date = get_start_end_date (cadence_type )
128133 logger .info (f'<Email Cadence> Email Cadence Audience { len (users )} ' )
129134 for user in users :
130- send_digest_email_to_user (user , cadence_type , start_date , end_date , courses_data = courses_data )
135+ user_language = language_prefs .get (user .id , 'en' )
136+ send_digest_email_to_user (user , cadence_type , start_date , end_date , user_language = user_language ,
137+ courses_data = courses_data )
131138
132139
133140def send_immediate_cadence_email (email_notification_mapping , course_key ):
@@ -141,6 +148,7 @@ def send_immediate_cadence_email(email_notification_mapping, course_key):
141148 return
142149 user_list = email_notification_mapping .keys ()
143150 users = User .objects .filter (id__in = user_list )
151+ language_prefs = get_language_preference_for_users (user_list )
144152 course_name = get_course_info (course_key ).get ("name" , course_key )
145153 for user in users .iterator (chunk_size = 100 ):
146154 if not user .has_usable_password ():
@@ -153,21 +161,25 @@ def send_immediate_cadence_email(email_notification_mapping, course_key):
153161 if not notification :
154162 logger .info (f'<Immediate Email> No notification for { user .username } ' )
155163 continue
156- soup = BeautifulSoup (notification .content , "html.parser" )
157- title = "New Course Update" if notification .notification_type == "course_updates" else soup .get_text ()
158- message_context = create_email_template_context (user .username )
159- message_context .update ({
160- "course_id" : course_key ,
161- "course_name" : course_name ,
162- "content_url" : notification .content_url ,
163- "content_title" : title ,
164- "footer_email_reason" : "You are receiving this email because you are enrolled in the edX course "
165- f"{ course_name } " ,
166- "content" : notification .content_context .get ("email_content" , notification .content ),
167- "view_text" : get_text_for_notification_type (notification .notification_type ),
168- })
169- message = EmailNotificationMessageType (
170- app_label = "notifications" , name = "immediate_email"
171- ).personalize (Recipient (user .id , user .email ), 'en' , message_context )
172- message = add_headers_to_email_message (message , message_context )
173- ace .send (message )
164+
165+ language = language_prefs .get (user .id , 'en' )
166+ with translation_override (language ):
167+ soup = BeautifulSoup (notification .content , "html.parser" )
168+ title = _ ("New Course Update" ) if notification .notification_type == "course_updates" else soup .get_text ()
169+ message_context = create_email_template_context (user .username )
170+ message_context .update ({
171+ "course_id" : course_key ,
172+ "course_name" : course_name ,
173+ "content_url" : notification .content_url ,
174+ "content_title" : title ,
175+ "footer_email_reason" : _ (
176+ "You are receiving this email because you are enrolled in the edX course "
177+ ) + str (course_name ),
178+ "content" : notification .content_context .get ("email_content" , notification .content ),
179+ "view_text" : get_text_for_notification_type (notification .notification_type ),
180+ })
181+ message = EmailNotificationMessageType (
182+ app_label = "notifications" , name = "immediate_email"
183+ ).personalize (Recipient (user .id , user .email ), language , message_context )
184+ message = add_headers_to_email_message (message , message_context )
185+ ace .send (message )
0 commit comments