home · contact · privacy
Add Day.prev_date/Day.next_date and use them for navigation in Day page.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 02:50:28 +0000 (03:50 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 02:50:28 +0000 (03:50 +0100)
plomtask/days.py
templates/day.html
tests/test_days.py

index 776658e14aa6f7f488eeda7546eb8612c2f50ee0..0936d047aa3eda7c4ce9374c90ca3f9ad3362ad5 100644 (file)
@@ -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 (?, ?)',
index 0fff9e049792f695230947e56afc5be3b4d40060..7a34e58544f1c93a871bc316962d2bf60040b5d2 100644 (file)
@@ -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" />
index 2adf7657a46f2f7d49102c7aa3db06bbcc117302..ba91c057a1d38bbe1aaef197e2adeb320fcc2f94 100644 (file)
@@ -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)."""