home · contact · privacy
Improve consistency of DB column names.
[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):
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         assert isinstance(condition, Condition)
28         for title_row in db_conn.exec('SELECT * FROM condition_titles '
29                                       'WHERE parent = ?', (row[0],)):
30             condition.title.history[title_row[1]]\
31                     = title_row[2]  # pylint: disable=no-member
32         for desc_row in db_conn.exec('SELECT * FROM condition_descriptions '
33                                      'WHERE parent = ?', (row[0],)):
34             condition.description.history[desc_row[1]]\
35                     = desc_row[2]  # pylint: disable=no-member
36         return condition
37
38     @classmethod
39     def all(cls, db_conn: DatabaseConnection) -> list[Condition]:
40         """Collect all Conditions and their VersionedAttributes."""
41         conditions = {}
42         for id_, condition in db_conn.cached_conditions.items():
43             conditions[id_] = condition
44         already_recorded = conditions.keys()
45         for row in db_conn.exec('SELECT id FROM conditions'):
46             if row[0] not in already_recorded:
47                 condition = cls.by_id(db_conn, row[0])
48                 conditions[condition.id_] = condition
49         return list(conditions.values())
50
51     @classmethod
52     def by_id(cls, db_conn: DatabaseConnection, id_: int | None,
53               create: bool = False) -> Condition:
54         """Collect (or create) Condition and its VersionedAttributes."""
55         condition = None
56         if id_:
57             condition, _ = super()._by_id(db_conn, id_)
58         if not condition:
59             if not create:
60                 raise NotFoundException(f'Condition not found of id: {id_}')
61             condition = cls(id_, False)
62             condition.save(db_conn)
63         assert isinstance(condition, Condition)
64         return condition
65
66     def save(self, db_conn: DatabaseConnection) -> None:
67         """Save self and its VersionedAttributes to DB and cache."""
68         self.save_core(db_conn)
69         self.title.save(db_conn)
70         self.description.save(db_conn)
71         assert isinstance(self.id_, int)
72         db_conn.cached_conditions[self.id_] = self