home · contact · privacy
Refactor BaseModel.from_table_row in regards to VersionedAttributes.
authorChristian Heller <c.heller@plomlompom.de>
Mon, 17 Jun 2024 21:28:13 +0000 (23:28 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Mon, 17 Jun 2024 21:28:13 +0000 (23:28 +0200)
plomtask/conditions.py
plomtask/db.py
plomtask/processes.py

index d2559272cd876c5b071b59437998e3356409cd07..70365ce6a1d3d5eb4aa49077eb95e62cf1444bb0 100644 (file)
@@ -1,7 +1,5 @@
 """Non-doable elements of ProcessStep/Todo chains."""
 from __future__ import annotations
-from typing import Any
-from sqlite3 import Row
 from plomtask.db import DatabaseConnection, BaseModel
 from plomtask.versioned_attributes import VersionedAttribute
 from plomtask.exceptions import HandledException
@@ -21,17 +19,6 @@ class Condition(BaseModel[int]):
         self.description = VersionedAttribute(self, 'condition_descriptions',
                                               '')
 
-    @classmethod
-    def from_table_row(cls, db_conn: DatabaseConnection,
-                       row: Row | list[Any]) -> Condition:
-        """Build condition from row, including VersionedAttributes."""
-        condition = super().from_table_row(db_conn, row)
-        for name in ('title', 'description'):
-            table_name = f'condition_{name}s'
-            for row_ in db_conn.row_where(table_name, 'parent', row[0]):
-                getattr(condition, name).history_from_row(row_)
-        return condition
-
     def remove(self, db_conn: DatabaseConnection) -> None:
         """Remove from DB, with VersionedAttributes.
 
index 054060e13b7c1fa4aa27274efab13ff1c2828815..3917ce0558e3f7deb1f917a07eb5dd4df7b279e6 100644 (file)
@@ -369,8 +369,14 @@ class BaseModel(Generic[BaseModelId]):
                        # pylint: disable=unused-argument
                        db_conn: DatabaseConnection,
                        row: Row | list[Any]) -> BaseModelInstance:
-        """Make from DB row, update DB cache with it."""
+        """Make from DB row (sans relations), update DB cache with it."""
         obj = cls(*row)
+        assert obj.id_ is not None
+        for attr_name in cls.to_save_versioned:
+            attr = getattr(obj, attr_name)
+            table_name = attr.table_name
+            for row_ in db_conn.row_where(table_name, 'parent', obj.id_):
+                attr.history_from_row(row_)
         obj._cache()
         return obj
 
index 6df8eaf9132d7652e447931d59a2cc6a39d3043b..4ff90ef7457035a6e144fbaf0764d20258de7f77 100644 (file)
@@ -59,11 +59,7 @@ class Process(BaseModel[int], ConditionsRelations):
                        row: Row | list[Any]) -> Process:
         """Make from DB row, with dependencies."""
         process = super().from_table_row(db_conn, row)
-        assert isinstance(process.id_, int)
-        for name in ('title', 'description', 'effort'):
-            table = f'process_{name}s'
-            for row_ in db_conn.row_where(table, 'parent', process.id_):
-                getattr(process, name).history_from_row(row_)
+        assert process.id_ is not None
         for name in ('conditions', 'blockers', 'enables', 'disables'):
             table = f'process_{name}'
             assert isinstance(process.id_, int)