- @classmethod
- def by_id(cls, db_conn: DatabaseConnection, id_: int | None,
- create: bool = False) -> Condition:
- """Collect (or create) Condition and its VersionedAttributes."""
- condition = None
- if id_:
- condition, _ = super()._by_id(db_conn, id_)
- if not condition:
- if not create:
- raise NotFoundException(f'Condition not found of id: {id_}')
- condition = cls(id_, False)
- condition.save(db_conn)
- assert isinstance(condition, Condition)
- return condition
+ Checks for Todos and Processes that depend on Condition, prohibits
+ deletion if found.
+ """
+ if self.id_ is None:
+ raise HandledException('cannot remove unsaved item')
+ for item in ('process', 'todo'):
+ for attr in ('conditions', 'blockers', 'enables', 'disables'):
+ table_name = f'{item}_{attr}'
+ for _ in db_conn.row_where(table_name, 'condition', self.id_):
+ raise HandledException('cannot remove Condition in use')
+ super().remove(db_conn)
+
+
+class ConditionsRelations:
+ """Methods for handling relations to Conditions, for Todo and Process."""
+
+ def __init__(self) -> None:
+ self.conditions: list[Condition] = []
+ self.blockers: 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')