home · contact · privacy
Minor fixes.
[plomtask] / plomtask / todos.py
index 336ec0350830ce5dfbdf5e85a92b3945608835d9..ff6cdcb1e96144743f828ca5fea14072988be10a 100644 (file)
@@ -1,6 +1,6 @@
 """Actionables."""
 from __future__ import annotations
-from collections import namedtuple
+from dataclasses import dataclass
 from typing import Any
 from sqlite3 import Row
 from plomtask.db import DatabaseConnection, BaseModel
@@ -10,8 +10,13 @@ from plomtask.exceptions import (NotFoundException, BadFormatException,
                                  HandledException)
 
 
-TodoStepsNode = namedtuple('TodoStepsNode',
-                           ('item', 'is_todo', 'children', 'seen'))
+@dataclass
+class TodoStepsNode:
+    """Collects what's useful to know for Todo/Condition tree display."""
+    item: Todo | Condition
+    is_todo: bool
+    children: list[TodoStepsNode]
+    seen: bool
 
 
 class Todo(BaseModel, ConditionsRelations):
@@ -170,12 +175,14 @@ class Todo(BaseModel, ConditionsRelations):
         return node
 
     def add_child(self, child: Todo) -> None:
-        """Add child to self.children, guard against recursion"""
+        """Add child to self.children, avoid recursion, update parenthoods."""
+
         def walk_steps(node: Todo) -> None:
             if node.id_ == self.id_:
                 raise BadFormatException('bad child choice causes recursion')
             for child in node.children:
                 walk_steps(child)
+
         if self.id_ is None:
             raise HandledException('Can only add children to saved Todos.')
         if child.id_ is None:
@@ -186,6 +193,13 @@ class Todo(BaseModel, ConditionsRelations):
         self.children += [child]
         child.parents += [self]
 
+    def remove_child(self, child: Todo) -> None:
+        """Remove child from self.children, update counter relations."""
+        if child not in self.children:
+            raise HandledException('Cannot remove un-parented child.')
+        self.children.remove(child)
+        child.parents.remove(self)
+
     def save(self, db_conn: DatabaseConnection) -> None:
         """Write self and children to DB and its cache."""
         if self.process.id_ is None: