From 98e0a70c7d15bb840eb0efcc1302e4bfdc6f934a Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Mon, 14 Nov 2022 21:19:09 +0100
Subject: [PATCH] Improve rendering of substituted or cancelled items on daily
 lessons/supervisions pages

---
 CHANGELOG.rst                  |  5 ++++
 aleksis/apps/chronos/tables.py | 43 +++++++++++++++++++++++++++-------
 2 files changed, 40 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 3c56b9f4..1d5f45b9 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -9,6 +9,11 @@ and this project adheres to `Semantic Versioning`_.
 Unreleased
 ----------
 
+Changed
+~~~~~~~
+
+* Improve rendering of substituted or cancelled items on daily lessons/supervisions pages.
+
 Fixed
 ~~~~~
 
diff --git a/aleksis/apps/chronos/tables.py b/aleksis/apps/chronos/tables.py
index 89eb15f8..45fa4500 100644
--- a/aleksis/apps/chronos/tables.py
+++ b/aleksis/apps/chronos/tables.py
@@ -11,27 +11,48 @@ from django_tables2.utils import A, Accessor
 from .models import LessonPeriod, Supervision
 
 
-def _css_class_from_lesson_or_supervision_state(
+def _css_color_class_from_lesson_or_supervision_state(
     record: Optional[Union[LessonPeriod, Supervision]] = None,
     table: Optional[Union[LessonsTable, SupervisionsTable]] = None,
 ) -> str:
     """Return CSS class depending on lesson or supervision state."""
     if record.get_substitution():
         if hasattr(record.get_substitution(), "cancelled") and record.get_substitution().cancelled:
-            return "success"
+            return "error"
         else:
             return "warning"
     else:
         return ""
 
 
+def _title_attr_from_lesson_or_supervision_state(
+    record: Optional[Union[LessonPeriod, Supervision]] = None,
+    table: Optional[Union[LessonsTable, SupervisionsTable]] = None,
+) -> str:
+    """Return HTML title depending on lesson or supervision state."""
+    if record.get_substitution():
+        if hasattr(record.get_substitution(), "cancelled") and record.get_substitution().cancelled:
+            return _("Lesson cancelled")
+        else:
+            return _("Substituted")
+    else:
+        return ""
+
+
 class SubstitutionColumn(tables.Column):
     def render(self, value, record: Optional[Union[LessonPeriod, Supervision]] = None):
         if record.get_substitution():
-            return format_html(
-                "<s>{}</s> → {}",
-                value,
-                self.substitution_accessor.resolve(record.get_substitution()),
+            return (
+                format_html(
+                    "<s>{}</s> → {}",
+                    value,
+                    self.substitution_accessor.resolve(record.get_substitution()),
+                )
+                if self.substitution_accessor.resolve(record.get_substitution())
+                else format_html(
+                    "<s>{}</s>",
+                    value,
+                )
             )
         return value
 
@@ -45,7 +66,10 @@ class LessonsTable(tables.Table):
 
     class Meta:
         attrs = {"class": "highlight"}
-        row_attrs = {"class": _css_class_from_lesson_or_supervision_state}
+        row_attrs = {
+            "class": _css_color_class_from_lesson_or_supervision_state,
+            "title": _title_attr_from_lesson_or_supervision_state,
+        }
 
     period__period = tables.Column(accessor="period__period")
     lesson__groups = tables.Column(accessor="lesson__group_names", verbose_name=_("Groups"))
@@ -72,7 +96,10 @@ class SupervisionsTable(tables.Table):
 
     class Meta:
         attrs = {"class": "highlight"}
-        row_attrs = {"class": _css_class_from_lesson_or_supervision_state}
+        row_attrs = {
+            "class": _css_color_class_from_lesson_or_supervision_state,
+            "title": _title_attr_from_lesson_or_supervision_state,
+        }
 
     break_item = tables.Column(accessor="break_item")
     area = tables.Column(accessor="area")
-- 
GitLab