From: Christian Heller Date: Fri, 19 Apr 2024 06:08:06 +0000 (+0200) Subject: Further refactor Conditions handling. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/ledger?a=commitdiff_plain;h=c2004503dc42449f1fa129b8e56eeef0a6df4712;p=plomtask Further refactor Conditions handling. --- diff --git a/plomtask/conditions.py b/plomtask/conditions.py index 9a44200..cd147cb 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -66,3 +66,26 @@ class Condition(BaseModel): self.description.save(db_conn) assert isinstance(self.id_, int) db_conn.cached_conditions[self.id_] = self + + +class ConditionsRelations: + """Methods for handling relations to Conditions, for Todo and Process.""" + + def set_conditions(self, db_conn: DatabaseConnection, ids: list[int], + target: str = 'conditions') -> None: + """Set self.[target] to Conditions identified by ids.""" + target_list = getattr(self, target) + while len(target_list) > 0: + target_list.pop() + for id_ in ids: + target_list += [Condition.by_id(db_conn, id_)] + + def set_enables(self, db_conn: DatabaseConnection, + ids: list[int]) -> None: + """Set self.enables to Conditions identified by ids.""" + self.set_conditions(db_conn, ids, 'enables') + + def set_disables(self, db_conn: DatabaseConnection, + ids: list[int]) -> None: + """Set self.disables to Conditions identified by ids.""" + self.set_conditions(db_conn, ids, 'disables') diff --git a/plomtask/processes.py b/plomtask/processes.py index 0e8846d..eb43895 100644 --- a/plomtask/processes.py +++ b/plomtask/processes.py @@ -3,11 +3,11 @@ from __future__ import annotations from typing import Any, Set from plomtask.db import DatabaseConnection, BaseModel from plomtask.misc import VersionedAttribute -from plomtask.conditions import Condition +from plomtask.conditions import Condition, ConditionsRelations from plomtask.exceptions import NotFoundException, BadFormatException -class Process(BaseModel): +class Process(BaseModel, ConditionsRelations): """Template for, and metadata for, Todos, and their arrangements.""" table_name = 'processes' @@ -110,25 +110,6 @@ class Process(BaseModel): walk_steps(step_id, step_node) return steps - def set_conditions(self, db_conn: DatabaseConnection, ids: list[int], - trgt: str = 'conditions') -> None: - """Set self.[target] to Conditions identified by ids.""" - trgt_list = getattr(self, trgt) - while len(trgt_list) > 0: - trgt_list.pop() - for id_ in ids: - trgt_list += [Condition.by_id(db_conn, id_)] - - def set_enables(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.enables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'enables') - - def set_disables(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.disables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'disables') - def _add_step(self, db_conn: DatabaseConnection, id_: int | None, diff --git a/plomtask/todos.py b/plomtask/todos.py index ebe35ac..d060e23 100644 --- a/plomtask/todos.py +++ b/plomtask/todos.py @@ -4,12 +4,12 @@ from typing import Any from sqlite3 import Row from plomtask.db import DatabaseConnection, BaseModel from plomtask.processes import Process -from plomtask.conditions import Condition +from plomtask.conditions import Condition, ConditionsRelations from plomtask.exceptions import (NotFoundException, BadFormatException, HandledException) -class Todo(BaseModel): +class Todo(BaseModel, ConditionsRelations): """Individual actionable.""" # pylint: disable=too-many-instance-attributes @@ -134,25 +134,6 @@ class Todo(BaseModel): for condition in self.disables: condition.is_active = False - def set_disables(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.disables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'disables') - - def set_enables(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.enables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'enables') - - def set_conditions(self, db_conn: DatabaseConnection, ids: list[int], - target: str = 'conditions') -> None: - """Set self.[target] to Conditions identified by ids.""" - target_list = getattr(self, target) - while len(target_list) > 0: - target_list.pop() - for id_ in ids: - target_list += [Condition.by_id(db_conn, id_)] - def add_child(self, child: Todo) -> None: """Add child to self.children, guard against recursion""" def walk_steps(node: Todo) -> None: