"""Actionables."""
from __future__ import annotations
+from typing import Any
from sqlite3 import Row
from plomtask.db import DatabaseConnection, BaseModel
-from plomtask.days import Day
from plomtask.processes import Process
from plomtask.conditions import Condition
from plomtask.exceptions import (NotFoundException, BadFormatException,
# pylint: disable=too-many-instance-attributes
- name = 'Todo'
table_name = 'todos'
to_save = ['process_id', 'is_done', 'date']
def __init__(self, id_: int | None, process: Process,
- is_done: bool, day: Day) -> None:
+ is_done: bool, date: str) -> None:
self.set_int_id(id_)
self.process = process
self._is_done = is_done
- self.day = day
+ self.date = date
self.children: list[Todo] = []
self.parents: list[Todo] = []
self.conditions: list[Condition] = []
self.undoes = process.undoes[:]
@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=bool(row[2]),
- day=Day.by_date(db_conn, row[3]))
- assert isinstance(todo.id_, int)
- db_conn.cached_todos[todo.id_] = todo
+ def from_table_row(cls, db_conn: DatabaseConnection,
+ row: Row | list[Any]) -> Todo:
+ """Make from DB row, write to DB cache."""
+ if row[1] == 0:
+ raise NotFoundException('calling Todo of '
+ 'unsaved Process')
+ row_as_list = list(row)
+ row_as_list[1] = Process.by_id(db_conn, row[1])
+ todo = super().from_table_row(db_conn, row_as_list)
+ assert isinstance(todo, Todo)
return todo
@classmethod
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_]
+ if id_:
+ todo, from_cache = super()._by_id(db_conn, 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_}')
+ todo, from_cache = None, False
+ if todo is None:
+ raise NotFoundException(f'Todo of ID not found: {id_}')
+ if not from_cache:
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 todo FROM todo_fulfills '
'WHERE condition = ?', (condition.id_,)):
todo = cls.by_id(db_conn, row[0])
- if todo.day.date == date:
+ if todo.date == date:
enablers += [todo]
return enablers
for row in db_conn.exec('SELECT todo FROM todo_undoes '
'WHERE condition = ?', (condition.id_,)):
todo = cls.by_id(db_conn, row[0])
- if todo.day.date == date:
+ if todo.date == date:
disablers += [todo]
return disablers
"""Return ID of tasked Process."""
return self.process.id_
- @property
- def date(self) -> str:
- """Return date of used Day."""
- return self.day.date
-
@property
def is_done(self) -> bool:
"""Wrapper around self._is_done so we can control its setter."""
self.save_core(db_conn)
assert isinstance(self.id_, int)
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_))
+ db_conn.rewrite_relations('todo_children', 'parent', self.id_,
+ [[c.id_] for c in self.children])
+ db_conn.rewrite_relations('todo_conditions', 'todo', self.id_,
+ [[c.id_] for c in self.conditions])
+ db_conn.rewrite_relations('todo_fulfills', 'todo', self.id_,
+ [[c.id_] for c in self.fulfills])
+ db_conn.rewrite_relations('todo_undoes', 'todo', self.id_,
+ [[c.id_] for c in self.undoes])