From 63e940d65dbcd0191aa3d2865de6420d0f2b2c33 Mon Sep 17 00:00:00 2001 From: Saad Yousaf Date: Tue, 21 May 2024 15:39:16 +0500 Subject: [PATCH] fix: fix and optimize search for Course Notification Preference model on django admin --- .../core/djangoapps/notifications/admin.py | 36 ++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) 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)