from sqlite3 import Row
from plomtask.db import DatabaseConnection, BaseModel
from plomtask.misc import VersionedAttribute
-from plomtask.exceptions import NotFoundException
class Condition(BaseModel[int]):
to_save = ['is_active']
def __init__(self, id_: int | None, is_active: bool = False) -> None:
- self.set_int_id(id_)
+ super().__init__(id_)
self.is_active = is_active
self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED')
self.description = VersionedAttribute(self, 'condition_descriptions',
getattr(condition, name).history_from_row(row_)
return condition
- @classmethod
- def by_id(cls, db_conn: DatabaseConnection, id_: int | None,
- create: bool = False) -> Condition:
- """Collect (or create) Condition and its VersionedAttributes."""
- condition = None
- if id_:
- condition, _ = super()._by_id(db_conn, id_)
- if not condition:
- if not create:
- raise NotFoundException(f'Condition not found of id: {id_}')
- condition = cls(id_, False)
- condition.save(db_conn)
- return condition
-
def save(self, db_conn: DatabaseConnection) -> None:
"""Save self and its VersionedAttributes to DB and cache."""
self.save_core(db_conn)