home · contact · privacy
Slightly reduce the do_POST_todo code.
[plomtask] / plomtask / conditions.py
index 15dcb9df623c60378485632ce3bebc4c30f03d47..8d4160424423cf6ddfccb06b5b04d77e2bea4b15 100644 (file)
@@ -8,8 +8,8 @@ from plomtask.exceptions import HandledException
 class Condition(BaseModel[int]):
     """Non-Process dependency for ProcessSteps and Todos."""
     table_name = 'conditions'
-    to_save = ['is_active']
-    to_save_versioned = ['title', 'description']
+    to_save_simples = ['is_active']
+    versioned_defaults = {'title': 'UNNAMED', 'description': ''}
     to_search = ['title.newest', 'description.newest']
     can_create_by_id = True
     sorters = {'is_active': lambda c: c.is_active,
@@ -18,9 +18,10 @@ class Condition(BaseModel[int]):
     def __init__(self, id_: int | None, is_active: bool = False) -> None:
         super().__init__(id_)
         self.is_active = is_active
-        self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
-        self.description = VersionedAttribute(self, 'condition_descriptions',
-                                              '')
+        for name in ['title', 'description']:
+            attr = VersionedAttribute(self, f'condition_{name}s',
+                                      self.versioned_defaults[name])
+            setattr(self, name, attr)
 
     def remove(self, db_conn: DatabaseConnection) -> None:
         """Remove from DB, with VersionedAttributes.
@@ -41,6 +42,7 @@ class Condition(BaseModel[int]):
 
 class ConditionsRelations:
     """Methods for handling relations to Conditions, for Todo and Process."""
+    # pylint: disable=too-few-public-methods
 
     def __init__(self) -> None:
         self.conditions: list[Condition] = []
@@ -48,26 +50,21 @@ class ConditionsRelations:
         self.enables: list[Condition] = []
         self.disables: list[Condition] = []
 
-    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_blockers(self, db_conn: DatabaseConnection,
-                     ids: list[int]) -> None:
-        """Set self.enables to Conditions identified by ids."""
-        self.set_conditions(db_conn, ids, 'blockers')
-
-    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 set_condition_relations(self,
+                                db_conn: DatabaseConnection,
+                                ids_conditions: list[int],
+                                ids_blockers: list[int],
+                                ids_enables: list[int],
+                                ids_disables: list[int]
+                                ) -> None:
+        """Set owned Condition lists to those identified by respective IDs."""
+        # pylint: disable=too-many-arguments
+        for ids, target in [(ids_conditions, 'conditions'),
+                            (ids_blockers, 'blockers'),
+                            (ids_enables, 'enables'),
+                            (ids_disables, 'disables')]:
+            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_)]