home · contact · privacy
Add Day.comment field.
authorChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 00:02:03 +0000 (01:02 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Wed, 20 Mar 2024 00:02:03 +0000 (01:02 +0100)
plomtask/days.py
scripts/init.sql
templates/calendar.html
templates/day.html
tests/test_days.py

index d64b34b6bc69c01fd6d6a17437bbc90f357d301a..ba466b52c792f81029a94f4bc9fcc21718dd500b 100644 (file)
@@ -19,11 +19,12 @@ def date_valid(date: str):
 class Day:
     """Individual days defined by their dates."""
 
-    def __init__(self, date: str):
+    def __init__(self, date: str, comment: str = ''):
         self.date = date
         self.datetime = date_valid(self.date)
         if not self.datetime:
             raise HandledException(f'Given date of wrong format: {self.date}')
+        self.comment = comment
 
     def __eq__(self, other: object):
         return isinstance(other, self.__class__) and self.date == other.date
@@ -34,7 +35,7 @@ class Day:
     @classmethod
     def from_table_row(cls, row: Row):
         """Make new Day from database row."""
-        return cls(row[0])
+        return cls(row[0], row[1])
 
     @classmethod
     def all(cls, db_conn: DatabaseConnection,
@@ -63,4 +64,5 @@ class Day:
 
     def save(self, db_conn: DatabaseConnection):
         """Add (or re-write) self to database."""
-        db_conn.exec('REPLACE INTO days VALUES (?)', (self.date,))
+        db_conn.exec('REPLACE INTO days VALUES (?, ?)',
+                     (self.date, self.comment))
index 1aba8a4315577827f28bcc3296bbfe61c85e27e6..cc68fb6302ef0b11e690e7596d66282603f336d5 100644 (file)
@@ -1,3 +1,4 @@
 CREATE TABLE days (
-    date TEXT PRIMARY KEY
+    date TEXT PRIMARY KEY,
+    comment TEXT NOT NULL
 );
index 832e03867614ef0aa08d7694dbfd4b325b8ac298..0b7197fd526b171c81d112171abf83f69fb9b354 100644 (file)
@@ -3,7 +3,7 @@
 {% block content %}
 <ul>
 {% for day in days %}
-<li><a href="day?date={{day.date}}">{{day.date}}</a> ({{day.weekday}})
+<li><a href="day?date={{day.date}}">{{day.date}}</a> ({{day.weekday}}) {{day.comment|e}}
 {% endfor %}
 </ul>
 {% endblock %}
index 51829b2282146c09fb9ec175103f4fe2944378f9..5547f4534e94742600f0664aaece2b7cdc33afc3 100644 (file)
@@ -2,5 +2,6 @@
 
 {% block content %}
 <h3>{{day.date}} / {{day.weekday}}</h3>
+comment: {{day.comment|e}}
 {% endblock %}
 
index d3a7bba656d19d39f35b93535564e4e3d17b0a2c..be2719318140fa52e5c53613e854a727b76d1887 100644 (file)
@@ -81,3 +81,15 @@ class TestsWithDB(TestCase):
                          [day1, day2])
         self.assertEqual(Day.all(self.db_conn, ('2024-01-03, 2024-01-01')),
                          [])
+
+    def test_Day_comment(self):
+        """Test Day.by_date()."""
+        self.assertEqual(None, Day.by_date(self.db_conn, '2024-01-01'))
+        d1 = Day('2024-01-01')
+        d1.save(self.db_conn)
+        day_retrieved = Day.by_date(self.db_conn, d1.date)
+        self.assertEqual(day_retrieved.comment, '')
+        d2 = Day('2024-01-02', 'foo')
+        d2.save(self.db_conn)
+        day_retrieved = Day.by_date(self.db_conn, d2.date)
+        self.assertEqual(day_retrieved.comment, 'foo')