Skip to content

Commit

Permalink
fix: fix and optimize search for Course Notification Preference model…
Browse files Browse the repository at this point in the history
… on django admin
  • Loading branch information
Saad Yousaf authored and Saad Yousaf committed May 21, 2024
1 parent e95d7e7 commit df511bd
Showing 1 changed file with 35 additions and 1 deletion.
36 changes: 35 additions & 1 deletion openedx/core/djangoapps/notifications/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,47 @@ class CourseNotificationPreferenceAdmin(admin.ModelAdmin):
"""
model = CourseNotificationPreference
raw_id_fields = ('user',)
list_display = ('get_username', 'course_id', 'notification_preference_config')
list_display = ('get_username', 'course_id')
search_fields = ('course_id', 'user__username')
search_help_text = _('Search by username, course_id. '
'Specify fields with username: or course_id: prefixes. '
'If no prefix is specified, search will be done on username. \n'
'Examples: \n'
' - testuser (default username search) \n'
' - username:testuser (username keyword search) \n'
' - course_id:course-v1:edX+DemoX+Demo_Course (course_id keyword search) \n'
' - username:testuser, course_id:course-v1:edX+DemoX+Demo_Course (combined keyword search) \n'
)

@admin.display(description='Username', ordering='user__username')
def get_username(self, obj):
return obj.user.username

def get_search_results(self, request, queryset, search_term):
"""
Custom search for CourseNotificationPreference model
"""
if search_term:
criteria = search_term.split(',')

for criterion in criteria:
criterion = criterion.strip()
if criterion.startswith('username:'):
queryset = queryset.filter(user__username=criterion.split(':')[1])

elif criterion.startswith('course_id:'):
criteria = criterion.split(':')
course_id = ':'.join(criteria[1:]).strip()
queryset = queryset.filter(course_id=course_id)

else:
queryset = queryset.filter(user__username=search_term)

else:
queryset = queryset.all()

return queryset, True


admin.site.register(Notification, NotificationAdmin)
admin.site.register(CourseNotificationPreference, CourseNotificationPreferenceAdmin)

0 comments on commit df511bd

Please sign in to comment.