home · contact · privacy
Refactor BaseModel sorting from GET handlers into class definitions.
[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     can_create_by_id = True
15     sorters = {'is_active': lambda c: c.is_active,
16                'title': lambda c: c.title.newest}
17
18     def __init__(self, id_: int | None, is_active: bool = False) -> None:
19         super().__init__(id_)
20         self.is_active = is_active
21         self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
22         self.description = VersionedAttribute(self, 'condition_descriptions',
23                                               '')
24
25     def remove(self, db_conn: DatabaseConnection) -> None:
26         """Remove from DB, with VersionedAttributes.
27
28         Checks for Todos and Processes that depend on Condition, prohibits
29         deletion if found.
30         """
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',
36                                                self.id_):
37                         msg = 'cannot remove Condition in use'
38                         raise HandledException(msg)
39         super().remove(db_conn)
40
41
42 class ConditionsRelations:
43     """Methods for handling relations to Conditions, for Todo and Process."""
44
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] = []
50
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:
56             target_list.pop()
57         for id_ in ids:
58             target_list += [Condition.by_id(db_conn, id_)]
59
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')
64
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')
69
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')