Skip to content

Commit

Permalink
refactor: refactored code and fixed order in API
Browse files Browse the repository at this point in the history
  • Loading branch information
muhammadadeeltajamul committed Sep 23, 2024
1 parent 6f81e67 commit 9236459
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 50 deletions.
42 changes: 0 additions & 42 deletions openedx/core/djangoapps/notifications/base_notification.py
Original file line number Diff line number Diff line change
Expand Up @@ -459,45 +459,6 @@ def get_notification_app_preferences(self):
return course_notification_preference_config


def get_grouped_template_context(context):
"""
Generates a grouped template context if 'grouped' is True in the context.
Args:
context (dict): A dictionary containing template data, including 'grouped',
'user_key', 'group_key', and 'grouped_count'.
Returns:
dict: Formatted template with the grouped context or an empty string if 'grouped' is False.
"""
# Ensure 'grouped' is in context and True, otherwise return an empty string
if not context.get('grouped'):
return context

# Retrieve keys from the context with error handling for missing keys
user_key = context['user_key']
group_key = context['group_key']
user_list = context.get(group_key, [])
grouped_count = context.get('grouped_count', 0)

# Validate that user_list and grouped_count are consistent
if not user_list or grouped_count <= 0:
raise ValueError("Invalid 'grouped_count' or empty 'user_list'.")

# Format the user display text based on the number of users in the group
if grouped_count == 2:
text = " and ".join(user_list)
else:
text = f"{user_list[0]} and {grouped_count - 1} others"

# Update the context with the new user text
new_context = context.copy()
new_context[user_key] = text

# Format and return the template with the new context
return new_context


def get_notification_content(notification_type, context):
"""
Returns notification content for the given notification type with provided context.
Expand Down Expand Up @@ -539,9 +500,6 @@ def get_notification_content(notification_type, context):

if template:
# Handle grouped templates differently by modifying the context using a different function.
if is_grouped:
context = get_grouped_template_context(context)

return template.format(**context)

return ''
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,11 @@ def group(self, new_notification, old_notification):
Groups new comment notifications based on the replier name.
"""
context = old_notification.content_context.copy()
user_key = 'replier_name'
group_key = f'{user_key}_grouped'
if not context.get('grouped'):
context['user_key'] = user_key
context['group_key'] = group_key
context[group_key] = [context[user_key]]
context['replier_name_list'] = [context['replier_name']]
context['grouped_count'] = 1
context['grouped'] = True
context[group_key].append(new_notification.content_context['replier_name'])
context['replier_name_list'].append(new_notification.content_context['replier_name'])
context['grouped_count'] += 1
return context

Expand Down
10 changes: 9 additions & 1 deletion openedx/core/djangoapps/notifications/notification_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ def get_notification_context_with_author_pronoun(context: Dict) -> Dict:


# Returns notification content for the new_comment notification.
get_new_comment_notification_context = get_notification_context_with_author_pronoun
def get_new_comment_notification_context(context):
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


# Returns notification content for the comment_on_followed_post notification.
get_comment_on_followed_post_notification_context = get_notification_context_with_author_pronoun
2 changes: 1 addition & 1 deletion openedx/core/djangoapps/notifications/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ def get_queryset(self):

if app_name:
params['app_name'] = app_name
return Notification.objects.filter(**params).order_by('-id')
return Notification.objects.filter(**params).order_by('-created')


@allow_any_authenticated_user()
Expand Down

0 comments on commit 9236459

Please sign in to comment.