From a99b13325a21042825450d2497ddf61f8c5c3644 Mon Sep 17 00:00:00 2001
From: Christian Heller <c.heller@plomlompom.de>
Date: Mon, 29 Apr 2024 01:02:33 +0200
Subject: [PATCH] Enable deletion of Todos.

---
 plomtask/http.py    |  4 ++++
 plomtask/todos.py   | 12 ++++++++++++
 templates/todo.html |  7 ++++++-
 tests/todos.py      | 25 ++++++++++++++++++++++---
 4 files changed, 44 insertions(+), 4 deletions(-)

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: <input name="adopt" list="todo_candidates" autocomplete="off" />
 <option value="{{candidate.id_}}">{{candidate.process.title.newest|e}} ({{candidate.id_}})</option>
 {% endfor %}
 </datalist>
-<input type="submit" value="OK" />
+
+<input class="btn-harmless" type="submit" name="update" value="update" />
+<div class="btn-to-right">
+<input class="btn-dangerous" type="submit" name="delete" value="delete" />
+</div>
+
 </form
 {% endblock %}
diff --git a/tests/todos.py b/tests/todos.py
index 182bd55..b85f2d1 100644
--- a/tests/todos.py
+++ b/tests/todos.py
@@ -266,6 +266,22 @@ class TestsWithDB(TestCaseWithDB):
         retrieved_todo.is_done = False
         self.assertEqual(todo.is_done, False)
 
+    def test_Todo_remove(self) -> 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."""
-- 
2.30.2