+ @dataclass
+ class TodoStepsNode:
+ """Collect what's useful for Todo steps tree display."""
+ id_: int
+ todo: Todo | None
+ process: Process | None
+ children: list[TodoStepsNode] # pylint: disable=undefined-variable
+ fillable: bool = False
+
+ def walk_process_steps(id_: int,
+ process_step_nodes: list[ProcessStepsNode],
+ steps_nodes: list[TodoStepsNode]) -> None:
+ for process_step_node in process_step_nodes:
+ id_ += 1
+ node = TodoStepsNode(id_, None, process_step_node.process, [])
+ steps_nodes += [node]
+ walk_process_steps(id_, list(process_step_node.steps.values()),
+ node.children)
+
+ def walk_todo_steps(id_: int, todos: list[Todo],
+ steps_nodes: list[TodoStepsNode]) -> None:
+ for todo in todos:
+ matched = False
+ for match in [item for item in steps_nodes
+ if item.process
+ and item.process == todo.process]:
+ match.todo = todo
+ matched = True
+ for child in match.children:
+ child.fillable = True
+ walk_todo_steps(id_, todo.children, match.children)
+ if not matched:
+ id_ += 1
+ node = TodoStepsNode(id_, todo, None, [])
+ steps_nodes += [node]
+ walk_todo_steps(id_, todo.children, node.children)
+
+ def collect_adoptables_keys(steps_nodes: list[TodoStepsNode]
+ ) -> set[int]:
+ ids = set()
+ for node in steps_nodes:
+ if not node.todo:
+ assert isinstance(node.process, Process)
+ assert isinstance(node.process.id_, int)
+ ids.add(node.process.id_)
+ ids = ids | collect_adoptables_keys(node.children)
+ return ids
+
+ id_ = self._params.get_int('id')
+ todo = Todo.by_id(self.conn, id_)
+ todo_steps = [step.todo for step in todo.get_step_tree(set()).children]
+ process_tree = todo.process.get_steps(self.conn, None)
+ steps_todo_to_process: list[TodoStepsNode] = []
+ walk_process_steps(0, list(process_tree.values()),
+ steps_todo_to_process)
+ for steps_node in steps_todo_to_process:
+ steps_node.fillable = True
+ walk_todo_steps(len(steps_todo_to_process), todo_steps,
+ steps_todo_to_process)
+ adoptables: dict[int, list[Todo]] = {}
+ any_adoptables = [Todo.by_id(self.conn, t.id_)
+ for t in Todo.by_date(self.conn, todo.date)
+ if t != todo]
+ for id_ in collect_adoptables_keys(steps_todo_to_process):
+ adoptables[id_] = [t for t in any_adoptables
+ if t.process.id_ == id_]
+ return {'todo': todo, 'steps_todo_to_process': steps_todo_to_process,
+ 'adoption_candidates_for': adoptables,
+ 'process_candidates': Process.all(self.conn),
+ 'todo_candidates': any_adoptables,
+ 'condition_candidates': Condition.all(self.conn)}
+
+ def do_GET_todos(self) -> dict[str, object]:
+ """Show Todos from ?start= to ?end=, of ?process=, ?comment= pattern"""
+ sort_by = self._params.get_str('sort_by')
+ start = self._params.get_str('start')
+ end = self._params.get_str('end')
+ process_id = self._params.get_int_or_none('process_id')
+ comment_pattern = self._params.get_str('comment_pattern')
+ todos = []
+ ret = Todo.by_date_range_with_limits(self.conn, (start, end))
+ todos_by_date_range, start, end = ret
+ todos = [t for t in todos_by_date_range
+ if comment_pattern in t.comment
+ and ((not process_id) or t.process.id_ == process_id)]
+ if sort_by == 'doneness':
+ todos.sort(key=lambda t: t.is_done)
+ elif sort_by == '-doneness':
+ todos.sort(key=lambda t: t.is_done, reverse=True)
+ elif sort_by == 'title':
+ todos.sort(key=lambda t: t.title_then)
+ elif sort_by == '-title':
+ todos.sort(key=lambda t: t.title_then, reverse=True)
+ elif sort_by == 'comment':
+ todos.sort(key=lambda t: t.comment)
+ elif sort_by == '-comment':
+ todos.sort(key=lambda t: t.comment, reverse=True)
+ elif sort_by == '-date':
+ todos.sort(key=lambda t: t.date, reverse=True)
+ else:
+ todos.sort(key=lambda t: t.date)
+ return {'start': start, 'end': end, 'process_id': process_id,
+ 'comment_pattern': comment_pattern, 'todos': todos,
+ 'all_processes': Process.all(self.conn), 'sort_by': sort_by}
+
+ def do_GET_conditions(self) -> dict[str, object]:
+ """Show all Conditions."""
+ pattern = self._params.get_str('pattern')
+ conditions = Condition.matching(self.conn, pattern)
+ sort_by = self._params.get_str('sort_by')
+ if sort_by == 'is_active':
+ conditions.sort(key=lambda c: c.is_active)
+ elif sort_by == '-is_active':
+ conditions.sort(key=lambda c: c.is_active, reverse=True)
+ elif sort_by == '-title':
+ conditions.sort(key=lambda c: c.title.newest, reverse=True)
+ else:
+ conditions.sort(key=lambda c: c.title.newest)
+ return {'conditions': conditions,
+ 'sort_by': sort_by,
+ 'pattern': pattern}
+
+ def do_GET_condition(self) -> dict[str, object]: