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
15 sorters = {'is_active': lambda c: c.is_active,
16 'title': lambda c: c.title.newest}
18 def __init__(self, id_: int | None, is_active: bool = False) -> None:
20 self.is_active = is_active
21 self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
22 self.description = VersionedAttribute(self, 'condition_descriptions',
25 def remove(self, db_conn: DatabaseConnection) -> None:
26 """Remove from DB, with VersionedAttributes.
28 Checks for Todos and Processes that depend on Condition, prohibits
31 if self.id_ is not None:
32 for item in ('process', 'todo'):
33 for attr in ('conditions', 'blockers', 'enables', 'disables'):
34 table_name = f'{item}_{attr}'
35 for _ in db_conn.row_where(table_name, 'condition',
37 msg = 'cannot remove Condition in use'
38 raise HandledException(msg)
39 super().remove(db_conn)
42 class ConditionsRelations:
43 """Methods for handling relations to Conditions, for Todo and Process."""
45 def __init__(self) -> None:
46 self.conditions: list[Condition] = []
47 self.blockers: list[Condition] = []
48 self.enables: list[Condition] = []
49 self.disables: list[Condition] = []
51 def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
52 target: str = 'conditions') -> None:
53 """Set self.[target] to Conditions identified by ids."""
54 target_list = getattr(self, target)
55 while len(target_list) > 0:
58 target_list += [Condition.by_id(db_conn, id_)]
60 def set_blockers(self, db_conn: DatabaseConnection,
61 ids: list[int]) -> None:
62 """Set self.enables to Conditions identified by ids."""
63 self.set_conditions(db_conn, ids, 'blockers')
65 def set_enables(self, db_conn: DatabaseConnection,
66 ids: list[int]) -> None:
67 """Set self.enables to Conditions identified by ids."""
68 self.set_conditions(db_conn, ids, 'enables')
70 def set_disables(self, db_conn: DatabaseConnection,
71 ids: list[int]) -> None:
72 """Set self.disables to Conditions identified by ids."""
73 self.set_conditions(db_conn, ids, 'disables')