home · contact · privacy
In calendar view, fill gaps between non-subsequent dates.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 03:01:14 +0000 (04:01 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 03:01:14 +0000 (04:01 +0100)
plomtask/days.py
plomtask/http.py
tests/test_days.py

index 0936d047aa3eda7c4ce9374c90ca3f9ad3362ad5..aee4bf82354574b91b9b9f0983865daf59f54db1 100644 (file)
@@ -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
index e812471a9c8eb5bd0d0dccb6df1f5546ee16a20d..3ef1721e5aa1b4c9a1ff19856fb99d76c669e4bb 100644 (file)
@@ -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."""
index f35e6b2725cdc3545f4490d7d8fe94f940704004..bc2e82d2dee69766f451a50dc6c90ae1e2abf834 100644 (file)
@@ -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])