From 5a393ec6a77d8f1040763ffa3e9e908f38a0e517 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Fri, 21 Jun 2024 04:58:56 +0200 Subject: [PATCH] Refactor .as_dict integration of Process.explicit_steps, Day.todos. --- plomtask/days.py | 10 +--------- plomtask/db.py | 4 ++++ plomtask/processes.py | 10 +--------- 3 files changed, 6 insertions(+), 18 deletions(-) diff --git a/plomtask/days.py b/plomtask/days.py index 68cf989..92e44b2 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,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: diff --git a/plomtask/db.py b/plomtask/db.py index b3f1db0..e95c27d 100644 --- a/plomtask/db.py +++ b/plomtask/db.py @@ -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, diff --git a/plomtask/processes.py b/plomtask/processes.py index ebe781e..3615899 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -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: -- 2.30.2