From b33256c63e9a267ca249efdd8127ce56ea5843a2 Mon Sep 17 00:00:00 2001 From: Server <10.9.8.204@example.com> Date: Sun, 30 Aug 2026 14:21:42 +0000 Subject: [PATCH] show schedule context by target type --- models/schedule.py | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-) diff --git a/models/schedule.py b/models/schedule.py index 761b349..636cc2f 100644 --- a/models/schedule.py +++ b/models/schedule.py @@ -184,14 +184,20 @@ def _minutes_to_time(value: int | None) -> list[int]: class Lesson: - def __init__(self, data: dict[str, Any], subgroup_names: list[str]) -> None: + def __init__( + self, + data: dict[str, Any], + target_kind: TargetKind, + group_names: list[str], + ) -> None: self.start = _minutes_to_time(data.get("time_start")) self.end = _minutes_to_time(data.get("time_end")) self.name = str(data.get("name") or "Без названия") self.type = str(data.get("type") or "пара") self.teacher_ids = [str(value) for value in data.get("teacher_ids", [])] self.room_ids = [str(value) for value in data.get("room_ids", [])] - self.subgroup_names = subgroup_names + self.target_kind = target_kind + self.group_names = group_names self.info = self.teacher_ids + [f"ауд. {room}" for room in self.room_ids] self.is_odd_week = bool(data.get("is_odd_week")) self.is_even_week = bool(data.get("is_even_week")) @@ -211,13 +217,19 @@ class Lesson: minutes = int((lesson_end - now).total_seconds() // 60) date_text = f"Сейчас идет {lesson_type}, закончится через {max(minutes, 1)} мин." - details = [html.escape(teacher) for teacher in self.teacher_ids] - details.extend( + teachers = [html.escape(teacher) for teacher in self.teacher_ids] + rooms = [ f'ауд. {html.escape(room)}' for room in self.room_ids - ) - if self.subgroup_names: - details.append(html.escape(", ".join(self.subgroup_names))) + ] + groups = [f"гр. {html.escape(group)}" for group in self.group_names] + + if self.target_kind == "teacher": + details = rooms + groups + elif self.target_kind == "room": + details = groups + teachers + else: + details = teachers + rooms + groups info = ", ".join(details) if details else "Дополнительная информация отсутствует" return f"
{html.escape(self.name)}\n{date_text}\n{info}" @@ -236,6 +248,10 @@ class Schedule: self.weekDay = current.weekday() self.days: list[list[Lesson]] = [[] for _ in range(5)] + groups_by_id = { + str(group["id"]): str(group["name"]) + for group in groups + } target_ids = {target["id"]} child_names: dict[str, str] = {} field = {"group": "group_ids", "room": "room_ids", "teacher": "teacher_ids"}[target["kind"]] @@ -254,8 +270,16 @@ class Schedule: matched_ids = target_ids.intersection(map(str, raw_lesson.get(field, []))) if not matched_ids: continue - subgroup_names = sorted(child_names[item] for item in matched_ids if item in child_names) - self.days[weekday - 1].append(Lesson(raw_lesson, subgroup_names)) + if target["kind"] == "group": + group_names = sorted(child_names[item] for item in matched_ids if item in child_names) + else: + group_names = sorted( + { + groups_by_id.get(str(group_id), str(group_id)) + for group_id in raw_lesson.get("group_ids", []) + } + ) + self.days[weekday - 1].append(Lesson(raw_lesson, target["kind"], group_names)) for day in self.days: day.sort(key=lambda lesson: (lesson.start, lesson.name.casefold()))