From 97598f9b377d475114fb5561fee90d2d6b3cf488 Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Tue, 17 Sep 2024 08:19:20 +0200
Subject: [PATCH] WIP: Show course name in timetable overview

---
 .../event_bar/LessonEventBar.vue              |  6 ++++
 aleksis/apps/chronos/models.py                | 31 +++++++++++++++++++
 aleksis/apps/chronos/preferences.py           |  8 +++++
 .../chronos/lesson_event_description.txt      |  3 +-
 4 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/aleksis/apps/chronos/frontend/components/calendar_feeds/event_bar/LessonEventBar.vue b/aleksis/apps/chronos/frontend/components/calendar_feeds/event_bar/LessonEventBar.vue
index b63154d6..252e781d 100644
--- a/aleksis/apps/chronos/frontend/components/calendar_feeds/event_bar/LessonEventBar.vue
+++ b/aleksis/apps/chronos/frontend/components/calendar_feeds/event_bar/LessonEventBar.vue
@@ -40,6 +40,12 @@
               attr="short_name"
               class="mr-1"
             />
+            <lesson-event-link-iterator
+              v-if="selectedEvent.meta.courses"
+              :items="selectedEvent.meta.courses"
+              attr="name"
+              class="mr-1"
+            />
             <lesson-event-old-new
               v-if="!selectedEvent.meta.is_teacher || newTeachers.length > 0"
               :new-items="newTeachers"
diff --git a/aleksis/apps/chronos/models.py b/aleksis/apps/chronos/models.py
index 3189999f..4cf0cc50 100644
--- a/aleksis/apps/chronos/models.py
+++ b/aleksis/apps/chronos/models.py
@@ -1429,6 +1429,20 @@ class LessonEvent(CalendarEvent):
             return my_subject
         return _("Lesson")
 
+    @property
+    def course_name_with_amends(self: LessonEvent) -> str:
+        """Get formatted course name (including amends)."""
+        my_course = self.course.name if self.course else ""
+        amended_course = self.amends.course.name if self.amends and self.amends.course else ""
+
+        if my_course and amended_course:
+            return _("{} (instead of {})").format(my_course, amended_course)
+        elif not my_course and amended_course:
+            return amended_course
+        elif my_course:
+            return my_course
+        return _("Course")
+
     @classmethod
     def value_title(cls, reference_object: LessonEvent, request: HttpRequest | None = None) -> str:
         """Get the title of the lesson event."""
@@ -1438,6 +1452,12 @@ class LessonEvent(CalendarEvent):
             reference_object.amends and reference_object.amends.subject
         ):
             title = reference_object.subject_name_with_amends
+            if (
+                get_site_preferences()["chronos__show_course_name"]
+                and reference_object.course
+                or (reference_object.amends and reference_object.amends.course)
+            ):
+                title += " · " + reference_object.course_name_with_amends
             if request and request.user.person in reference_object.teachers.all():
                 title += " · " + reference_object.group_names
             elif request:
@@ -1545,6 +1565,17 @@ class LessonEvent(CalendarEvent):
             else None,
             "comment": reference_object.comment,
             "cancelled": reference_object.cancelled,
+            "course": {
+                "id": reference_object.course.pk,
+                "name": reference_object.course.name,
+                "subject": {
+                    "id": reference_object.course.subject.pk,
+                    "name": reference_object.course.subject.name,
+                    "short_name": reference_object.course.subject.short_name,
+                },
+            }
+            if reference_object.course and get_site_preferences()["chronos__show_course_name"]
+            else None,
         }
 
     @classmethod
diff --git a/aleksis/apps/chronos/preferences.py b/aleksis/apps/chronos/preferences.py
index b3dc8362..156aaa5f 100644
--- a/aleksis/apps/chronos/preferences.py
+++ b/aleksis/apps/chronos/preferences.py
@@ -55,6 +55,14 @@ class ShortenGroupsLimit(IntegerPreference):
     )
 
 
+@site_preferences_registry.register
+class ShowCourseName(BooleanPreference):
+    section = chronos
+    name = "show_course_name"
+    default = False
+    verbose_name = _("Show course name in timetable views")
+
+
 @site_preferences_registry.register
 class SubstitutionsRelevantDays(MultipleChoicePreference):
     """Relevant days which have substitution plans."""
diff --git a/aleksis/apps/chronos/templates/chronos/lesson_event_description.txt b/aleksis/apps/chronos/templates/chronos/lesson_event_description.txt
index 0b9a2edc..a21dfaf4 100644
--- a/aleksis/apps/chronos/templates/chronos/lesson_event_description.txt
+++ b/aleksis/apps/chronos/templates/chronos/lesson_event_description.txt
@@ -1,5 +1,6 @@
 {% load i18n %}{% trans "Groups" %}: {{ event.group_names|default:"–" }}{% if event.subject %}
-{% trans "Subject" %}: {{ event.subject_name_with_amends }}{% endif %}
+{% trans "Subject" %}: {{ event.subject_name_with_amends }}{% endif %}{% if event.course and global_preferences.chronos__show_course_name %}
+{% trans "Course" %}: {{ event.course_name_with_amends }}{% endif %}
 {% trans "Teachers" %}: {{ event.teacher_names_with_amends|default:"–" }}
 {% trans "Rooms" %}: {{ event.room_names_with_amends|default:"–" }}{% if event.comment %}
 
-- 
GitLab