X-Git-Url: https://plomlompom.com/repos/%7B%7B%20web_path%20%7D%7D/decks/%7B%7Bdeck_id%7D%7D/cards/%7B%7B%20card_id%20%7D%7D/delete?a=blobdiff_plain;f=plomtask%2Ftodos.py;h=f1d98ad3a0719c3027bc55620b5f6a97be2ab628;hb=6b329a28bb4aec8d1846f5cc5402ed6fca5eb3da;hp=1b6b740c97930862b8867d5229e01685713e6595;hpb=a4ca74f81ae42abe27cf6dbab7ef18c850db72c2;p=plomtask diff --git a/plomtask/todos.py b/plomtask/todos.py index 1b6b740..f1d98ad 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -17,30 +17,43 @@ class Todo: self.is_done = is_done self.day = day - def __eq__(self, other: object) -> bool: - return isinstance(other, self.__class__) and self.id_ == other.id_ - @classmethod - def from_table_row(cls, row: Row, db_conn: DatabaseConnection) -> Todo: - """Make Todo from database row.""" - return cls(id_=row[0], + 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], 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.""" + 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_}') @classmethod def by_date(cls, db_conn: DatabaseConnection, date: str) -> list[Todo]: """Collect all Todos for Day of date.""" todos = [] - for row in db_conn.exec('SELECT * FROM todos WHERE day = ?', (date,)): - todos += [cls.from_table_row(row, db_conn)] + for row in db_conn.exec('SELECT id FROM todos WHERE day = ?', (date,)): + todos += [cls.by_id(db_conn, row[0])] return todos def save(self, db_conn: DatabaseConnection) -> None: - """Write self to DB.""" + """Write self 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