From: Christian Heller Date: Mon, 22 Apr 2024 04:17:46 +0000 (+0200) Subject: Refactor Todo adoption code. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bprefix%7D%7D/static/bar%20baz.html?a=commitdiff_plain;h=e825a876e82ffbedf0234f4dfb6d6055d9e29241;p=plomtask Refactor Todo adoption code. --- diff --git a/plomtask/http.py b/plomtask/http.py index 64cc6f9..deadb21 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -200,11 +200,7 @@ class TaskHandler(BaseHTTPRequestHandler): process = Process.by_id(self.conn, process_id) todo = Todo(None, process, False, day.date) todo.save(self.conn) - for step in todo.process.explicit_steps: - for t in [t for t in existing_todos - if t.process.id_ == step.step_process_id]: - todo.add_child(t) - break + todo.adopt_from(existing_todos) todo.save(self.conn) def do_POST_todo(self) -> None: diff --git a/plomtask/todos.py b/plomtask/todos.py index ff6cdcb..ed78ca9 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -39,9 +39,9 @@ class Todo(BaseModel, ConditionsRelations): self.enables: list[Condition] = [] self.disables: list[Condition] = [] if not self.id_: - self.conditions = process.conditions[:] - self.enables = process.enables[:] - self.disables = process.disables[:] + self.conditions = self.process.conditions[:] + self.enables = self.process.enables[:] + self.disables = self.process.disables[:] @classmethod def from_table_row(cls, db_conn: DatabaseConnection, @@ -127,6 +127,17 @@ class Todo(BaseModel, ConditionsRelations): """Return ID of tasked Process.""" return self.process.id_ + @property + def unsatisfied_dependencies(self) -> list[int]: + """Return Process IDs of .process.explicit_steps not in .children.""" + child_process_ids = {c.process.id_ for c in self.children} + unsatisfied: list[int] = [] + for process_id in [s.step_process_id + for s in self.process.explicit_steps]: + if process_id not in child_process_ids: + unsatisfied += [process_id] + return unsatisfied + @property def is_done(self) -> bool: """Wrapper around self._is_done so we can control its setter.""" @@ -144,6 +155,13 @@ class Todo(BaseModel, ConditionsRelations): for condition in self.disables: condition.is_active = False + def adopt_from(self, todos: list[Todo]) -> None: + """As far as possible, fill unsatisfied dependencies from todos.""" + for process_id in self.unsatisfied_dependencies: + for todo in [t for t in todos if t.process.id_ == process_id]: + self.add_child(todo) + break + def get_step_tree(self, seen_todos: set[int], seen_conditions: set[int]) -> TodoStepsNode: """Return tree of depended-on Todos and Conditions."""