From: Christian Heller Date: Sun, 21 Jul 2024 06:14:22 +0000 (+0200) Subject: Simplify Condition relations setting API. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/static/%7B%7Bprefix%7D%7D/day?a=commitdiff_plain;h=41a0f7ccd2eadd62fe62d23f13fd943842d3f102;p=plomtask Simplify Condition relations setting API. --- diff --git a/plomtask/conditions.py b/plomtask/conditions.py index e752e91..8d41604 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -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_)] diff --git a/plomtask/http.py b/plomtask/http.py index bcd6d1c..d7af591 100644 --- a/plomtask/http.py +++ b/plomtask/http.py @@ -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) diff --git a/tests/conditions.py b/tests/conditions.py index 1a6b08e..6cf06b8 100644 --- a/tests/conditions.py +++ b/tests/conditions.py @@ -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) diff --git a/tests/processes.py b/tests/processes.py index 973ba3b..eb94745 100644 --- a/tests/processes.py +++ b/tests/processes.py @@ -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: diff --git a/tests/todos.py b/tests/todos.py index 695d9a7..25fa05b 100644 --- a/tests/todos.py +++ b/tests/todos.py @@ -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