Skip to content

Commit 3931727

Browse files
committed
Добавлен lesson_detail в query-слой модуля courses после рефакторинга
1 parent 99b492c commit 3931727

1 file changed

Lines changed: 109 additions & 0 deletions

File tree

courses/queries/lesson_detail.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
from django.db.models import Prefetch
2+
from django.shortcuts import get_object_or_404
3+
4+
from courses.models import (
5+
CourseContentStatus,
6+
CourseLesson,
7+
CourseLessonContentStatus,
8+
CourseModuleContentStatus,
9+
CourseTask,
10+
CourseTaskAnswerType,
11+
CourseTaskContentStatus,
12+
CourseTaskOption,
13+
)
14+
from courses.services.learning_flow import (
15+
ensure_lesson_access,
16+
first_unfinished_task,
17+
task_completion_map,
18+
)
19+
from courses.services.progress import build_progress_snapshot
20+
21+
22+
def build_lesson_detail_payload(user, pk: int) -> dict:
23+
lesson = get_object_or_404(
24+
CourseLesson.objects.filter(
25+
status=CourseLessonContentStatus.PUBLISHED,
26+
module__status=CourseModuleContentStatus.PUBLISHED,
27+
module__course__status__in=(
28+
CourseContentStatus.PUBLISHED,
29+
CourseContentStatus.COMPLETED,
30+
),
31+
)
32+
.select_related("module", "module__course")
33+
.prefetch_related(
34+
Prefetch(
35+
"tasks",
36+
queryset=CourseTask.objects.filter(
37+
status=CourseTaskContentStatus.PUBLISHED
38+
)
39+
.order_by("order", "id")
40+
.prefetch_related(
41+
Prefetch(
42+
"options",
43+
queryset=CourseTaskOption.objects.order_by("order", "id"),
44+
)
45+
),
46+
),
47+
),
48+
pk=pk,
49+
)
50+
51+
ensure_lesson_access(user, lesson)
52+
tasks = list(lesson.tasks.all())
53+
completion_map = task_completion_map(user, tasks)
54+
current_task = first_unfinished_task(tasks, completion_map)
55+
total_tasks = len(tasks)
56+
completed_tasks = sum(1 for task in tasks if completion_map.get(task.id, False))
57+
snapshot = build_progress_snapshot(completed_tasks, total_tasks)
58+
current_task_id = current_task.id if current_task is not None else None
59+
60+
tasks_payload = []
61+
for task in tasks:
62+
is_completed = completion_map.get(task.id, False)
63+
is_available = is_completed or (
64+
current_task_id is not None and task.id == current_task_id
65+
)
66+
if current_task_id is None:
67+
is_available = True
68+
69+
options_payload = []
70+
if task.answer_type in (
71+
CourseTaskAnswerType.SINGLE_CHOICE,
72+
CourseTaskAnswerType.MULTIPLE_CHOICE,
73+
):
74+
options_payload = [
75+
{"id": option.id, "order": option.order, "text": option.text}
76+
for option in task.options.all()
77+
]
78+
79+
tasks_payload.append(
80+
{
81+
"id": task.id,
82+
"order": task.order,
83+
"title": task.title,
84+
"status": task.status,
85+
"task_kind": task.task_kind,
86+
"check_type": task.check_type,
87+
"informational_type": task.informational_type,
88+
"question_type": task.question_type,
89+
"answer_type": task.answer_type,
90+
"body_text": task.body_text,
91+
"video_url": task.video_url,
92+
"image_url": task.image_file_id,
93+
"attachment_url": task.attachment_file_id,
94+
"is_available": is_available,
95+
"is_completed": is_completed,
96+
"options": options_payload,
97+
}
98+
)
99+
100+
return {
101+
"id": lesson.id,
102+
"module_id": lesson.module_id,
103+
"course_id": lesson.module.course_id,
104+
"title": lesson.title,
105+
"progress_status": snapshot.status,
106+
"percent": snapshot.percent,
107+
"current_task_id": current_task_id,
108+
"tasks": tasks_payload,
109+
}

0 commit comments

Comments
 (0)