- """Write self and children to DB and its cache."""
- if self.process.id_ is None:
- raise NotFoundException('Process of Todo without ID (not saved?)')
- cursor = db_conn.exec('REPLACE INTO todos VALUES (?,?,?,?)',
- (self.id_, self.process.id_,
- self.is_done, self.day.date))
- self.id_ = cursor.lastrowid
- assert self.id_ is not None
- db_conn.cached_todos[self.id_] = self
- db_conn.exec('DELETE FROM todo_children WHERE parent = ?',
- (self.id_,))
- for child in self.children:
- db_conn.exec('INSERT INTO todo_children VALUES (?, ?)',
- (self.id_, child.id_))
- db_conn.exec('DELETE FROM todo_fulfills WHERE todo = ?', (self.id_,))
- for condition in self.fulfills:
- if condition.id_ is None:
- raise NotFoundException('Fulfilled Condition of Todo '
- 'without ID (not saved?)')
- db_conn.exec('INSERT INTO todo_fulfills VALUES (?, ?)',
- (self.id_, condition.id_))
- db_conn.exec('DELETE FROM todo_undoes WHERE todo = ?', (self.id_,))
- for condition in self.undoes:
- if condition.id_ is None:
- raise NotFoundException('Undone Condition of Todo '
- 'without ID (not saved?)')
- db_conn.exec('INSERT INTO todo_undoes VALUES (?, ?)',
- (self.id_, condition.id_))
- db_conn.exec('DELETE FROM todo_conditions WHERE todo = ?', (self.id_,))
- for condition in self.conditions:
- if condition.id_ is None:
- raise NotFoundException('Condition of Todo '
- 'without ID (not saved?)')
- db_conn.exec('INSERT INTO todo_conditions VALUES (?, ?)',
- (self.id_, condition.id_))
+ """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:
+ self.remove_child(child)
+ for parent in parents_to_remove:
+ parent.remove_child(self)
+ super().remove(db_conn)