X-Git-Url: https://plomlompom.com/repos/foo.html?a=blobdiff_plain;ds=inline;f=plomtask%2Ftodos.py;h=de6438c1e248777f388a1e8217b7fc059c851c2c;hb=2f4c681e1c4d78eacc1daa5f297f06038b45912e;hp=46a353d91456a13b7968ba6718e02f1ea70e81a8;hpb=f92de64d072009c8c4bf96b9eeb9fa245045662b;p=plomtask
diff --git a/plomtask/todos.py b/plomtask/todos.py
index 46a353d..de6438c 100644
--- a/plomtask/todos.py
+++ b/plomtask/todos.py
@@ -4,7 +4,7 @@ from dataclasses import dataclass
from typing import Any
from sqlite3 import Row
from plomtask.db import DatabaseConnection, BaseModel
-from plomtask.processes import Process
+from plomtask.processes import Process, ProcessStepsNode
from plomtask.versioned_attributes import VersionedAttribute
from plomtask.conditions import Condition, ConditionsRelations
from plomtask.exceptions import (NotFoundException, BadFormatException,
@@ -26,12 +26,12 @@ class Todo(BaseModel[int], ConditionsRelations):
table_name = 'todos'
to_save = ['process_id', 'is_done', 'date', 'comment', 'effort',
'calendarize']
- to_save_relations = [('todo_conditions', 'todo', 'conditions'),
- ('todo_blockers', 'todo', 'blockers'),
- ('todo_enables', 'todo', 'enables'),
- ('todo_disables', 'todo', 'disables'),
- ('todo_children', 'parent', 'children'),
- ('todo_children', 'child', 'parents')]
+ to_save_relations = [('todo_conditions', 'todo', 'conditions', 0),
+ ('todo_blockers', 'todo', 'blockers', 0),
+ ('todo_enables', 'todo', 'enables', 0),
+ ('todo_disables', 'todo', 'disables', 0),
+ ('todo_children', 'parent', 'children', 0),
+ ('todo_children', 'child', 'parents', 1)]
to_search = ['comment']
# pylint: disable=too-many-arguments
@@ -68,25 +68,52 @@ class Todo(BaseModel[int], ConditionsRelations):
return todos
@classmethod
- def create_with_children(cls, db_conn: DatabaseConnection, date: str,
- process_ids: list[int]) -> list[Todo]:
- """Create Todos of process_ids for date, ensure children."""
- new_todos = []
- for process_id in process_ids:
- process = Process.by_id(db_conn, process_id)
- todo = Todo(None, process, False, date)
- todo.save(db_conn)
- new_todos += [todo]
- nothing_to_adopt = False
- while not nothing_to_adopt:
- nothing_to_adopt = True
- existing_todos = Todo.by_date(db_conn, date)
- for todo in new_todos:
- if todo.adopt_from(existing_todos):
- nothing_to_adopt = False
- todo.make_missing_children(db_conn)
- todo.save(db_conn)
- return new_todos
+ def create_with_children(cls, db_conn: DatabaseConnection,
+ process_id: int, date: str) -> Todo:
+ """Create Todo of process for date, ensure children."""
+
+ def key_order_func(n: ProcessStepsNode) -> int:
+ assert isinstance(n.process.id_, int)
+ return n.process.id_
+
+ def walk_steps(parent: Todo, step_node: ProcessStepsNode) -> Todo:
+ adoptables = [t for t in cls.by_date(db_conn, date)
+ if (t not in parent.children)
+ and (t != parent)
+ and step_node.process == t.process]
+ satisfier = None
+ for adoptable in adoptables:
+ satisfier = adoptable
+ break
+ if not satisfier:
+ satisfier = cls(None, step_node.process, False, date)
+ satisfier.save(db_conn)
+ sub_step_nodes = list(step_node.steps.values())
+ sub_step_nodes.sort(key=key_order_func)
+ for sub_node in sub_step_nodes:
+ if sub_node.is_suppressed:
+ continue
+ n_slots = len([n for n in sub_step_nodes
+ if n.process == sub_node.process])
+ filled_slots = len([t for t in satisfier.children
+ if t.process == sub_node.process])
+ # if we did not newly create satisfier, it may already fill
+ # some step dependencies, so only fill what remains open
+ if n_slots - filled_slots > 0:
+ satisfier.add_child(walk_steps(satisfier, sub_node))
+ satisfier.save(db_conn)
+ return satisfier
+
+ process = Process.by_id(db_conn, process_id)
+ todo = cls(None, process, False, date)
+ todo.save(db_conn)
+ steps_tree = process.get_steps(db_conn)
+ for step_node in steps_tree.values():
+ if step_node.is_suppressed:
+ continue
+ todo.add_child(walk_steps(todo, step_node))
+ todo.save(db_conn)
+ return todo
@classmethod
def from_table_row(cls, db_conn: DatabaseConnection,
@@ -155,16 +182,6 @@ class Todo(BaseModel[int], ConditionsRelations):
"""Needed for super().save to save Processes as attributes."""
return self.process.id_
- @property
- def unsatisfied_dependencies(self) -> list[int]:
- """Return Process IDs of .process.explicit_steps not in .children."""
- unsatisfied = [s.step_process_id for s in self.process.explicit_steps
- if s.parent_step_id is None]
- for child_process_id in [c.process.id_ for c in self.children]:
- if child_process_id in unsatisfied:
- unsatisfied.remove(child_process_id)
- return unsatisfied
-
@property
def is_done(self) -> bool:
"""Wrapper around self._is_done so we can control its setter."""
@@ -201,23 +218,17 @@ class Todo(BaseModel[int], ConditionsRelations):
assert isinstance(effort_then, float)
return effort_then
- def adopt_from(self, todos: list[Todo]) -> bool:
- """As far as possible, fill unsatisfied dependencies from todos."""
- adopted = False
- for process_id in self.unsatisfied_dependencies:
- for todo in [t for t in todos if t.process.id_ == process_id
- and t not in self.children]:
- self.add_child(todo)
- adopted = True
- break
- return adopted
-
- def make_missing_children(self, db_conn: DatabaseConnection) -> None:
- """Fill unsatisfied dependencies with new Todos."""
- new_todos = self.__class__.create_with_children(
- db_conn, self.date, self.unsatisfied_dependencies)
- for todo in new_todos:
- self.add_child(todo)
+ @property
+ def has_doneness_in_path(self) -> bool:
+ """Check whether self is done or has any children that are."""
+ if self.is_done:
+ return True
+ for child in self.children:
+ if child.is_done:
+ return True
+ if child.has_doneness_in_path:
+ return True
+ return False
def get_step_tree(self, seen_todos: set[int]) -> TodoNode:
"""Return tree of depended-on Todos."""