home · contact · privacy
b2ecda14cb7cef0b5bf350d1a253389ea32aabb7
[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.misc import VersionedAttribute
7 from plomtask.exceptions import NotFoundException
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
15     def __init__(self, id_: int | None, is_active: bool = False) -> None:
16         self.set_int_id(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     @classmethod
23     def from_table_row(cls, db_conn: DatabaseConnection,
24                        row: Row | list[Any]) -> Condition:
25         """Build condition from row, including VersionedAttributes."""
26         condition = super().from_table_row(db_conn, row)
27         for name in ('title', 'description'):
28             table_name = f'condition_{name}s'
29             for row_ in db_conn.row_where(table_name, 'parent', row[0]):
30                 getattr(condition, name).history_from_row(row_)
31         return condition
32
33     @classmethod
34     def all(cls, db_conn: DatabaseConnection) -> list[Condition]:
35         """Collect all Conditions and their VersionedAttributes."""
36         conditions = {}
37         for id_, condition in cls.cache_.items():
38             conditions[id_] = condition
39         already_recorded = conditions.keys()
40         for id_ in db_conn.column_all('conditions', 'id'):
41             if id_ not in already_recorded:
42                 condition = cls.by_id(db_conn, id_)
43                 assert isinstance(condition.id_, int)
44                 conditions[condition.id_] = condition
45         return list(conditions.values())
46
47     @classmethod
48     def by_id(cls, db_conn: DatabaseConnection, id_: int | None,
49               create: bool = False) -> Condition:
50         """Collect (or create) Condition and its VersionedAttributes."""
51         condition = None
52         if id_:
53             condition, _ = super()._by_id(db_conn, id_)
54         if not condition:
55             if not create:
56                 raise NotFoundException(f'Condition not found of id: {id_}')
57             condition = cls(id_, False)
58             condition.save(db_conn)
59         return condition
60
61     def save(self, db_conn: DatabaseConnection) -> None:
62         """Save self and its VersionedAttributes to DB and cache."""
63         self.save_core(db_conn)
64         self.title.save(db_conn)
65         self.description.save(db_conn)
66
67
68 class ConditionsRelations:
69     """Methods for handling relations to Conditions, for Todo and Process."""
70
71     def set_conditions(self, db_conn: DatabaseConnection, ids: list[int],
72                        target: str = 'conditions') -> None:
73         """Set self.[target] to Conditions identified by ids."""
74         target_list = getattr(self, target)
75         while len(target_list) > 0:
76             target_list.pop()
77         for id_ in ids:
78             target_list += [Condition.by_id(db_conn, id_)]
79
80     def set_enables(self, db_conn: DatabaseConnection,
81                     ids: list[int]) -> None:
82         """Set self.enables to Conditions identified by ids."""
83         self.set_conditions(db_conn, ids, 'enables')
84
85     def set_disables(self, db_conn: DatabaseConnection,
86                      ids: list[int]) -> None:
87         """Set self.disables to Conditions identified by ids."""
88         self.set_conditions(db_conn, ids, 'disables')