is_todo: bool
children: list[TodoStepsNode]
seen: bool
+ hide: bool
class Todo(BaseModel[int], ConditionsRelations):
potentially_enabled = set()
for child in step.children:
for condition in child.enables:
- potentially_enabled.add(condition)
+ potentially_enabled.add(condition.id_)
children += [make_node(child)]
for condition in [c for c in step.conditions
if (not c.is_active)
- and (c not in potentially_enabled)]:
+ and (c.id_ not in potentially_enabled)]:
children += [make_node(condition)]
else:
seen = step.id_ in seen_conditions
seen_conditions.add(step.id_)
- return TodoStepsNode(step, is_todo, children, seen)
+ return TodoStepsNode(step, is_todo, children, seen, False)
node = make_node(self)
return node
+ def get_undone_steps_tree(self) -> TodoStepsNode:
+ """Return tree of depended-on undone Todos and Conditions."""
+
+ def walk_tree(node: TodoStepsNode) -> None:
+ if isinstance(node.item, Todo) and node.item.is_done:
+ node.hide = True
+ for child in node.children:
+ walk_tree(child)
+
+ seen_todos: set[int] = set()
+ seen_conditions: set[int] = set()
+ step_tree = self.get_step_tree(seen_todos, seen_conditions)
+ walk_tree(step_tree)
+ return step_tree
+
+ def get_done_steps_tree(self) -> list[TodoStepsNode]:
+ """Return tree of depended-on done Todos."""
+
+ def make_nodes(node: TodoStepsNode) -> list[TodoStepsNode]:
+ children: list[TodoStepsNode] = []
+ if not isinstance(node.item, Todo):
+ return children
+ for child in node.children:
+ children += make_nodes(child)
+ if node.item.is_done:
+ node.children = children
+ return [node]
+ return children
+
+ seen_todos: set[int] = set()
+ seen_conditions: set[int] = set()
+ step_tree = self.get_step_tree(seen_todos, seen_conditions)
+ nodes = make_nodes(step_tree)
+ return nodes
+
def add_child(self, child: Todo) -> None:
"""Add child to self.children, avoid recursion, update parenthoods."""
raise NotFoundException('Process of Todo without ID (not saved?)')
self.save_core(db_conn)
assert isinstance(self.id_, int)
+ db_conn.rewrite_relations('todo_children', 'child', self.id_,
+ [[p.id_] for p in self.parents])
db_conn.rewrite_relations('todo_children', 'parent', self.id_,
[[c.id_] for c in self.children])
db_conn.rewrite_relations('todo_conditions', 'todo', self.id_,
def remove(self, db_conn: DatabaseConnection) -> None:
"""Remove from DB, including relations."""
assert isinstance(self.id_, int)
- for child in self.children:
+ children_to_remove = self.children[:]
+ parents_to_remove = self.parents[:]
+ for child in children_to_remove:
self.remove_child(child)
- for parent in self.parents:
+ for parent in parents_to_remove:
parent.remove_child(self)
+ db_conn.delete_where('todo_children', 'parent', self.id_)
+ db_conn.delete_where('todo_children', 'child', self.id_)
db_conn.delete_where('todo_conditions', 'todo', self.id_)
db_conn.delete_where('todo_enables', 'todo', self.id_)
db_conn.delete_where('todo_disables', 'todo', self.id_)