diff --git a/openedx/core/djangoapps/notifications/admin.py b/openedx/core/djangoapps/notifications/admin.py index 52a758b97b03..82524e4b08b2 100644 --- a/openedx/core/djangoapps/notifications/admin.py +++ b/openedx/core/djangoapps/notifications/admin.py @@ -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)