From 7bd9c956b0a0efaed731683732d3e54753e5269e Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 20 Mar 2024 04:01:14 +0100
Subject: [PATCH] In calendar view, fill gaps between non-subsequent dates.

---
 plomtask/days.py   | 11 ++++++++++-
 plomtask/http.py   |  4 ++--
 tests/test_days.py |  7 +++++++
 3 files changed, 19 insertions(+), 3 deletions(-)

diff --git a/plomtask/days.py b/plomtask/days.py
index 0936d04..aee4bf8 100644
--- a/plomtask/days.py
+++ b/plomtask/days.py
@@ -44,7 +44,7 @@ class Day:
 
     @classmethod
     def all(cls, db_conn: DatabaseConnection,
-            date_range: tuple[str, str] = ('', '')):
+            date_range: tuple[str, str] = ('', ''), fill_gaps: bool = False):
         """Return list of Days in database within date_range."""
         def date_from_range_str(date_str: str, default: str):
             if date_str == '':
@@ -61,6 +61,15 @@ class Day:
         for row in db_conn.exec(sql, (start_date, end_date)):
             days += [cls.from_table_row(row)]
         days.sort()
+        if fill_gaps and len(days) > 1:
+            gapless_days = []
+            for i, day in enumerate(days):
+                gapless_days += [day]
+                if i < len(days) - 1:
+                    while day.next_date != days[i+1].date:
+                        day = Day(day.next_date)
+                        gapless_days += [day]
+            days = gapless_days
         return days
 
     @classmethod
diff --git a/plomtask/http.py b/plomtask/http.py
index e812471..3ef1721 100644
--- a/plomtask/http.py
+++ b/plomtask/http.py
@@ -45,9 +45,9 @@ class TaskHandler(BaseHTTPRequestHandler):
 
     def do_GET_calendar(self, conn: DatabaseConnection, start: str, end: str):
         """Show Days."""
+        days = Day.all(conn, date_range=(start, end), fill_gaps=True)
         return self.server.jinja.get_template('calendar.html').render(
-                days=Day.all(conn, date_range=(start, end)),
-                start=start, end=end)
+                days=days, start=start, end=end)
 
     def do_GET_day(self, conn: DatabaseConnection, date: str):
         """Show single Day."""
diff --git a/tests/test_days.py b/tests/test_days.py
index f35e6b2..bc2e82d 100644
--- a/tests/test_days.py
+++ b/tests/test_days.py
@@ -89,6 +89,13 @@ class TestsWithDB(TestCase):
                          [day1, day2])
         self.assertEqual(Day.all(self.db_conn, ('2024-01-03', '2024-01-01')),
                          [])
+        day4 = Day('2024-01-04')
+        day5 = Day('2024-01-05')
+        day6 = Day('2024-01-06')
+        day6.save(self.db_conn)
+        self.assertEqual(Day.all(self.db_conn, ('2024-01-02', '2024-01-07'),
+                                 fill_gaps=True),
+                         [day2, day3, day4, day5, day6])
         today = Day(todays_date())
         today.save(self.db_conn)
         self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
-- 
2.30.2