Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions openedx/core/djangoapps/notifications/base_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
'is_core': True,
'content_template': _('<{p}><{strong}>{replier_name}</{strong}> commented on <{strong}>{author_name}'
'</{strong}> response to your post <{strong}>{post_title}</{strong}></{p}>'),
'grouped_content_template': _('<{p}><{strong}>{replier_name}</{strong}> commented on <{strong}>{author_name}'
'</{strong}> response to your post <{strong}>{post_title}</{strong}></{p}>'),
'content_context': {
'post_title': 'Post title',
'author_name': 'author name',
Expand Down
21 changes: 0 additions & 21 deletions openedx/core/djangoapps/notifications/grouping_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,27 +63,6 @@ def get_grouper(cls, notification_type: str) -> Union[BaseNotificationGrouper, N
return grouper_class()


@NotificationRegistry.register('new_comment')
class NewCommentGrouper(BaseNotificationGrouper):
"""
Groups new comment notifications based on the replier name.
"""

def group(self, new_notification, old_notification):
"""
Groups new comment notifications based on the replier name.
"""
context = old_notification.content_context.copy()
if not context.get('grouped'):
context['replier_name_list'] = [context['replier_name']]
context['grouped_count'] = 1
context['grouped'] = True
context['replier_name_list'].append(new_notification.content_context['replier_name'])
context['grouped_count'] += 1
context['email_content'] = new_notification.content_context.get('email_content', '')
return context


@NotificationRegistry.register('new_discussion_post')
class NewPostGrouper(BaseNotificationGrouper):
"""
Expand Down
11 changes: 1 addition & 10 deletions openedx/core/djangoapps/notifications/notification_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,7 @@ def get_notification_context_with_author_pronoun(context: Dict) -> Dict:


# Returns notification content for the new_comment notification.
def get_new_comment_notification_context(context):
"""
Returns the context for the new_comment notification
"""
if not context.get('grouped'):
return get_notification_context_with_author_pronoun(context)
num_repliers = context['grouped_count']
repliers_string = f"{num_repliers - 1} other{'s' if num_repliers > 2 else ''}"
context['replier_name'] = f"{context['replier_name_list'][0]} and {repliers_string}"
return context
get_new_comment_notification_context = get_notification_context_with_author_pronoun


# Returns notification content for the comment_on_followed_post notification.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from openedx.core.djangoapps.notifications.grouping_notifications import (
BaseNotificationGrouper,
NotificationRegistry,
NewCommentGrouper,
group_user_notifications,
get_user_existing_notifications, NewPostGrouper
)
Expand Down Expand Up @@ -47,65 +46,6 @@ def test_get_grouper_returns_none_for_unregistered_type(self):
self.assertIsNone(grouper)


class TestNewCommentGrouper(unittest.TestCase):
"""
Tests for the NewCommentGrouper class
"""

def setUp(self):
"""
Set up the test
"""
self.new_notification = MagicMock(spec=Notification)
self.old_notification = MagicMock(spec=Notification)
self.old_notification.content_context = {
'replier_name': 'User1'
}

def test_group_creates_grouping_keys(self):
"""
Test that the function creates the grouping keys
"""
updated_context = NewCommentGrouper().group(self.new_notification, self.old_notification)

self.assertIn('replier_name_list', updated_context)
self.assertIn('grouped_count', updated_context)
self.assertEqual(updated_context['grouped_count'], 2)
self.assertTrue(updated_context['grouped'])

def test_group_appends_to_existing_grouping(self):
"""
Test that the function appends to the existing grouping
"""
# Mock a pre-grouped notification
self.old_notification.content_context = {
'replier_name': 'User1',
'replier_name_list': ['User1', 'User2'],
'grouped': True,
'grouped_count': 2
}
self.new_notification.content_context = {'replier_name': 'User3'}

updated_context = NewCommentGrouper().group(self.new_notification, self.old_notification)

self.assertIn('replier_name_list', updated_context)
self.assertEqual(len(updated_context['replier_name_list']), 3)
self.assertEqual(updated_context['grouped_count'], 3)

def test_group_email_content(self):
"""
Tests email_content in content_context when grouping notification
"""
self.old_notification.content_context['email_content'] = 'old content'
self.new_notification.content_context = {
'email_content': 'new content',
'replier_name': 'user_2',
}
content_context = NewCommentGrouper().group(self.new_notification, self.old_notification)
self.assertIn('email_content', content_context)
self.assertEqual(content_context['email_content'], 'new content')


class TestNewPostGrouper(unittest.TestCase):
"""
Tests for the NewPostGrouper class
Expand Down Expand Up @@ -159,7 +99,7 @@ def test_group_user_notifications(self, mock_get_grouper):
Test that the function groups notifications using the appropriate grou
"""
# Mock the grouper
mock_grouper = MagicMock(spec=NewCommentGrouper)
mock_grouper = MagicMock(spec=NewPostGrouper)
mock_get_grouper.return_value = mock_grouper

new_notification = MagicMock(spec=Notification)
Expand Down Expand Up @@ -237,7 +177,7 @@ def test_get_user_existing_notifications(self, mock_filter):
mock_filter.return_value = [mock_notification1, mock_notification2]

user_ids = [1, 2]
notification_type = 'new_comment'
notification_type = 'new_discussion_post'
group_by_id = 'group_id_1'
course_id = 'course_1'

Expand Down
16 changes: 10 additions & 6 deletions openedx/core/djangoapps/notifications/tests/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,27 +196,31 @@ def test_send_notification_with_grouping_enabled(self):
"""
Test send_notifications with grouping enabled.
"""
(
self.preference_v1.notification_preference_config['discussion']
['notification_types']['new_discussion_post']['web']
) = True
self.preference_v1.save()
with patch('openedx.core.djangoapps.notifications.tasks.group_user_notifications') as user_notifications_mock:
context = {
'post_title': 'Post title',
'author_name': 'author name',
'replier_name': 'replier name',
'group_by_id': 'group_by_id',
'post_title': 'Test Post',
'username': 'Test Author',
'group_by_id': 'group_by_id'
}
content_url = 'https://example.com/'
send_notifications(
[self.user.id],
str(self.course_1.id),
'discussion',
'new_comment',
'new_discussion_post',
{**context},
content_url
)
send_notifications(
[self.user.id],
str(self.course_1.id),
'discussion',
'new_comment',
'new_discussion_post',
{**context},
content_url
)
Expand Down
Loading