From ba98d8ede560afd51e8dddd7cab893d758efcc8b Mon Sep 17 00:00:00 2001
From: Hangzhi Yu <hangzhi@protonmail.com>
Date: Wed, 2 Nov 2022 17:54:21 +0100
Subject: [PATCH] Display initial lesson data with substituted lessons in daily
 lessons table

---
 CHANGELOG.rst                                 |  1 +
 aleksis/apps/chronos/tables.py                | 30 ++++++++++++++++---
 .../templates/chronos/lessons_day.html        |  4 ---
 3 files changed, 27 insertions(+), 8 deletions(-)

diff --git a/CHANGELOG.rst b/CHANGELOG.rst
index 91111fe9..8ff47728 100644
--- a/CHANGELOG.rst
+++ b/CHANGELOG.rst
@@ -13,6 +13,7 @@ Added
 ~~~~~
 
 * Add filter to daily lessons page.
+* Display initial lesson data with substituted lessons in daily lessons table.
 
 Fixed
 ~~~~~
diff --git a/aleksis/apps/chronos/tables.py b/aleksis/apps/chronos/tables.py
index 6caff934..887291f7 100644
--- a/aleksis/apps/chronos/tables.py
+++ b/aleksis/apps/chronos/tables.py
@@ -2,10 +2,11 @@ from __future__ import annotations
 
 from typing import Optional
 
+from django.utils.html import format_html
 from django.utils.translation import gettext_lazy as _
 
 import django_tables2 as tables
-from django_tables2.utils import A
+from django_tables2.utils import A, Accessor
 
 from .models import LessonPeriod
 
@@ -23,6 +24,21 @@ def _css_class_from_lesson_state(
         return ""
 
 
+class SubstitutionColumn(tables.Column):
+    def render(self, value, record: Optional[LessonPeriod] = None):
+        if record.get_substitution():
+            return format_html(
+                "<s>{}</s> → {}",
+                value,
+                self.substitution_accessor.resolve(record.get_substitution()),
+            )
+        return value
+
+    def __init__(self, *args, **kwargs):
+        self.substitution_accessor = Accessor(kwargs.pop("substitution_accessor"))
+        super().__init__(*args, **kwargs)
+
+
 class LessonsTable(tables.Table):
     """Table for daily lessons and management of substitutions."""
 
@@ -32,9 +48,15 @@ class LessonsTable(tables.Table):
 
     period__period = tables.Column(accessor="period__period")
     lesson__groups = tables.Column(accessor="lesson__group_names", verbose_name=_("Groups"))
-    lesson__teachers = tables.Column(accessor="lesson__teacher_names", verbose_name=_("Teachers"))
-    lesson__subject = tables.Column(accessor="lesson__subject")
-    room = tables.Column(accessor="room")
+    lesson__teachers = SubstitutionColumn(
+        accessor="lesson__teacher_names",
+        substitution_accessor="teacher_names",
+        verbose_name=_("Teachers"),
+    )
+    lesson__subject = SubstitutionColumn(
+        accessor="lesson__subject", substitution_accessor="subject"
+    )
+    room = SubstitutionColumn(accessor="room", substitution_accessor="room")
     edit_substitution = tables.LinkColumn(
         "edit_substitution",
         args=[A("id"), A("_week")],
diff --git a/aleksis/apps/chronos/templates/chronos/lessons_day.html b/aleksis/apps/chronos/templates/chronos/lessons_day.html
index e0147132..e8d0662f 100644
--- a/aleksis/apps/chronos/templates/chronos/lessons_day.html
+++ b/aleksis/apps/chronos/templates/chronos/lessons_day.html
@@ -24,10 +24,6 @@
     {% form form=lesson_periods_filter.form %}{% endform %}
     {% trans "Search" as caption %}
     {% include "core/partials/save_button.html" with caption=caption icon="mdi:search" %}
-    <button type="reset" class="btn red waves-effect waves-light">
-      <i class="material-icons iconify left" data-icon="mdi:close"></i>
-      {% trans "Clear" %}
-    </button>
   </form>
 
   <div class="row no-margin">
-- 
GitLab