home · contact · privacy
Slightly improve and re-organize Condition tests.
[plomtask] / plomtask / days.py
index afe4a01be6f509a8b624da7c45650500a96805e2..23201301bbe792042a361d3f970415c622d80627 100644 (file)
@@ -12,6 +12,8 @@ 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:
         id_ = valid_date(date)
@@ -33,13 +35,16 @@ class Day(BaseModel[str]):
         return day
 
     @classmethod
-    def by_id(cls,
-              db_conn: DatabaseConnection, id_: str | None,
-              create: bool = False,
-              ) -> Day:
-        """Extend BaseModel.by_id checking for new/lost .todos."""
-        day = super().by_id(db_conn, id_, create)
-        assert day.id_ is not None
+    def by_id(cls, db_conn: DatabaseConnection, id_: str) -> Day:
+        """Extend BaseModel.by_id
+
+        Checks Todo.days_to_update if we need to a retrieved Day's .todos,
+        and also ensures we're looking for proper dates and not strings like
+        "yesterday" by enforcing the valid_date translation.
+        """
+        assert isinstance(id_, str)
+        possibly_translated_date = valid_date(id_)
+        day = super().by_id(db_conn, possibly_translated_date)
         if day.id_ in Todo.days_to_update:
             Todo.days_to_update.remove(day.id_)
             day.todos = Todo.by_date(db_conn, day.id_)