home · contact · privacy
Further refactor Conditions handling.
authorChristian Heller <c.heller@plomlompom.de>
Fri, 19 Apr 2024 06:08:06 +0000 (08:08 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Fri, 19 Apr 2024 06:08:06 +0000 (08:08 +0200)
plomtask/conditions.py
plomtask/processes.py
plomtask/todos.py

index 9a442000a99befa563582346826244739bf13ae7..cd147cb79cafcfd81f4672fae0a0b6296201ff6a 100644 (file)
@@ -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')
index 0e8846d3f8591342d87c860f757ec9c2c8c43d07..eb438958d4ac7748426116664032cae18f73a5dc 100644 (file)
@@ -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,
index ebe35ac56bb78af42a10644c9209b34778527c0e..d060e230381d69dc53cacde6ab28fc37e6afefe6 100644 (file)
@@ -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: