From 4c0b3ff23b3d9fc526dfaa4803eb167d64c32181 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Wed, 15 May 2024 04:10:27 +0200 Subject: [PATCH] On POSTing new Todos on Day view, ensure possible adoptions within them. --- plomtask/http.py | 15 +++++++++++---- plomtask/todos.py | 5 ++++- templates/day.html | 2 +- tests/todos.py | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 6 deletions(-) diff --git a/plomtask/http.py b/plomtask/http.py index a1f85fd..d411124 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -196,14 +196,21 @@ class TaskHandler(BaseHTTPRequestHandler): day = Day.by_id(self.conn, date, create=True) day.comment = self.form_data.get_str('comment') day.save(self.conn) - existing_todos = Todo.by_date(self.conn, date) + 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) - todo.adopt_from(existing_todos) - todo.make_missing_children(self.conn) - 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) for todo_id in self.form_data.get_all_int('done'): todo = Todo.by_id(self.conn, todo_id) todo.is_done = True diff --git a/plomtask/todos.py b/plomtask/todos.py index e42c484..4e3a4db 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -126,13 +126,16 @@ class Todo(BaseModel[int], ConditionsRelations): for condition in self.disables: condition.is_active = False - def adopt_from(self, todos: list[Todo]) -> None: + 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.""" diff --git a/templates/day.html b/templates/day.html index f13eb5c..167b703 100644 --- a/templates/day.html +++ b/templates/day.html @@ -38,7 +38,7 @@ td.todo_line { {% endfor %} -> - + {% for i in range(indent) %}  {% endfor %} + {% if node.seen %}({% endif %}{{node.todo.process.title.newest|e}}{% if node.seen %}){% endif %} diff --git a/tests/todos.py b/tests/todos.py index a8219fa..5e9f4b8 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -324,6 +324,39 @@ class TestsWithServer(TestCaseWithServer): self.assertEqual(todo2.children, [todo1]) self.assertEqual(todo2.parents, []) + def test_do_POST_day_todo_multiple(self) -> None: + """Test multiple Todos can be posted to Day view.""" + form_data = self.post_process() + form_data = self.post_process(2) + form_data = {'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.process.id_, 1) + self.assertEqual(todo2.process.id_, 2) + + def test_do_POST_day_todo_multiple_inner_adoption(self) -> None: + """Test multiple Todos can be posted to Day view w. inner adoption.""" + form_data = self.post_process() + form_data = self.post_process(2, form_data | {'new_top_step': 1}) + form_data = {'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 = {'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, []) + def test_do_GET_todo(self) -> None: """Test GET /todo response codes.""" self.post_process() -- 2.30.2