home · contact · privacy
Add Todo/Process.blockers for Conditions that block rather than enable.
[plomtask] / plomtask / conditions.py
1 """Non-doable elements of ProcessStep/Todo chains."""
2 from __future__ import annotations
3 from typing import Any
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
8
9
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
16     def __init__(self, id_: int | None, is_active: bool = False) -> None:
17         super().__init__(id_)
18         self.is_active = is_active
19         self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
20         self.description = VersionedAttribute(self, 'condition_descriptions',
21                                               '')
22
23     @classmethod
24     def from_table_row(cls, db_conn: DatabaseConnection,
25                        row: Row | list[Any]) -> Condition:
26         """Build condition from row, including VersionedAttributes."""
27         condition = super().from_table_row(db_conn, row)
28         for name in ('title', 'description'):
29             table_name = f'condition_{name}s'
30             for row_ in db_conn.row_where(table_name, 'parent', row[0]):
31                 getattr(condition, name).history_from_row(row_)
32         return condition
33
34     def remove(self, db_conn: DatabaseConnection) -> None:
35         """Remove from DB, with VersionedAttributes.
36
37         Checks for Todos and Processes that depend on Condition, prohibits
38         deletion if found.
39         """
40         if self.id_ is None:
41             raise HandledException('cannot remove unsaved item')
42         for item in ('process', 'todo'):
43             for attr in ('conditions', 'blockers', 'enables', 'disables'):
44                 table_name = f'{item}_{attr}'
45                 for _ in db_conn.row_where(table_name, 'condition', self.id_):
46                     raise HandledException('cannot remove Condition in use')
47         super().remove(db_conn)
48
49
50 class ConditionsRelations:
51     """Methods for handling relations to Conditions, for Todo and Process."""
52
53     def __init__(self) -> None:
54         self.conditions: list[Condition] = []
55         self.blockers: list[Condition] = []
56         self.enables: list[Condition] = []
57         self.disables: list[Condition] = []
58
59     def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
60                        target: str = 'conditions') -> None:
61         """Set self.[target] to Conditions identified by ids."""
62         target_list = getattr(self, target)
63         while len(target_list) > 0:
64             target_list.pop()
65         for id_ in ids:
66             target_list += [Condition.by_id(db_conn, id_)]
67
68     def set_blockers(self, db_conn: DatabaseConnection,
69                      ids: list[int]) -> None:
70         """Set self.enables to Conditions identified by ids."""
71         self.set_conditions(db_conn, ids, 'blockers')
72
73     def set_enables(self, db_conn: DatabaseConnection,
74                     ids: list[int]) -> None:
75         """Set self.enables to Conditions identified by ids."""
76         self.set_conditions(db_conn, ids, 'enables')
77
78     def set_disables(self, db_conn: DatabaseConnection,
79                      ids: list[int]) -> None:
80         """Set self.disables to Conditions identified by ids."""
81         self.set_conditions(db_conn, ids, 'disables')