home · contact · privacy
Refactor .as_dict integration of Process.explicit_steps, Day.todos.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 21 Jun 2024 02:58:56 +0000 (04:58 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 21 Jun 2024 02:58:56 +0000 (04:58 +0200)
plomtask/days.py
plomtask/db.py
plomtask/processes.py

index 68cf989643924a42b1ef8b48cc04a63434efe6b9..92e44b2330b27caf7da9a3fce9ffca8aff3e7bc5 100644 (file)
@@ -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,15 +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
-        assert isinstance(d['_library'], dict)
-        d['todos'] = [t.as_dict_into_reference(d['_library'])
-                      for t in self.todos]
-        return d
-
     @classmethod
     def from_table_row(cls, db_conn: DatabaseConnection, row: Row | list[Any]
                        ) -> Day:
index b3f1db00986b1142f5f31be34060864840ab5bdc..e95c27d52133ed16d448f221f9f6df89efd25fde 100644 (file)
@@ -235,6 +235,7 @@ class BaseModel(Generic[BaseModelId]):
     to_save: list[str] = []
     to_save_versioned: list[str] = []
     to_save_relations: list[tuple[str, str, str, int]] = []
+    add_to_dict: list[str] = []
     id_: None | BaseModelId
     cache_: dict[BaseModelId, Self]
     to_search: list[str] = []
@@ -295,6 +296,9 @@ class BaseModel(Generic[BaseModelId]):
             for rel in getattr(self, attr_name):
                 l += [rel.as_dict_into_reference(library)]
             d[attr_name] = l
+        for k in self.add_to_dict:
+            d[k] = [x.as_dict_into_reference(library)
+                    for x in getattr(self, k)]
         return d
 
     def as_dict_into_reference(self,
index ebe781e8fb83e09699e38532c0c2c7b3a9a2e1f2..36158996595dccfad678730eb29a50c422c26b9d 100644 (file)
@@ -33,6 +33,7 @@ class Process(BaseModel[int], ConditionsRelations):
                          ('process_disables', 'process', 'disables', 0),
                          ('process_step_suppressions', 'process',
                           'suppressed_steps', 0)]
+    add_to_dict = ['explicit_steps']
     to_search = ['title.newest', 'description.newest']
     can_create_by_id = True
 
@@ -47,15 +48,6 @@ class Process(BaseModel[int], ConditionsRelations):
         self.calendarize = calendarize
         self.n_owners: int | None = None  # only set by from_table_row
 
-    @property
-    def as_dict(self) -> dict[str, object]:
-        """Return self as (json.dumps-coompatible) dict."""
-        d = super().as_dict
-        assert isinstance(d['_library'], dict)
-        d['explicit_steps'] = [s.as_dict_into_reference(d['_library'])
-                               for s in self.explicit_steps]
-        return d
-
     @classmethod
     def from_table_row(cls, db_conn: DatabaseConnection,
                        row: Row | list[Any]) -> Process: