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:
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:
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)
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."""