From: Christian Heller Date: Mon, 20 May 2024 23:50:36 +0000 (+0200) Subject: For Todos, on Save check for auto-deletion by .effort < 0, and on removal check if... X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bdb.prefix%7D%7D/balance?a=commitdiff_plain;h=31b778276bb7297151f1c90098cc50842c9c138f;p=plomtask For Todos, on Save check for auto-deletion by .effort < 0, and on removal check if nothing worth preserving would be lost. --- diff --git a/plomtask/todos.py b/plomtask/todos.py index 0fea234..b3d50e9 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -105,6 +105,15 @@ class Todo(BaseModel[int], ConditionsRelations): return False return True + @property + def is_deletable(self) -> bool: + """Decide whether self be deletable (not if preserve-worthy values).""" + if self.comment: + return False + if self.effort and self.effort >= 0: + return False + return True + @property def process_id(self) -> int | str | None: """Needed for super().save to save Processes as attributes.""" @@ -201,8 +210,17 @@ class Todo(BaseModel[int], ConditionsRelations): self.children.remove(child) child.parents.remove(self) + def save(self, db_conn: DatabaseConnection) -> None: + """On save calls, also check if auto-deletion by effort < 0.""" + if self.effort and self.effort < 0 and self.is_deletable: + self.remove(db_conn) + return + super().save(db_conn) + def remove(self, db_conn: DatabaseConnection) -> None: """Remove from DB, including relations.""" + if not self.is_deletable: + raise HandledException('Cannot remove non-deletable Todo.') children_to_remove = self.children[:] parents_to_remove = self.parents[:] for child in children_to_remove: diff --git a/templates/day.html b/templates/day.html index 4c77705..9af3754 100644 --- a/templates/day.html +++ b/templates/day.html @@ -33,15 +33,22 @@ td.todo_line { {% macro show_node_undone(node, indent) %} {% if not node.todo.is_done %} +{% if not node.seen %} +{% endif %} {% for condition in conditions_present %} {% if condition in node.todo.conditions %}{% if not condition.is_active %}O{% endif %}{% endif %} {% endfor %} -> - +{% if node.seen %} + +{% if node.todo.effort %}{{ node.todo.effort }}{% endif %} +{% else %} + +{% endif %} {% for i in range(indent) %}  {% endfor %} + {% if node.seen %}({% endif %}{{node.todo.process.title.newest|e}}{% if node.seen %}){% endif %} @@ -52,7 +59,13 @@ td.todo_line { {% if condition in node.todo.enables %}+{% elif condition in node.todo.disables %}!{% endif %} {% endfor %} - + +{% if node.seen %} +{{node.todo.comment|e}} +{% else %} + +{% endif %} + {% endif %} diff --git a/tests/todos.py b/tests/todos.py index 059bd9f..6ce5d69 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -218,6 +218,26 @@ class TestsWithDB(TestCaseWithDB): Todo.by_id(self.db_conn, todo_1.id_) self.assertEqual(todo_0.children, []) self.assertEqual(todo_2.parents, []) + todo_2.comment = 'foo' + with self.assertRaises(HandledException): + todo_2.remove(self.db_conn) + todo_2.comment = '' + todo_2.effort = 5 + with self.assertRaises(HandledException): + todo_2.remove(self.db_conn) + + def test_Todo_autoremoval(self) -> None: + """"Test automatic removal for Todo.effort < 0.""" + todo_1 = Todo(None, self.proc, False, self.date1) + todo_1.save(self.db_conn) + todo_1.comment = 'foo' + todo_1.effort = -0.1 + todo_1.save(self.db_conn) + Todo.by_id(self.db_conn, todo_1.id_) + todo_1.comment = '' + todo_1.save(self.db_conn) + with self.assertRaises(NotFoundException): + Todo.by_id(self.db_conn, todo_1.id_) class TestsWithServer(TestCaseWithServer):