1 """Non-doable elements of ProcessStep/Todo chains."""
2 from __future__ import annotations
4 from sqlite3 import Row
5 from plomtask.db import DatabaseConnection, BaseModel
6 from plomtask.versioned_attributes import VersionedAttribute
7 from plomtask.exceptions import HandledException
10 class Condition(BaseModel[int]):
11 """Non-Process dependency for ProcessSteps and Todos."""
12 table_name = 'conditions'
13 to_save = ['is_active']
14 to_save_versioned = ['title', 'description']
15 to_search = ['title.newest', 'description.newest']
17 def __init__(self, id_: int | None, is_active: bool = False) -> None:
19 self.is_active = is_active
20 self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
21 self.description = VersionedAttribute(self, 'condition_descriptions',
25 def from_table_row(cls, db_conn: DatabaseConnection,
26 row: Row | list[Any]) -> Condition:
27 """Build condition from row, including VersionedAttributes."""
28 condition = super().from_table_row(db_conn, row)
29 for name in ('title', 'description'):
30 table_name = f'condition_{name}s'
31 for row_ in db_conn.row_where(table_name, 'parent', row[0]):
32 getattr(condition, name).history_from_row(row_)
35 def remove(self, db_conn: DatabaseConnection) -> None:
36 """Remove from DB, with VersionedAttributes.
38 Checks for Todos and Processes that depend on Condition, prohibits
42 raise HandledException('cannot remove unsaved item')
43 for item in ('process', 'todo'):
44 for attr in ('conditions', 'blockers', 'enables', 'disables'):
45 table_name = f'{item}_{attr}'
46 for _ in db_conn.row_where(table_name, 'condition', self.id_):
47 raise HandledException('cannot remove Condition in use')
48 super().remove(db_conn)
51 class ConditionsRelations:
52 """Methods for handling relations to Conditions, for Todo and Process."""
54 def __init__(self) -> None:
55 self.conditions: list[Condition] = []
56 self.blockers: list[Condition] = []
57 self.enables: list[Condition] = []
58 self.disables: list[Condition] = []
60 def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
61 target: str = 'conditions') -> None:
62 """Set self.[target] to Conditions identified by ids."""
63 target_list = getattr(self, target)
64 while len(target_list) > 0:
67 target_list += [Condition.by_id(db_conn, id_)]
69 def set_blockers(self, db_conn: DatabaseConnection,
70 ids: list[int]) -> None:
71 """Set self.enables to Conditions identified by ids."""
72 self.set_conditions(db_conn, ids, 'blockers')
74 def set_enables(self, db_conn: DatabaseConnection,
75 ids: list[int]) -> None:
76 """Set self.enables to Conditions identified by ids."""
77 self.set_conditions(db_conn, ids, 'enables')
79 def set_disables(self, db_conn: DatabaseConnection,
80 ids: list[int]) -> None:
81 """Set self.disables to Conditions identified by ids."""
82 self.set_conditions(db_conn, ids, 'disables')