|
| 1 | +""" |
| 2 | +Django Admin for Notifications |
| 3 | +""" |
| 4 | + |
| 5 | +from django.contrib import admin |
| 6 | +from django.utils.translation import gettext_lazy as _ |
| 7 | + |
| 8 | +from .base_notification import COURSE_NOTIFICATION_APPS, COURSE_NOTIFICATION_TYPES |
| 9 | +from .models import CourseNotificationPreference, Notification |
| 10 | + |
| 11 | + |
| 12 | +class NotificationAppNameListFilter(admin.SimpleListFilter): |
| 13 | + """ |
| 14 | + Shows list filter in django admin of notification apps |
| 15 | + """ |
| 16 | + title = _("Notification App") |
| 17 | + parameter_name = "app_name" |
| 18 | + |
| 19 | + def lookups(self, request, model_admin): |
| 20 | + lookup_list = [ |
| 21 | + (app_name, app_name) |
| 22 | + for app_name in COURSE_NOTIFICATION_APPS.keys() |
| 23 | + ] |
| 24 | + return lookup_list |
| 25 | + |
| 26 | + def queryset(self, request, queryset): |
| 27 | + app_name = self.value() |
| 28 | + if app_name not in COURSE_NOTIFICATION_APPS.keys(): |
| 29 | + return queryset |
| 30 | + return queryset.filter(app_name=app_name) |
| 31 | + |
| 32 | + |
| 33 | +class NotificationTypeListFilter(admin.SimpleListFilter): |
| 34 | + """ |
| 35 | + Shows list filter in django admin of notification types |
| 36 | + """ |
| 37 | + title = _("Notification Type") |
| 38 | + parameter_name = "notification_type" |
| 39 | + |
| 40 | + def lookups(self, request, model_admin): |
| 41 | + lookup_list = [ |
| 42 | + (notification_type, notification_type) |
| 43 | + for notification_type in COURSE_NOTIFICATION_TYPES.keys() |
| 44 | + ] |
| 45 | + return lookup_list |
| 46 | + |
| 47 | + def queryset(self, request, queryset): |
| 48 | + notification_type = self.value() |
| 49 | + if notification_type not in COURSE_NOTIFICATION_TYPES.keys(): |
| 50 | + return queryset |
| 51 | + return queryset.filter(notification_type=notification_type) |
| 52 | + |
| 53 | + |
| 54 | +class NotificationAdmin(admin.ModelAdmin): |
| 55 | + """ |
| 56 | + Admin for Notifications |
| 57 | + """ |
| 58 | + raw_id_fields = ('user',) |
| 59 | + search_fields = ('course_id', 'app_name', 'notification_type', 'user__username') |
| 60 | + list_filter = (NotificationAppNameListFilter, NotificationTypeListFilter) |
| 61 | + |
| 62 | + |
| 63 | +class CourseNotificationPreferenceAdmin(admin.ModelAdmin): |
| 64 | + """ |
| 65 | + Admin for Course Notification Preferences |
| 66 | + """ |
| 67 | + model = CourseNotificationPreference |
| 68 | + raw_id_fields = ('user',) |
| 69 | + list_display = ('get_username', 'course_id') |
| 70 | + search_fields = ('course_id', 'user__username') |
| 71 | + search_help_text = _('Search by username, course_id. ' |
| 72 | + 'Specify fields with username: or course_id: prefixes. ' |
| 73 | + 'If no prefix is specified, search will be done on username. \n' |
| 74 | + 'Examples: \n' |
| 75 | + ' - testuser (default username search) \n' |
| 76 | + ' - username:testuser (username keyword search) \n' |
| 77 | + ' - course_id:course-v1:edX+DemoX+Demo_Course (course_id keyword search) \n' |
| 78 | + ' - username:testuser, course_id:course-v1:edX+DemoX+Demo_Course (combined keyword search) \n' |
| 79 | + ) |
| 80 | + |
| 81 | + @admin.display(description='Username', ordering='user__username') |
| 82 | + def get_username(self, obj): |
| 83 | + return obj.user.username |
| 84 | + |
| 85 | + def get_queryset(self, request): |
| 86 | + queryset = super().get_queryset(request) |
| 87 | + return queryset.select_related("user").only("id", "user__username", "course_id") |
| 88 | + |
| 89 | + def get_search_results(self, request, queryset, search_term): |
| 90 | + """ |
| 91 | + Custom search for CourseNotificationPreference model |
| 92 | + """ |
| 93 | + if search_term: |
| 94 | + criteria = search_term.split(',') |
| 95 | + |
| 96 | + for criterion in criteria: |
| 97 | + criterion = criterion.strip() |
| 98 | + if criterion.startswith('username:'): |
| 99 | + queryset = queryset.filter(user__username=criterion.split(':')[1]) |
| 100 | + |
| 101 | + elif criterion.startswith('course_id:'): |
| 102 | + criteria = criterion.split(':') |
| 103 | + course_id = ':'.join(criteria[1:]).strip() |
| 104 | + queryset = queryset.filter(course_id=course_id) |
| 105 | + |
| 106 | + else: |
| 107 | + queryset = queryset.filter(user__username=search_term) |
| 108 | + |
| 109 | + else: |
| 110 | + queryset = queryset.all() |
| 111 | + |
| 112 | + return queryset, True |
| 113 | + |
| 114 | + |
| 115 | +admin.site.register(Notification, NotificationAdmin) |
| 116 | +admin.site.register(CourseNotificationPreference, CourseNotificationPreferenceAdmin) |
0 commit comments