From bf8b491d50379772879f1cc9cbe6846fe50ce63b Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Wed, 20 Mar 2024 01:02:03 +0100
Subject: [PATCH] Add Day.comment field.

---
 plomtask/days.py        |  8 +++++---
 scripts/init.sql        |  3 ++-
 templates/calendar.html |  2 +-
 templates/day.html      |  1 +
 tests/test_days.py      | 12 ++++++++++++
 5 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/plomtask/days.py b/plomtask/days.py
index d64b34b..ba466b5 100644
--- a/plomtask/days.py
+++ b/plomtask/days.py
@@ -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))
diff --git a/scripts/init.sql b/scripts/init.sql
index 1aba8a4..cc68fb6 100644
--- a/scripts/init.sql
+++ b/scripts/init.sql
@@ -1,3 +1,4 @@
 CREATE TABLE days (
-    date TEXT PRIMARY KEY
+    date TEXT PRIMARY KEY,
+    comment TEXT NOT NULL
 );
diff --git a/templates/calendar.html b/templates/calendar.html
index 832e038..0b7197f 100644
--- a/templates/calendar.html
+++ b/templates/calendar.html
@@ -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 %}
diff --git a/templates/day.html b/templates/day.html
index 51829b2..5547f45 100644
--- a/templates/day.html
+++ b/templates/day.html
@@ -2,5 +2,6 @@
 
 {% block content %}
 <h3>{{day.date}} / {{day.weekday}}</h3>
+comment: {{day.comment|e}}
 {% endblock %}
 
diff --git a/tests/test_days.py b/tests/test_days.py
index d3a7bba..be27193 100644
--- a/tests/test_days.py
+++ b/tests/test_days.py
@@ -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')
-- 
2.30.2