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
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}
18 def __init__(self, id_: int | None, is_active: bool = False) -> None:
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)
26 def remove(self, db_conn: DatabaseConnection) -> None:
27 """Remove from DB, with VersionedAttributes.
29 Checks for Todos and Processes that depend on Condition, prohibits
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',
38 msg = 'cannot remove Condition in use'
39 raise HandledException(msg)
40 super().remove(db_conn)
43 class ConditionsRelations:
44 """Methods for handling relations to Conditions, for Todo and Process."""
45 # pylint: disable=too-few-public-methods
47 def __init__(self) -> None:
48 self.conditions: list[Condition] = []
49 self.blockers: list[Condition] = []
50 self.enables: list[Condition] = []
51 self.disables: list[Condition] = []
53 def set_condition_relations(self,
54 db_conn: DatabaseConnection,
55 ids_conditions: list[int],
56 ids_blockers: list[int],
57 ids_enables: list[int],
58 ids_disables: list[int]
60 """Set owned Condition lists to those identified by respective IDs."""
61 # pylint: disable=too-many-arguments
62 for ids, target in [(ids_conditions, 'conditions'),
63 (ids_blockers, 'blockers'),
64 (ids_enables, 'enables'),
65 (ids_disables, 'disables')]:
66 target_list = getattr(self, target)
67 while len(target_list) > 0:
70 target_list += [Condition.by_id(db_conn, id_)]