home · contact · privacy
Refactor Todo adoption code.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 22 Apr 2024 04:17:46 +0000 (06:17 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 22 Apr 2024 04:17:46 +0000 (06:17 +0200)
plomtask/http.py
plomtask/todos.py

index 64cc6f95871174c71badbf495f9f3dfe56f74cc1..deadb21f88c65726aa7544c810922f2712600e36 100644 (file)
@@ -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:
index ff6cdcb1e96144743f828ca5fea14072988be10a..ed78ca947b359286456e7dcc4c664fcb32961c12 100644 (file)
@@ -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."""