1 """Non-doable elements of ProcessStep/Todo chains."""
2 from __future__ import annotations
3 from plomtask.db import DatabaseConnection, BaseModel
4 from plomtask.versioned_attributes import VersionedAttribute
5 from plomtask.exceptions import HandledException
8 class Condition(BaseModel[int]):
9 """Non-Process dependency for ProcessSteps and Todos."""
10 table_name = 'conditions'
11 to_save = ['is_active']
12 to_save_versioned = ['title', 'description']
13 to_search = ['title.newest', 'description.newest']
14 can_create_by_id = True
16 def __init__(self, id_: int | None, is_active: bool = False) -> None:
18 self.is_active = is_active
19 self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
20 self.description = VersionedAttribute(self, 'condition_descriptions',
23 def remove(self, db_conn: DatabaseConnection) -> None:
24 """Remove from DB, with VersionedAttributes.
26 Checks for Todos and Processes that depend on Condition, prohibits
29 if self.id_ is not None:
30 for item in ('process', 'todo'):
31 for attr in ('conditions', 'blockers', 'enables', 'disables'):
32 table_name = f'{item}_{attr}'
33 for _ in db_conn.row_where(table_name, 'condition',
35 msg = 'cannot remove Condition in use'
36 raise HandledException(msg)
37 super().remove(db_conn)
40 class ConditionsRelations:
41 """Methods for handling relations to Conditions, for Todo and Process."""
43 def __init__(self) -> None:
44 self.conditions: list[Condition] = []
45 self.blockers: list[Condition] = []
46 self.enables: list[Condition] = []
47 self.disables: list[Condition] = []
49 def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
50 target: str = 'conditions') -> None:
51 """Set self.[target] to Conditions identified by ids."""
52 target_list = getattr(self, target)
53 while len(target_list) > 0:
56 target_list += [Condition.by_id(db_conn, id_)]
58 def set_blockers(self, db_conn: DatabaseConnection,
59 ids: list[int]) -> None:
60 """Set self.enables to Conditions identified by ids."""
61 self.set_conditions(db_conn, ids, 'blockers')
63 def set_enables(self, db_conn: DatabaseConnection,
64 ids: list[int]) -> None:
65 """Set self.enables to Conditions identified by ids."""
66 self.set_conditions(db_conn, ids, 'enables')
68 def set_disables(self, db_conn: DatabaseConnection,
69 ids: list[int]) -> None:
70 """Set self.disables to Conditions identified by ids."""
71 self.set_conditions(db_conn, ids, 'disables')