From: Christian Heller Date: Thu, 30 May 2024 01:28:35 +0000 (+0200) Subject: Nest Todo creation by adopting and making children as needed. X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/%7B%7Bprefix%7D%7D/coming?a=commitdiff_plain;h=3346c7fab2fda82b290d245798f4537b43abb012;p=plomtask Nest Todo creation by adopting and making children as needed. --- diff --git a/plomtask/http.py b/plomtask/http.py index f5e24ed..dbc78eb 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -256,21 +256,8 @@ class TaskHandler(BaseHTTPRequestHandler): day = Day.by_id(self.conn, date, create=True) day.comment = self.form_data.get_str('day_comment') day.save(self.conn) - new_todos = [] - for process_id in self.form_data.get_all_int('new_todo'): - process = Process.by_id(self.conn, process_id) - todo = Todo(None, process, False, day.date) - todo.save(self.conn) - new_todos += [todo] - adopted = True - while adopted: - adopted = False - existing_todos = Todo.by_date(self.conn, date) - for todo in new_todos: - if todo.adopt_from(existing_todos): - adopted = True - todo.make_missing_children(self.conn) - todo.save(self.conn) + Todo.create_with_children(self.conn, date, + self.form_data.get_all_int('new_todo')) done_ids = self.form_data.get_all_int('done') comments = self.form_data.get_all_str('comment') efforts = self.form_data.get_all_str('effort') diff --git a/plomtask/todos.py b/plomtask/todos.py index a7af99f..bffa23a 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -58,6 +58,27 @@ class Todo(BaseModel[int], ConditionsRelations): self.enables = self.process.enables[:] self.disables = self.process.disables[:] + @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 + @classmethod def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]) -> Todo: @@ -167,10 +188,9 @@ class Todo(BaseModel[int], ConditionsRelations): def make_missing_children(self, db_conn: DatabaseConnection) -> None: """Fill unsatisfied dependencies with new Todos.""" - for process_id in self.unsatisfied_dependencies: - process = Process.by_id(db_conn, process_id) - todo = self.__class__(None, process, False, self.date) - todo.save(db_conn) + new_todos = self.__class__.create_with_children( + db_conn, self.date, self.unsatisfied_dependencies) + for todo in new_todos: self.add_child(todo) def get_step_tree(self, seen_todos: set[int]) -> TodoNode: diff --git a/tests/todos.py b/tests/todos.py index 6ce5d69..4ba5a1c 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -190,8 +190,6 @@ class TestsWithDB(TestCaseWithDB): self.assertEqual(todo_3.children, []) self.assertEqual(todo_4.process, proc3) self.assertEqual(todo_4.parents, [todo_2]) - # test .make_missing_children doesn't further than top-level - self.assertEqual(todo_4.children, []) # test .make_missing_children lower down the tree todo_4.make_missing_children(self.db_conn) todo_5 = Todo.by_id(self.db_conn, 5) @@ -349,25 +347,54 @@ class TestsWithServer(TestCaseWithServer): def test_do_POST_day_todo_multiple_inner_adoption(self) -> None: """Test multiple Todos can be posted to Day view w. inner adoption.""" + + def key_order_func(t: Todo) -> int: + assert isinstance(t.process.id_, int) + return t.process.id_ + + def check_adoption(date: str, new_todo: list[int]) -> None: + form_data = {'day_comment': '', 'new_todo': new_todo} + self.check_post(form_data, f'/day?date={date}', 302) + day_todos = Todo.by_date(self.db_conn, date) + day_todos.sort(key=key_order_func) + todo1 = day_todos[0] + todo2 = day_todos[1] + self.assertEqual(todo1.children, []) + self.assertEqual(todo1.parents, [todo2]) + self.assertEqual(todo2.children, [todo1]) + self.assertEqual(todo2.parents, []) + + def check_nesting_adoption(process_id: int, date: str, + new_top_steps: list[int]) -> None: + result_reversed = new_top_steps[1] < new_top_steps[0] + form_data = self.post_process() + form_data = self.post_process(process_id, + form_data | + {'new_top_step': new_top_steps}) + form_data = {'day_comment': '', 'new_todo': [process_id]} + self.check_post(form_data, f'/day?date={date}', 302) + day_todos = Todo.by_date(self.db_conn, date) + day_todos.sort(key=key_order_func, reverse=True) + self.assertEqual(len(day_todos), 3) + todo1 = day_todos[0] # process of process_id + todo2 = day_todos[1] # process 2 + todo3 = day_todos[2] # process 1 + if result_reversed: + self.assertEqual(todo1.children, [todo2, todo3]) + else: + self.assertEqual(todo1.children, [todo3, todo2]) + self.assertEqual(todo1.parents, []) + self.assertEqual(todo2.children, [todo3]) + self.assertEqual(todo2.parents, [todo1]) + self.assertEqual(todo3.children, []) + self.assertEqual(todo3.parents, [todo2, todo1]) + form_data = self.post_process() form_data = self.post_process(2, form_data | {'new_top_step': 1}) - form_data = {'day_comment': '', 'new_todo': [1, 2]} - self.check_post(form_data, '/day?date=2024-01-01', 302) - todo1 = Todo.by_date(self.db_conn, '2024-01-01')[0] - todo2 = Todo.by_date(self.db_conn, '2024-01-01')[1] - self.assertEqual(todo1.children, []) - self.assertEqual(todo1.parents, [todo2]) - self.assertEqual(todo2.children, [todo1]) - self.assertEqual(todo2.parents, []) - # check process ID order does not affect end result - form_data = {'day_comment': '', 'new_todo': [2, 1]} - self.check_post(form_data, '/day?date=2024-01-02', 302) - todo1 = Todo.by_date(self.db_conn, '2024-01-02')[1] - todo2 = Todo.by_date(self.db_conn, '2024-01-02')[0] - self.assertEqual(todo1.children, []) - self.assertEqual(todo1.parents, [todo2]) - self.assertEqual(todo2.children, [todo1]) - self.assertEqual(todo2.parents, []) + check_adoption('2024-01-01', [1, 2]) + check_adoption('2024-01-02', [2, 1]) + check_nesting_adoption(3, '2024-01-03', [1, 2]) + check_nesting_adoption(4, '2024-01-04', [2, 1]) def test_do_POST_day_todo_doneness(self) -> None: """Test Todo doneness can be posted to Day view."""