home · contact · privacy
Base core models on BaseModel providing sensible defaults.
[plomtask] / plomtask / conditions.py
1 """Non-doable elements of ProcessStep/Todo chains."""
2 from __future__ import annotations
3 from sqlite3 import Row
4 from plomtask.db import DatabaseConnection, BaseModel
5 from plomtask.misc import VersionedAttribute
6 from plomtask.exceptions import NotFoundException
7
8
9 class Condition(BaseModel):
10     """Non Process-dependency for ProcessSteps and Todos."""
11     table_name = 'conditions'
12     to_save = ['is_active']
13
14     def __init__(self, id_: int | None, is_active: bool = False) -> None:
15         self.set_int_id(id_)
16         self.is_active = is_active
17         self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
18         self.description = VersionedAttribute(self, 'condition_descriptions',
19                                               '')
20
21     @classmethod
22     def from_table_row(cls, db_conn: DatabaseConnection,
23                        row: Row) -> Condition:
24         """Build condition from row, including VersionedAttributes."""
25         condition = cls(row[0], row[1])
26         for title_row in db_conn.exec('SELECT * FROM condition_titles '
27                                       'WHERE parent_id = ?', (row[0],)):
28             condition.title.history[title_row[1]] = title_row[2]
29         for desc_row in db_conn.exec('SELECT * FROM condition_descriptions '
30                                      'WHERE parent_id = ?', (row[0],)):
31             condition.description.history[desc_row[1]] = desc_row[2]
32         return condition
33
34     @classmethod
35     def all(cls, db_conn: DatabaseConnection) -> list[Condition]:
36         """Collect all Conditions and their VersionedAttributes."""
37         conditions = {}
38         for id_, condition in db_conn.cached_conditions.items():
39             conditions[id_] = condition
40         already_recorded = conditions.keys()
41         for row in db_conn.exec('SELECT id FROM conditions'):
42             if row[0] not in already_recorded:
43                 condition = cls.by_id(db_conn, row[0])
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_ in db_conn.cached_conditions.keys():
53             condition = db_conn.cached_conditions[id_]
54         else:
55             for row in db_conn.exec('SELECT * FROM conditions WHERE id = ?',
56                                     (id_,)):
57                 condition = cls.from_table_row(db_conn, row)
58                 break
59         if not condition:
60             if not create:
61                 raise NotFoundException(f'Condition not found of id: {id_}')
62             condition = cls(id_, False)
63         return condition
64
65     def save(self, db_conn: DatabaseConnection) -> None:
66         """Save self and its VersionedAttributes to DB and cache."""
67         self.save_core(db_conn)
68         self.title.save(db_conn)
69         self.description.save(db_conn)
70         assert isinstance(self.id_, int)
71         db_conn.cached_conditions[self.id_] = self