|
| 1 | +from django.contrib import admin |
| 2 | + |
| 3 | +from courses.models import Course, CourseLesson, CourseModule, CourseTask, CourseTaskOption |
| 4 | + |
| 5 | +from .forms import CourseAdminForm, CourseModuleAdminForm, CourseTaskAdminForm |
| 6 | +from .helpers import UserFileUploadAdminMixin |
| 7 | +from .inlines import ( |
| 8 | + CourseLessonInline, |
| 9 | + CourseModuleInline, |
| 10 | + CourseTaskOptionInline, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +@admin.register(Course) |
| 15 | +class CourseAdmin(UserFileUploadAdminMixin, admin.ModelAdmin): |
| 16 | + form = CourseAdminForm |
| 17 | + list_display = ( |
| 18 | + "id", |
| 19 | + "title", |
| 20 | + "access_type", |
| 21 | + "status", |
| 22 | + "is_completed", |
| 23 | + "start_date", |
| 24 | + "end_date", |
| 25 | + "partner_program", |
| 26 | + "datetime_created", |
| 27 | + ) |
| 28 | + list_display_links = ("id", "title") |
| 29 | + list_filter = ("access_type", "status", "is_completed") |
| 30 | + search_fields = ("id", "title", "partner_program__name") |
| 31 | + raw_id_fields = ("partner_program",) |
| 32 | + readonly_fields = ("completed_at", "datetime_created", "datetime_updated") |
| 33 | + list_select_related = ("partner_program",) |
| 34 | + inlines = [CourseModuleInline] |
| 35 | + fieldsets = ( |
| 36 | + ( |
| 37 | + None, |
| 38 | + { |
| 39 | + "fields": ( |
| 40 | + "title", |
| 41 | + "description", |
| 42 | + "access_type", |
| 43 | + "partner_program", |
| 44 | + "status", |
| 45 | + "is_completed", |
| 46 | + ) |
| 47 | + }, |
| 48 | + ), |
| 49 | + ( |
| 50 | + "Период", |
| 51 | + {"fields": ("start_date", "end_date", "completed_at")}, |
| 52 | + ), |
| 53 | + ( |
| 54 | + "Файлы", |
| 55 | + { |
| 56 | + "fields": ( |
| 57 | + "avatar_file", |
| 58 | + "avatar_upload", |
| 59 | + "card_cover_file", |
| 60 | + "card_cover_upload", |
| 61 | + "header_cover_file", |
| 62 | + "header_cover_upload", |
| 63 | + ) |
| 64 | + }, |
| 65 | + ), |
| 66 | + ( |
| 67 | + "Системные поля", |
| 68 | + {"fields": ("datetime_created", "datetime_updated")}, |
| 69 | + ), |
| 70 | + ) |
| 71 | + |
| 72 | + def save_model(self, request, obj, form, change): |
| 73 | + avatar_upload = form.cleaned_data.get("avatar_upload") |
| 74 | + if avatar_upload: |
| 75 | + obj.avatar_file = self.create_user_file(request, avatar_upload) |
| 76 | + |
| 77 | + card_cover_upload = form.cleaned_data.get("card_cover_upload") |
| 78 | + if card_cover_upload: |
| 79 | + obj.card_cover_file = self.create_user_file(request, card_cover_upload) |
| 80 | + |
| 81 | + header_cover_upload = form.cleaned_data.get("header_cover_upload") |
| 82 | + if header_cover_upload: |
| 83 | + obj.header_cover_file = self.create_user_file(request, header_cover_upload) |
| 84 | + |
| 85 | + super().save_model(request, obj, form, change) |
| 86 | + |
| 87 | + |
| 88 | +@admin.register(CourseModule) |
| 89 | +class CourseModuleAdmin(UserFileUploadAdminMixin, admin.ModelAdmin): |
| 90 | + form = CourseModuleAdminForm |
| 91 | + list_display = ( |
| 92 | + "id", |
| 93 | + "title", |
| 94 | + "course", |
| 95 | + "status", |
| 96 | + "start_date", |
| 97 | + "order", |
| 98 | + "datetime_created", |
| 99 | + ) |
| 100 | + list_display_links = ("id", "title") |
| 101 | + list_filter = ("status", "course") |
| 102 | + search_fields = ("id", "title", "course__title") |
| 103 | + raw_id_fields = ("course",) |
| 104 | + readonly_fields = ("datetime_created", "datetime_updated") |
| 105 | + list_select_related = ("course",) |
| 106 | + inlines = [CourseLessonInline] |
| 107 | + fieldsets = ( |
| 108 | + ( |
| 109 | + None, |
| 110 | + { |
| 111 | + "fields": ( |
| 112 | + "course", |
| 113 | + "title", |
| 114 | + "start_date", |
| 115 | + "status", |
| 116 | + "order", |
| 117 | + ) |
| 118 | + }, |
| 119 | + ), |
| 120 | + ( |
| 121 | + "Файлы", |
| 122 | + {"fields": ("avatar_file", "avatar_upload")}, |
| 123 | + ), |
| 124 | + ( |
| 125 | + "Системные поля", |
| 126 | + {"fields": ("datetime_created", "datetime_updated")}, |
| 127 | + ), |
| 128 | + ) |
| 129 | + |
| 130 | + def save_model(self, request, obj, form, change): |
| 131 | + avatar_upload = form.cleaned_data.get("avatar_upload") |
| 132 | + if avatar_upload: |
| 133 | + obj.avatar_file = self.create_user_file(request, avatar_upload) |
| 134 | + super().save_model(request, obj, form, change) |
| 135 | + |
| 136 | + |
| 137 | +@admin.register(CourseLesson) |
| 138 | +class CourseLessonAdmin(admin.ModelAdmin): |
| 139 | + list_display = ( |
| 140 | + "id", |
| 141 | + "title", |
| 142 | + "module", |
| 143 | + "get_course", |
| 144 | + "status", |
| 145 | + "order", |
| 146 | + "datetime_created", |
| 147 | + ) |
| 148 | + list_display_links = ("id", "title") |
| 149 | + list_filter = ("status", "module__course") |
| 150 | + search_fields = ("id", "title", "module__title", "module__course__title") |
| 151 | + raw_id_fields = ("module",) |
| 152 | + readonly_fields = ("datetime_created", "datetime_updated") |
| 153 | + list_select_related = ("module", "module__course") |
| 154 | + |
| 155 | + @admin.display(description="Курс", ordering="module__course__title") |
| 156 | + def get_course(self, obj): |
| 157 | + return obj.module.course |
| 158 | + |
| 159 | + |
| 160 | +@admin.register(CourseTask) |
| 161 | +class CourseTaskAdmin(UserFileUploadAdminMixin, admin.ModelAdmin): |
| 162 | + form = CourseTaskAdminForm |
| 163 | + list_display = ( |
| 164 | + "id", |
| 165 | + "title", |
| 166 | + "lesson", |
| 167 | + "get_module", |
| 168 | + "get_course", |
| 169 | + "task_kind", |
| 170 | + "status", |
| 171 | + "question_type", |
| 172 | + "answer_type", |
| 173 | + "order", |
| 174 | + ) |
| 175 | + list_display_links = ("id", "title") |
| 176 | + list_filter = ( |
| 177 | + "status", |
| 178 | + "task_kind", |
| 179 | + "check_type", |
| 180 | + "question_type", |
| 181 | + "answer_type", |
| 182 | + "lesson__module__course", |
| 183 | + ) |
| 184 | + search_fields = ( |
| 185 | + "id", |
| 186 | + "title", |
| 187 | + "lesson__title", |
| 188 | + "lesson__module__title", |
| 189 | + "lesson__module__course__title", |
| 190 | + ) |
| 191 | + raw_id_fields = ("lesson",) |
| 192 | + readonly_fields = ("datetime_created", "datetime_updated") |
| 193 | + list_select_related = ("lesson", "lesson__module", "lesson__module__course") |
| 194 | + inlines = [CourseTaskOptionInline] |
| 195 | + fieldsets = ( |
| 196 | + ( |
| 197 | + None, |
| 198 | + { |
| 199 | + "fields": ( |
| 200 | + "lesson", |
| 201 | + "title", |
| 202 | + "status", |
| 203 | + "task_kind", |
| 204 | + "order", |
| 205 | + ) |
| 206 | + }, |
| 207 | + ), |
| 208 | + ( |
| 209 | + "Типы задания", |
| 210 | + { |
| 211 | + "fields": ( |
| 212 | + "check_type", |
| 213 | + "informational_type", |
| 214 | + "question_type", |
| 215 | + "answer_type", |
| 216 | + ) |
| 217 | + }, |
| 218 | + ), |
| 219 | + ( |
| 220 | + "Контент", |
| 221 | + { |
| 222 | + "fields": ( |
| 223 | + "body_text", |
| 224 | + "answer_title", |
| 225 | + "video_url", |
| 226 | + "image_file", |
| 227 | + "image_upload", |
| 228 | + "attachment_file", |
| 229 | + "attachment_upload", |
| 230 | + ) |
| 231 | + }, |
| 232 | + ), |
| 233 | + ( |
| 234 | + "Системные поля", |
| 235 | + {"fields": ("datetime_created", "datetime_updated")}, |
| 236 | + ), |
| 237 | + ) |
| 238 | + |
| 239 | + def save_model(self, request, obj, form, change): |
| 240 | + image_upload = form.cleaned_data.get("image_upload") |
| 241 | + if image_upload: |
| 242 | + obj.image_file = self.create_user_file(request, image_upload) |
| 243 | + |
| 244 | + attachment_upload = form.cleaned_data.get("attachment_upload") |
| 245 | + if attachment_upload: |
| 246 | + obj.attachment_file = self.create_user_file(request, attachment_upload) |
| 247 | + |
| 248 | + super().save_model(request, obj, form, change) |
| 249 | + |
| 250 | + @admin.display(description="Модуль", ordering="lesson__module__title") |
| 251 | + def get_module(self, obj): |
| 252 | + return obj.lesson.module |
| 253 | + |
| 254 | + @admin.display(description="Курс", ordering="lesson__module__course__title") |
| 255 | + def get_course(self, obj): |
| 256 | + return obj.lesson.module.course |
| 257 | + |
| 258 | + |
| 259 | +@admin.register(CourseTaskOption) |
| 260 | +class CourseTaskOptionAdmin(admin.ModelAdmin): |
| 261 | + list_display = ("id", "task", "text", "is_correct", "order", "datetime_created") |
| 262 | + list_display_links = ("id", "text") |
| 263 | + list_filter = ("is_correct", "task__answer_type") |
| 264 | + search_fields = ("id", "text", "task__title") |
| 265 | + raw_id_fields = ("task",) |
| 266 | + readonly_fields = ("datetime_created", "datetime_updated") |
| 267 | + list_select_related = ("task",) |
0 commit comments