X-Git-Url: https://plomlompom.com/repos/%22https:/validator.w3.org/static/git-favicon.png?a=blobdiff_plain;ds=sidebyside;f=plomtask%2Fconditions.py;h=15dcb9df623c60378485632ce3bebc4c30f03d47;hb=HEAD;hp=d2559272cd876c5b071b59437998e3356409cd07;hpb=85c260d1416340274b954175a69563868f481bc5;p=plomtask diff --git a/plomtask/conditions.py b/plomtask/conditions.py index d255927..8d41604 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -1,7 +1,5 @@ """Non-doable elements of ProcessStep/Todo chains.""" from __future__ import annotations -from typing import Any -from sqlite3 import Row from plomtask.db import DatabaseConnection, BaseModel from plomtask.versioned_attributes import VersionedAttribute from plomtask.exceptions import HandledException @@ -10,27 +8,20 @@ from plomtask.exceptions import HandledException class Condition(BaseModel[int]): """Non-Process dependency for ProcessSteps and Todos.""" table_name = 'conditions' - to_save = ['is_active'] - to_save_versioned = ['title', 'description'] + to_save_simples = ['is_active'] + versioned_defaults = {'title': 'UNNAMED', 'description': ''} to_search = ['title.newest', 'description.newest'] + can_create_by_id = True + sorters = {'is_active': lambda c: c.is_active, + 'title': lambda c: c.title.newest} def __init__(self, id_: int | None, is_active: bool = False) -> None: super().__init__(id_) self.is_active = is_active - self.title = VersionedAttribute(self, 'condition_titles', 'UNNAMED') - self.description = VersionedAttribute(self, 'condition_descriptions', - '') - - @classmethod - def from_table_row(cls, db_conn: DatabaseConnection, - row: Row | list[Any]) -> Condition: - """Build condition from row, including VersionedAttributes.""" - condition = super().from_table_row(db_conn, row) - for name in ('title', 'description'): - table_name = f'condition_{name}s' - for row_ in db_conn.row_where(table_name, 'parent', row[0]): - getattr(condition, name).history_from_row(row_) - return condition + for name in ['title', 'description']: + attr = VersionedAttribute(self, f'condition_{name}s', + self.versioned_defaults[name]) + setattr(self, name, attr) def remove(self, db_conn: DatabaseConnection) -> None: """Remove from DB, with VersionedAttributes. @@ -38,18 +29,20 @@ class Condition(BaseModel[int]): Checks for Todos and Processes that depend on Condition, prohibits deletion if found. """ - if self.id_ is None: - raise HandledException('cannot remove unsaved item') - for item in ('process', 'todo'): - for attr in ('conditions', 'blockers', 'enables', 'disables'): - table_name = f'{item}_{attr}' - for _ in db_conn.row_where(table_name, 'condition', self.id_): - raise HandledException('cannot remove Condition in use') + if self.id_ is not None: + for item in ('process', 'todo'): + for attr in ('conditions', 'blockers', 'enables', 'disables'): + table_name = f'{item}_{attr}' + for _ in db_conn.row_where(table_name, 'condition', + self.id_): + msg = 'cannot remove Condition in use' + raise HandledException(msg) super().remove(db_conn) class ConditionsRelations: """Methods for handling relations to Conditions, for Todo and Process.""" + # pylint: disable=too-few-public-methods def __init__(self) -> None: self.conditions: list[Condition] = [] @@ -57,26 +50,21 @@ class ConditionsRelations: self.enables: list[Condition] = [] self.disables: list[Condition] = [] - def set_conditions(self, db_conn: DatabaseConnection, ids: list[int], - target: str = 'conditions') -> None: - """Set self.[target] to Conditions identified by ids.""" - target_list = getattr(self, target) - while len(target_list) > 0: - target_list.pop() - for id_ in ids: - target_list += [Condition.by_id(db_conn, id_)] - - def set_blockers(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.enables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'blockers') - - def set_enables(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.enables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'enables') - - def set_disables(self, db_conn: DatabaseConnection, - ids: list[int]) -> None: - """Set self.disables to Conditions identified by ids.""" - self.set_conditions(db_conn, ids, 'disables') + def set_condition_relations(self, + db_conn: DatabaseConnection, + ids_conditions: list[int], + ids_blockers: list[int], + ids_enables: list[int], + ids_disables: list[int] + ) -> None: + """Set owned Condition lists to those identified by respective IDs.""" + # pylint: disable=too-many-arguments + for ids, target in [(ids_conditions, 'conditions'), + (ids_blockers, 'blockers'), + (ids_enables, 'enables'), + (ids_disables, 'disables')]: + target_list = getattr(self, target) + while len(target_list) > 0: + target_list.pop() + for id_ in ids: + target_list += [Condition.by_id(db_conn, id_)]