home · contact · privacy
Simplify Condition relations setting API.
authorChristian Heller <c.heller@plomlompom.de>
Sun, 21 Jul 2024 06:14:22 +0000 (08:14 +0200)
committerChristian Heller <c.heller@plomlompom.de>
Sun, 21 Jul 2024 06:14:22 +0000 (08:14 +0200)
plomtask/conditions.py
plomtask/http.py
tests/conditions.py
tests/processes.py
tests/todos.py

index e752e91a277936f62cf6a39a4cc57e571e6c49e7..8d4160424423cf6ddfccb06b5b04d77e2bea4b15 100644 (file)
@@ -42,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] = []
@@ -49,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_)]
index bcd6d1cb6e80851d8e147ed4cf8713c6ab29f0ca..d7af591012ef54362e1cad5af335091d66402b5b 100644 (file)
@@ -670,10 +670,8 @@ class TaskHandler(BaseHTTPRequestHandler):
             child = Todo.by_id(self.conn, id_)
             todo.remove_child(child)
         for child_id in adopted_child_ids:
-            if child_id in [c.id_ for c in todo.children]:
-                continue
-            child = Todo.by_id(self.conn, child_id)
-            todo.add_child(child)
+            if child_id not in [c.id_ for c in todo.children]:
+                todo.add_child(Todo.by_id(self.conn, child_id))
         for process_id in processes_to_make_empty:
             process = Process.by_id(self.conn, process_id)
             made = Todo(None, process, False, todo.date)
@@ -684,10 +682,8 @@ class TaskHandler(BaseHTTPRequestHandler):
             todo.add_child(made)
         if with_effort_post:
             todo.effort = effort
-        todo.set_conditions(self.conn, conditions)
-        todo.set_blockers(self.conn, blockers)
-        todo.set_enables(self.conn, enables)
-        todo.set_disables(self.conn, disables)
+        todo.set_condition_relations(
+                self.conn, conditions, blockers, enables, disables)
         todo.is_done = is_done
         todo.calendarize = calendarize
         todo.comment = comment
@@ -743,10 +739,8 @@ class TaskHandler(BaseHTTPRequestHandler):
         process.title.set(title)
         process.description.set(description)
         process.effort.set(effort)
-        process.set_conditions(self.conn, conditions)
-        process.set_blockers(self.conn, blockers)
-        process.set_enables(self.conn, enables)
-        process.set_disables(self.conn, disables)
+        process.set_condition_relations(
+                self.conn, conditions, blockers, enables, disables)
         process.calendarize = calendarize
         process.save(self.conn)
         assert isinstance(process.id_, int)
index 1a6b08ee1c083acd121644833c096c096e685c8e..6cf06b831bf8a46b206fecfbfa767305aa3f2733 100644 (file)
@@ -25,15 +25,13 @@ class TestsWithDB(TestCaseWithDB):
         todo.save(self.db_conn)
         # check condition can only be deleted if not depended upon
         for depender in (proc, todo):
-            assert hasattr(depender, 'save')
-            assert hasattr(depender, 'set_conditions')
             c = Condition(None)
             c.save(self.db_conn)
-            depender.set_conditions(self.db_conn, [c.id_])
+            depender.set_condition_relations(self.db_conn, [c.id_], [], [], [])
             depender.save(self.db_conn)
             with self.assertRaises(HandledException):
                 c.remove(self.db_conn)
-            depender.set_conditions(self.db_conn, [])
+            depender.set_condition_relations(self.db_conn, [], [], [], [])
             depender.save(self.db_conn)
             c.remove(self.db_conn)
 
index 973ba3beaad601264084824710d95d6084c1ec9a..eb94745bb0bae2dceed0da05559beaca92cf750c 100644 (file)
@@ -43,12 +43,10 @@ class TestsWithDB(TestCaseWithDB):
         set_1 = [c1, c2]
         set_2 = [c2, c3]
         set_3 = [c1, c3]
-        p.set_conditions(self.db_conn, [c.id_ for c in set_1
-                                        if isinstance(c.id_, int)])
-        p.set_enables(self.db_conn, [c.id_ for c in set_2
-                                     if isinstance(c.id_, int)])
-        p.set_disables(self.db_conn, [c.id_ for c in set_3
-                                      if isinstance(c.id_, int)])
+        conds = [c.id_ for c in set_1 if isinstance(c.id_, int)]
+        enables = [c.id_ for c in set_2 if isinstance(c.id_, int)]
+        disables = [c.id_ for c in set_3 if isinstance(c.id_, int)]
+        p.set_condition_relations(self.db_conn, conds, [], enables, disables)
         p.save(self.db_conn)
         return p, set_1, set_2, set_3
 
@@ -161,20 +159,25 @@ class TestsWithDB(TestCaseWithDB):
         """Test setting Process.conditions/enables/disables."""
         p = Process(None)
         p.save(self.db_conn)
-        for target in ('conditions', 'enables', 'disables'):
-            method = getattr(p, f'set_{target}')
+        targets = ['conditions', 'blockers', 'enables', 'disables']
+        for i, target in enumerate(targets):
             c1, c2 = Condition(None), Condition(None)
             c1.save(self.db_conn)
             c2.save(self.db_conn)
             assert isinstance(c1.id_, int)
             assert isinstance(c2.id_, int)
-            method(self.db_conn, [])
+            args: list[list[int]] = [[], [], [], []]
+            args[i] = []
+            p.set_condition_relations(self.db_conn, *args)
             self.assertEqual(getattr(p, target), [])
-            method(self.db_conn, [c1.id_])
+            args[i] = [c1.id_]
+            p.set_condition_relations(self.db_conn, *args)
             self.assertEqual(getattr(p, target), [c1])
-            method(self.db_conn, [c2.id_])
+            args[i] = [c2.id_]
+            p.set_condition_relations(self.db_conn, *args)
             self.assertEqual(getattr(p, target), [c2])
-            method(self.db_conn, [c1.id_, c2.id_])
+            args[i] = [c1.id_, c2.id_]
+            p.set_condition_relations(self.db_conn, *args)
             self.assertEqual(getattr(p, target), [c1, c2])
 
     def test_remove(self) -> None:
index 695d9a79b2a56361338bbcd55ea17ed5bad61e39..25fa05bbd4e1a030d578fc60ef22cecf3ef3accb 100644 (file)
@@ -38,9 +38,9 @@ class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
         process.save(self.db_conn)
         assert isinstance(self.cond1.id_, int)
         assert isinstance(self.cond2.id_, int)
-        process.set_conditions(self.db_conn, [self.cond1.id_, self.cond2.id_])
-        process.set_enables(self.db_conn, [self.cond1.id_])
-        process.set_disables(self.db_conn, [self.cond2.id_])
+        process.set_condition_relations(self.db_conn,
+                                        [self.cond1.id_, self.cond2.id_], [],
+                                        [self.cond1.id_], [self.cond2.id_])
         todo_no_id = Todo(None, process, False, self.date1)
         self.assertEqual(todo_no_id.conditions, [self.cond1, self.cond2])
         self.assertEqual(todo_no_id.enables, [self.cond1])
@@ -71,8 +71,8 @@ class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
         assert isinstance(self.cond2.id_, int)
         todo = Todo(None, self.proc, False, self.date1)
         todo.save(self.db_conn)
-        todo.set_enables(self.db_conn, [self.cond1.id_])
-        todo.set_disables(self.db_conn, [self.cond2.id_])
+        todo.set_condition_relations(self.db_conn, [], [],
+                                     [self.cond1.id_], [self.cond2.id_])
         todo.is_done = True
         self.assertEqual(self.cond1.is_active, True)
         self.assertEqual(self.cond2.is_active, False)
@@ -113,7 +113,8 @@ class TestsWithDB(TestCaseWithDB, TestCaseSansDB):
         todo_1.is_done = True
         todo_2.is_done = True
         todo_2.is_done = False
-        todo_2.set_conditions(self.db_conn, [self.cond1.id_])
+        todo_2.set_condition_relations(
+                self.db_conn, [self.cond1.id_], [], [], [])
         with self.assertRaises(BadFormatException):
             todo_2.is_done = True
         self.cond1.is_active = True