From: Christian Heller Date: Wed, 20 Mar 2024 00:02:03 +0000 (+0100) Subject: Add Day.comment field. X-Git-Url: https://plomlompom.com/repos/?p=plomtask;a=commitdiff_plain;h=bf8b491d50379772879f1cc9cbe6846fe50ce63b Add Day.comment field. --- 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 %} {% 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 %}

{{day.date}} / {{day.weekday}}

+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')