from plomtask.db import DatabaseConnection
from plomtask.days import Day
from plomtask.processes import Process
-from plomtask.exceptions import NotFoundException
+from plomtask.exceptions import (NotFoundException, BadFormatException,
+ HandledException)
class Todo:
is_done: bool, day: Day) -> None:
self.id_ = id_
self.process = process
- self.is_done = is_done
+ self._is_done = is_done
self.day = day
+ self.children: list[Todo] = []
+ self.parents: list[Todo] = []
@classmethod
def from_table_row(cls, db_conn: DatabaseConnection, row: Row) -> Todo:
"""Make Todo from database row, write to DB cache."""
todo = cls(id_=row[0],
process=Process.by_id(db_conn, row[1]),
- is_done=row[2],
+ is_done=bool(row[2]),
day=Day.by_date(db_conn, row[3]))
assert todo.id_ is not None
db_conn.cached_todos[todo.id_] = todo
return todo
@classmethod
- def by_id(cls, db_conn: DatabaseConnection, id_: int) -> Todo:
- """Get Todo of .id_=id_ – from DB cache if possible."""
+ def by_id(cls, db_conn: DatabaseConnection, id_: int | None) -> Todo:
+ """Get Todo of .id_=id_ and children (from DB cache if possible)."""
if id_ in db_conn.cached_todos.keys():
todo = db_conn.cached_todos[id_]
- assert isinstance(todo, Todo)
- return todo
- for row in db_conn.exec('SELECT * FROM todos WHERE id = ?', (id_,)):
- return cls.from_table_row(db_conn, row)
- raise NotFoundException(f'Todo of ID not found: {id_}')
+ else:
+ todo = None
+ for row in db_conn.exec('SELECT * FROM todos WHERE id = ?',
+ (id_,)):
+ todo = cls.from_table_row(db_conn, row)
+ break
+ if todo is None:
+ raise NotFoundException(f'Todo of ID not found: {id_}')
+ for row in db_conn.exec('SELECT child FROM todo_children '
+ 'WHERE parent = ?', (id_,)):
+ todo.children += [cls.by_id(db_conn, row[0])]
+ for row in db_conn.exec('SELECT parent FROM todo_children '
+ 'WHERE child = ?', (id_,)):
+ todo.parents += [cls.by_id(db_conn, row[0])]
+ assert isinstance(todo, Todo)
+ return todo
@classmethod
def by_date(cls, db_conn: DatabaseConnection, date: str) -> list[Todo]:
todos += [cls.by_id(db_conn, row[0])]
return todos
+ @property
+ def is_doable(self) -> bool:
+ """Decide whether .is_done can be set to True based on children's."""
+ for child in self.children:
+ if not child.is_done:
+ return False
+ return True
+
+ @property
+ def is_done(self) -> bool:
+ """Wrapper around self._is_done so we can control its setter."""
+ return self._is_done
+
+ @is_done.setter
+ def is_done(self, value: bool) -> None:
+ if value != self.is_done and not self.is_doable:
+ raise BadFormatException('cannot change doneness of undoable Todo')
+ self._is_done = value
+
+ def add_child(self, child: Todo) -> None:
+ """Add child to self.children, guard against recursion"""
+ def walk_steps(node: Todo) -> None:
+ if node.id_ == self.id_:
+ raise BadFormatException('bad child choice causes recursion')
+ for child in node.children:
+ walk_steps(child)
+ if self.id_ is None:
+ raise HandledException('Can only add children to saved Todos.')
+ if child.id_ is None:
+ raise HandledException('Can only add saved children to Todos.')
+ if child in self.children:
+ raise BadFormatException('cannot adopt same child twice')
+ walk_steps(child)
+ self.children += [child]
+ child.parents += [self]
+
def save(self, db_conn: DatabaseConnection) -> None:
- """Write self to DB and its cache."""
+ """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_ = 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_))