From: Christian Heller <c.heller@plomlompom.de>
Date: Sat, 13 Apr 2024 01:20:33 +0000 (+0200)
Subject: Add Todo.parents.
X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/static/%7B%7Bdb.prefix%7D%7D/calendar?a=commitdiff_plain;h=d77d0661697655a7c2d0e9e0b270cf21e286d7fb;p=plomtask

Add Todo.parents.
---

diff --git a/plomtask/todos.py b/plomtask/todos.py
index 7faea73..e8e00ac 100644
--- a/plomtask/todos.py
+++ b/plomtask/todos.py
@@ -18,6 +18,7 @@ class Todo:
         self.is_done = is_done
         self.day = day
         self.children: list[Todo] = []
+        self.parents: list[Todo] = []
 
     @classmethod
     def from_table_row(cls, db_conn: DatabaseConnection, row: Row) -> Todo:
@@ -46,6 +47,9 @@ class Todo:
             for row in db_conn.exec('SELECT child FROM todo_children '
                                     'WHERE parent = ?', (id_,)):
                 todo.children += [cls.by_id(db_conn, row[0])]
+            for row in db_conn.exec('SELECT parent FROM todo_children '
+                                    'WHERE child = ?', (id_,)):
+                todo.parents += [cls.by_id(db_conn, row[0])]
         assert isinstance(todo, Todo)
         return todo
 
@@ -72,6 +76,7 @@ class Todo:
             raise BadFormatException('cannot adopt same child twice')
         walk_steps(child)
         self.children += [child]
+        child.parents += [self]
 
     def save(self, db_conn: DatabaseConnection) -> None:
         """Write self and children to DB and its cache."""
diff --git a/tests/todos.py b/tests/todos.py
index 7ae6b64..a377920 100644
--- a/tests/todos.py
+++ b/tests/todos.py
@@ -118,6 +118,7 @@ class TestsWithServer(TestCaseWithServer):
         self.check_post(form_data, '/todo?id=1', 302, '/')
         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
         self.assertEqual(todo1.children, [])
+        self.assertEqual(todo1.parents, [])
         form_data = {'adopt': 'foo'}
         self.check_post(form_data, '/todo?id=1', 400)
         form_data = {'adopt': 1}
@@ -132,6 +133,9 @@ class TestsWithServer(TestCaseWithServer):
         todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0]
         todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1]
         self.assertEqual(todo1.children, [todo2])
+        self.assertEqual(todo1.parents, [])
+        self.assertEqual(todo2.children, [])
+        self.assertEqual(todo2.parents, [todo1])
         self.check_post(form_data, '/todo?id=1', 400, '/')
 
     def test_do_GET_todo(self) -> None: