home · contact · privacy
Enable deletion of Todos.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 28 Apr 2024 23:02:33 +0000 (01:02 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 28 Apr 2024 23:02:33 +0000 (01:02 +0200)
plomtask/http.py
plomtask/todos.py
templates/todo.html
tests/todos.py

index 7e2b241aec468a8b6c29221b6f146a702b990883..2b41db82e00e5d4cfac862161fb352607d09dfbb 100644 (file)
@@ -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:
index 9b9bc0b95527a901bd5a6008e1707f719df74b06..3bd3491778d9c95c93bc34db8794c643e2069486 100644 (file)
@@ -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)
index 9debffc0ad96db9d661dde5fcd343e3554d1aba5..41a9eb1c876bafb1d2b4f485653b80a5445a947e 100644 (file)
@@ -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 %}
index 182bd5566bff159e152fa7156e90e42c8a3caa8d..b85f2d105f0f09e174f13de88506b309ccfc4f3d 100644 (file)
@@ -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."""