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] = []
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_)]
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)
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
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)
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)
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
"""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:
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])
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)
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