|
|
|
@ -1,4 +1,5 @@ |
|
|
|
from django.contrib import admin, messages |
|
|
|
from django.db.models import Q |
|
|
|
from django.db.models import Count |
|
|
|
from django.utils.safestring import mark_safe |
|
|
|
from django.urls import reverse |
|
|
|
@ -9,6 +10,8 @@ from unfold.admin import ModelAdmin |
|
|
|
from unfold.decorators import display, action |
|
|
|
|
|
|
|
from apps.course.models import CourseLesson |
|
|
|
from apps.course.models import Course |
|
|
|
from apps.course.models.course import extract_text_from_json |
|
|
|
from apps.quiz.models import Quiz |
|
|
|
from apps.quiz.admin.question import QuestionAdminInline |
|
|
|
from apps.quiz.admin.participant import QuizParticipantInline |
|
|
|
@ -16,7 +19,7 @@ from utils.admin import project_admin_site, admin_url_generator |
|
|
|
|
|
|
|
|
|
|
|
class QuizAdmin(ModelAdmin): |
|
|
|
search_fields = ['title', 'lesson__title'] |
|
|
|
search_fields = ['title', 'description'] |
|
|
|
list_display = ['title', 'description', 'lesson', 'each_question_timing', 'status_display', 'questions_display'] |
|
|
|
list_filter = ['each_question_timing', 'status'] |
|
|
|
inlines = [QuestionAdminInline, QuizParticipantInline] |
|
|
|
@ -42,6 +45,43 @@ class QuizAdmin(ModelAdmin): |
|
|
|
|
|
|
|
return queryset.none() |
|
|
|
|
|
|
|
def _find_matching_course_ids(self, search_term): |
|
|
|
normalized = (search_term or "").strip().lower() |
|
|
|
if not normalized: |
|
|
|
return [] |
|
|
|
|
|
|
|
matching_ids = list( |
|
|
|
Course.objects.filter( |
|
|
|
Q(title__icontains=search_term) | Q(slug__icontains=search_term) |
|
|
|
).values_list("id", flat=True) |
|
|
|
) |
|
|
|
|
|
|
|
if matching_ids: |
|
|
|
return matching_ids |
|
|
|
|
|
|
|
for course in Course.objects.all().only("id", "title", "slug"): |
|
|
|
title_text = extract_text_from_json(course.title).lower() |
|
|
|
slug_text = extract_text_from_json(course.slug).lower() |
|
|
|
if normalized in title_text or normalized in slug_text: |
|
|
|
matching_ids.append(course.id) |
|
|
|
|
|
|
|
return matching_ids |
|
|
|
|
|
|
|
def _find_matching_lesson_ids(self, search_term): |
|
|
|
normalized = (search_term or "").strip().lower() |
|
|
|
if not normalized: |
|
|
|
return [] |
|
|
|
|
|
|
|
matching_ids = [] |
|
|
|
for lesson in CourseLesson.objects.select_related("lesson").all(): |
|
|
|
lesson_title = extract_text_from_json( |
|
|
|
lesson.title or (lesson.lesson.title if lesson.lesson else []) |
|
|
|
).lower() |
|
|
|
if normalized in lesson_title: |
|
|
|
matching_ids.append(lesson.id) |
|
|
|
|
|
|
|
return matching_ids |
|
|
|
|
|
|
|
def get_exclude(self, request, obj=None): |
|
|
|
if not obj: # اگر obj وجود ندارد یعنی صفحه Add است |
|
|
|
return ['course'] |
|
|
|
@ -73,6 +113,19 @@ class QuizAdmin(ModelAdmin): |
|
|
|
|
|
|
|
return form |
|
|
|
|
|
|
|
def get_search_results(self, request, queryset, search_term): |
|
|
|
queryset, use_distinct = super().get_search_results(request, queryset, search_term) |
|
|
|
matching_course_ids = self._find_matching_course_ids(search_term) |
|
|
|
matching_lesson_ids = self._find_matching_lesson_ids(search_term) |
|
|
|
|
|
|
|
if matching_course_ids or matching_lesson_ids: |
|
|
|
queryset = queryset | self.model.objects.filter( |
|
|
|
Q(course_id__in=matching_course_ids) | Q(lesson_id__in=matching_lesson_ids) |
|
|
|
) |
|
|
|
use_distinct = True |
|
|
|
|
|
|
|
return queryset, use_distinct |
|
|
|
|
|
|
|
@display(description=_('Status'), ordering='status') |
|
|
|
def status_display(self, obj): |
|
|
|
if obj.status: |
|
|
|
|