From 12bbfea600184c9e2657cc9a0c705848a4dae29a Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 20 Mar 2024 03:50:28 +0100
Subject: [PATCH] Add Day.prev_date/Day.next_date and use them for navigation
 in Day page.

---
 plomtask/days.py   | 20 ++++++++++++++++----
 templates/day.html |  3 +++
 tests/test_days.py |  7 ++++++-
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/plomtask/days.py b/plomtask/days.py
index 776658e..0936d04 100644
--- a/plomtask/days.py
+++ b/plomtask/days.py
@@ -1,5 +1,5 @@
 """Collecting Day and date-related items."""
-from datetime import datetime
+from datetime import datetime, timedelta
 from sqlite3 import Row
 from plomtask.misc import HandledException
 from plomtask.db import DatabaseConnection
@@ -46,7 +46,7 @@ class Day:
     def all(cls, db_conn: DatabaseConnection,
             date_range: tuple[str, str] = ('', '')):
         """Return list of Days in database within date_range."""
-        def date_from_range_element(date_str: str, default: str):
+        def date_from_range_str(date_str: str, default: str):
             if date_str == '':
                 date_str = default
             elif date_str == 'today':
@@ -54,8 +54,8 @@ class Day:
             elif not date_valid(date_str):
                 raise HandledException(f'Bad date: {date_str}')
             return date_str
-        start_date = date_from_range_element(date_range[0], '2024-01-01')
-        end_date = date_from_range_element(date_range[1], '2030-01-01')
+        start_date = date_from_range_str(date_range[0], '2024-01-01')
+        end_date = date_from_range_str(date_range[1], '2030-01-01')
         days = []
         sql = 'SELECT * FROM days WHERE date >= ? AND date <= ?'
         for row in db_conn.exec(sql, (start_date, end_date)):
@@ -78,6 +78,18 @@ class Day:
         """Return what weekday matches self.date."""
         return self.datetime.strftime('%A')
 
+    @property
+    def prev_date(self):
+        """Return date preceding date of this Day."""
+        prev_datetime = self.datetime - timedelta(days=1)
+        return prev_datetime.strftime(DATE_FORMAT)
+
+    @property
+    def next_date(self):
+        """Return date succeeding date of this Day."""
+        next_datetime = self.datetime + timedelta(days=1)
+        return next_datetime.strftime(DATE_FORMAT)
+
     def save(self, db_conn: DatabaseConnection):
         """Add (or re-write) self to database."""
         db_conn.exec('REPLACE INTO days VALUES (?, ?)',
diff --git a/templates/day.html b/templates/day.html
index 0fff9e0..7a34e58 100644
--- a/templates/day.html
+++ b/templates/day.html
@@ -2,6 +2,9 @@
 
 {% block content %}
 <h3>{{day.date}} / {{day.weekday}}</h3>
+<p>
+<a href="day?date={{day.prev_date}}">prev</a> | <a href="day?date={{day.next_date}}">next</a>
+</p>
 <form action="day?date={{day.date}}" method="POST">
 comment: <input name="comment" value="{{day.comment|e}}" />
 <input type="submit" value="OK" />
diff --git a/tests/test_days.py b/tests/test_days.py
index 2adf765..ba91c05 100644
--- a/tests/test_days.py
+++ b/tests/test_days.py
@@ -94,7 +94,7 @@ class TestsWithDB(TestCase):
         self.assertEqual(Day.all(self.db_conn, ('today', 'today')), [today])
 
     def test_Day_comment(self):
-        """Test Day.by_date()."""
+        """Test Day.comment."""
         self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
         d1 = Day('2024-01-01')
         d1.save(self.db_conn)
@@ -105,6 +105,11 @@ class TestsWithDB(TestCase):
         day_retrieved = Day.by_date(self.db_conn, d2.date)
         self.assertEqual(day_retrieved.comment, 'foo')
 
+    def test_Day_neighbor_dates(self):
+        """Test Day.prev_date and Day.next_date."""
+        self.assertEqual(Day('2024-01-01').prev_date, '2023-12-31')
+        self.assertEqual(Day('2023-02-28').next_date, '2023-03-01')
+
 
 class TestsWithServer(TestsWithDB):
     """Tests against our HTTP server/handler (and database)."""
-- 
2.30.2