"""Individual days defined by their dates."""
table_name = 'days'
to_save = ['comment']
+ add_to_dict = ['todos']
can_create_by_id = True
def __init__(self, date: str, comment: str = '') -> None:
def __lt__(self, other: Day) -> bool:
return self.date < other.date
- @property
- def as_dict(self) -> dict[str, object]:
- """Return self as (json.dumps-coompatible) dict."""
- d = super().as_dict
- d['todos'] = [t.as_dict for t in self.todos]
- return d
-
@classmethod
def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]
) -> Day:
return day
@classmethod
- def by_id(cls, db_conn: DatabaseConnection, id_: str | None) -> Day:
+ def by_id_or_create(cls, db_conn: DatabaseConnection, id_: str | None
+ ) -> Day:
+ """Extend BaseModel.by_id to ensure date name translation."""
+ assert isinstance(id_, str)
+ return super().by_id_or_create(db_conn, valid_date(id_))
+
+ @classmethod
+ def by_id(cls, db_conn: DatabaseConnection, id_: str) -> Day:
"""Extend BaseModel.by_id checking for new/lost .todos."""
day = super().by_id(db_conn, id_)
- assert day.id_ is not None
if day.id_ in Todo.days_to_update:
Todo.days_to_update.remove(day.id_)
day.todos = Todo.by_date(db_conn, day.id_)