From: Christian Heller Date: Sun, 28 Apr 2024 23:02:33 +0000 (+0200) Subject: Enable deletion of Todos. X-Git-Url: https://plomlompom.com/repos/index.html?a=commitdiff_plain;h=a99b13325a21042825450d2497ddf61f8c5c3644;p=plomtask Enable deletion of Todos. --- diff --git a/plomtask/http.py b/plomtask/http.py index 7e2b241..2b41db8 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -212,6 +212,10 @@ class TaskHandler(BaseHTTPRequestHandler): def do_POST_todo(self) -> str: """Update Todo and its children.""" id_ = self.params.get_int('id') + for _ in self.form_data.get_all_str('delete'): + todo = Todo .by_id(self.conn, id_) + todo.remove(self.conn) + return '/' todo = Todo.by_id(self.conn, id_) adopted_child_ids = self.form_data.get_all_int('adopt') for child in todo.children: diff --git a/plomtask/todos.py b/plomtask/todos.py index 9b9bc0b..3bd3491 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -230,3 +230,15 @@ class Todo(BaseModel[int], ConditionsRelations): [[c.id_] for c in self.enables]) db_conn.rewrite_relations('todo_disables', 'todo', self.id_, [[c.id_] for c in self.disables]) + + def remove(self, db_conn: DatabaseConnection) -> None: + """Remove from DB, including relations.""" + assert isinstance(self.id_, int) + for child in self.children: + self.remove_child(child) + for parent in self.parents: + parent.remove_child(self) + db_conn.delete_where('todo_conditions', 'todo', self.id_) + db_conn.delete_where('todo_enables', 'todo', self.id_) + db_conn.delete_where('todo_disables', 'todo', self.id_) + super().remove(db_conn) diff --git a/templates/todo.html b/templates/todo.html index 9debffc..41a9eb1 100644 --- a/templates/todo.html +++ b/templates/todo.html @@ -75,6 +75,11 @@ adopt: {% endfor %} - + + +
+ +
+ None: + """Test removal.""" + todo_1 = Todo(None, self.proc, False, self.date1) + todo_1.save(self.db_conn) + todo_0 = Todo(None, self.proc, False, self.date1) + todo_0.save(self.db_conn) + todo_0.add_child(todo_1) + todo_2 = Todo(None, self.proc, False, self.date1) + todo_2.save(self.db_conn) + todo_1.add_child(todo_2) + todo_1.remove(self.db_conn) + with self.assertRaises(NotFoundException): + Todo.by_id(self.db_conn, todo_1.id_) + self.assertEqual(todo_0.children, []) + self.assertEqual(todo_2.parents, []) + class TestsWithServer(TestCaseWithServer): """Tests against our HTTP server/handler (and database).""" @@ -298,9 +314,9 @@ class TestsWithServer(TestCaseWithServer): def test_do_POST_todo(self) -> None: """Test POST /todo.""" - def post_and_reload(form_data: dict[str, object], - status: int = 302) -> Todo: - self.check_post(form_data, '/todo?id=1', status) + def post_and_reload(form_data: dict[str, object], status: int = 302, + redir_url: str = '/todo?id=1') -> Todo: + self.check_post(form_data, '/todo?id=1', status, redir_url) return Todo.by_date(self.db_conn, '2024-01-01')[0] # test minimum form_data = {'title': '', 'description': '', 'effort': 1} @@ -356,6 +372,9 @@ class TestsWithServer(TestCaseWithServer): self.assertEqual(todo1.parents, []) self.assertEqual(todo2.children, []) self.assertEqual(todo2.parents, []) + # test todo1 deletion + form_data = {'delete': ''} + todo1 = post_and_reload(form_data, 302, '/') def test_do_POST_day_todo_adoption(self) -> None: """Test Todos posted to Day view may adopt existing Todos."""