home · contact · privacy
Minor refactoring.
[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_simples = ['is_active']
12     versioned_defaults = {'title': 'UNNAMED', '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         for name in ['title', 'description']:
22             attr = VersionedAttribute(self, f'condition_{name}s',
23                                       self.versioned_defaults[name])
24             setattr(self, name, attr)
25
26     def remove(self, db_conn: DatabaseConnection) -> None:
27         """Remove from DB, with VersionedAttributes.
28
29         Checks for Todos and Processes that depend on Condition, prohibits
30         deletion if found.
31         """
32         if self.id_ is not None:
33             for item in ('process', 'todo'):
34                 for attr in ('conditions', 'blockers', 'enables', 'disables'):
35                     table_name = f'{item}_{attr}'
36                     for _ in db_conn.row_where(table_name, 'condition',
37                                                self.id_):
38                         msg = 'cannot remove Condition in use'
39                         raise HandledException(msg)
40         super().remove(db_conn)
41
42
43 class ConditionsRelations:
44     """Methods for handling relations to Conditions, for Todo and Process."""
45
46     def __init__(self) -> None:
47         self.conditions: list[Condition] = []
48         self.blockers: list[Condition] = []
49         self.enables: list[Condition] = []
50         self.disables: list[Condition] = []
51
52     def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
53                        target: str = 'conditions') -> None:
54         """Set self.[target] to Conditions identified by ids."""
55         target_list = getattr(self, target)
56         while len(target_list) > 0:
57             target_list.pop()
58         for id_ in ids:
59             target_list += [Condition.by_id(db_conn, id_)]
60
61     def set_blockers(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, 'blockers')
65
66     def set_enables(self, db_conn: DatabaseConnection,
67                     ids: list[int]) -> None:
68         """Set self.enables to Conditions identified by ids."""
69         self.set_conditions(db_conn, ids, 'enables')
70
71     def set_disables(self, db_conn: DatabaseConnection,
72                      ids: list[int]) -> None:
73         """Set self.disables to Conditions identified by ids."""
74         self.set_conditions(db_conn, ids, 'disables')