From: Christian Heller Date: Mon, 17 Jun 2024 21:28:13 +0000 (+0200) Subject: Refactor BaseModel.from_table_row in regards to VersionedAttributes. X-Git-Url: https://plomlompom.com/repos/unset_cookie?a=commitdiff_plain;h=eba83bdbf91206ad3af19810f27ca6e24e484555;p=plomtask Refactor BaseModel.from_table_row in regards to VersionedAttributes. --- diff --git a/plomtask/conditions.py b/plomtask/conditions.py index d255927..70365ce 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -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. diff --git a/plomtask/db.py b/plomtask/db.py index 054060e..3917ce0 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -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 diff --git a/plomtask/processes.py b/plomtask/processes.py index 6df8eaf..4ff90ef 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -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)