X-Git-Url: https://plomlompom.com/repos/feed.xml?a=blobdiff_plain;f=plomtask%2Fdays.py;h=7e6a5cce9177e2450107a111262482b8e5c23c3a;hb=ef4dfff9ff3002c11ccfef4190999a5e4e513606;hp=0bd942cbd8804b8900a3271b9f4f0e4881f105ec;hpb=8f28c8c685fa91b9cbabb4b424da4091e52058cf;p=plomtask diff --git a/plomtask/days.py b/plomtask/days.py index 0bd942c..7e6a5cc 100644 --- a/plomtask/days.py +++ b/plomtask/days.py @@ -12,6 +12,7 @@ class Day(BaseModel[str]): """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: @@ -24,13 +25,6 @@ class Day(BaseModel[str]): 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: @@ -40,6 +34,13 @@ class Day(BaseModel[str]): day.todos = Todo.by_date(db_conn, day.id_) return day + @classmethod + 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."""