home · contact · privacy
Refactor BaseModel.from_table_row in regards to VersionedAttributes.
[plomtask] / plomtask / conditions.py
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
6
7
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
15     def __init__(self, id_: int | None, is_active: bool = False) -> None:
16         super().__init__(id_)
17         self.is_active = is_active
18         self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
19         self.description = VersionedAttribute(self, 'condition_descriptions',
20                                               '')
21
22     def remove(self, db_conn: DatabaseConnection) -> None:
23         """Remove from DB, with VersionedAttributes.
24
25         Checks for Todos and Processes that depend on Condition, prohibits
26         deletion if found.
27         """
28         if self.id_ is None:
29             raise HandledException('cannot remove unsaved item')
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', self.id_):
34                     raise HandledException('cannot remove Condition in use')
35         super().remove(db_conn)
36
37
38 class ConditionsRelations:
39     """Methods for handling relations to Conditions, for Todo and Process."""
40
41     def __init__(self) -> None:
42         self.conditions: list[Condition] = []
43         self.blockers: list[Condition] = []
44         self.enables: list[Condition] = []
45         self.disables: list[Condition] = []
46
47     def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
48                        target: str = 'conditions') -> None:
49         """Set self.[target] to Conditions identified by ids."""
50         target_list = getattr(self, target)
51         while len(target_list) > 0:
52             target_list.pop()
53         for id_ in ids:
54             target_list += [Condition.by_id(db_conn, id_)]
55
56     def set_blockers(self, db_conn: DatabaseConnection,
57                      ids: list[int]) -> None:
58         """Set self.enables to Conditions identified by ids."""
59         self.set_conditions(db_conn, ids, 'blockers')
60
61     def set_enables(self, db_conn: DatabaseConnection,
62                     ids: list[int]) -> None:
63         """Set self.enables to Conditions identified by ids."""
64         self.set_conditions(db_conn, ids, 'enables')
65
66     def set_disables(self, db_conn: DatabaseConnection,
67                      ids: list[int]) -> None:
68         """Set self.disables to Conditions identified by ids."""
69         self.set_conditions(db_conn, ids, 'disables')