feat: save author pronoun separately for notification to prevent info loss (#35234)

* feat: save author pronoun separately for notification to prevent info loss

* fix: missing pronoun in comment on followed post

* test: updated tests for new comment notifications
This commit is contained in:
Eemaan Amir
2024-08-12 12:04:20 +05:00
committed by GitHub
parent 25d5d084e0
commit 152b678e62
4 changed files with 65 additions and 7 deletions

View File

@@ -7,6 +7,7 @@ from .email_notifications import EmailCadence
from common.djangoapps.student.roles import CourseInstructorRole, CourseStaffRole
from .utils import find_app_in_normalized_apps, find_pref_in_normalized_prefs
from ..django_comment_common.models import FORUM_ROLE_ADMINISTRATOR, FORUM_ROLE_MODERATOR, FORUM_ROLE_COMMUNITY_TA
from .notification_content import get_notification_type_content_function
FILTER_AUDIT_EXPIRED_USERS_WITH_NO_ROLE = 'filter_audit_expired_users_with_no_role'
@@ -109,8 +110,9 @@ COURSE_NOTIFICATION_TYPES = {
'is_core': True,
'info': '',
'non_editable': [],
'content_template': _('<{p}><{strong}>{replier_name}</{strong}> commented on {author_name}\'s response in '
'a post youre following <{strong}>{post_title}</{strong}></{p}>'),
'content_template': _('<{p}><{strong}>{replier_name}</{strong}> commented on <{strong}>{author_name}'
'</{strong}> response in a post youre following <{strong}>{post_title}'
'</{strong}></{p}>'),
'content_context': {
'post_title': 'Post title',
'author_name': 'author name',
@@ -465,10 +467,13 @@ def get_notification_content(notification_type, context):
'strong': 'strong',
'p': 'p',
}
content_function = get_notification_type_content_function(notification_type)
if notification_type == 'course_update':
notification_type = 'course_updates'
notification_type = NotificationTypeManager().notification_types.get(notification_type, None)
if notification_type:
if content_function:
return content_function(notification_type, context)
notification_type_content_template = notification_type.get('content_template', None)
if notification_type_content_template:
return notification_type_content_template.format(**context, **html_tags_context)

View File

@@ -0,0 +1,35 @@
"""
Helper functions for overriding notification content for given notification type.
"""
def get_notification_type_content_function(notification_type):
"""
Returns the content function for the given notification if it exists.
"""
try:
return globals()[f"get_{notification_type}_notification_content"]
except KeyError:
return None
def get_notification_content_with_author_pronoun(notification_type, context):
"""
Helper function to get notification content with author's pronoun.
"""
html_tags_context = {
'strong': 'strong',
'p': 'p',
}
notification_type_content_template = notification_type.get('content_template', None)
if 'author_pronoun' in context:
context['author_name'] = context['author_pronoun']
if notification_type_content_template:
return notification_type_content_template.format(**context, **html_tags_context)
return ''
# Returns notification content for the new_comment notification.
get_new_comment_notification_content = get_notification_content_with_author_pronoun
# Returns notification content for the comment_on_followed_post notification.
get_comment_on_followed_post_notification_content = get_notification_content_with_author_pronoun