X-Git-Url: https://plomlompom.com/repos/?a=blobdiff_plain;f=plomtask%2Fconditions.py;h=cd147cb79cafcfd81f4672fae0a0b6296201ff6a;hb=8e1a5416151dbcf506f2435823362e21d85aed2d;hp=98a0b99e667adfe11f9f4f0ef3439df03a6cc67f;hpb=33cff8c5f6427c4e7e617c459ee024b5b6c2d32e;p=plomtask diff --git a/plomtask/conditions.py b/plomtask/conditions.py index 98a0b99..cd147cb 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -25,14 +25,10 @@ class Condition(BaseModel): """Build condition from row, including VersionedAttributes.""" condition = super().from_table_row(db_conn, row) assert isinstance(condition, Condition) - for title_row in db_conn.exec('SELECT * FROM condition_titles ' - 'WHERE parent = ?', (row[0],)): - condition.title.history[title_row[1]]\ - = title_row[2] # pylint: disable=no-member - for desc_row in db_conn.exec('SELECT * FROM condition_descriptions ' - 'WHERE parent = ?', (row[0],)): - condition.description.history[desc_row[1]]\ - = desc_row[2] # pylint: disable=no-member + 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 @classmethod @@ -42,9 +38,9 @@ class Condition(BaseModel): for id_, condition in db_conn.cached_conditions.items(): conditions[id_] = condition already_recorded = conditions.keys() - for row in db_conn.exec('SELECT id FROM conditions'): - if row[0] not in already_recorded: - condition = cls.by_id(db_conn, row[0]) + for id_ in db_conn.column_all('conditions', 'id'): + if id_ not in already_recorded: + condition = cls.by_id(db_conn, id_) conditions[condition.id_] = condition return list(conditions.values()) @@ -70,3 +66,26 @@ class Condition(BaseModel): self.description.save(db_conn) assert isinstance(self.id_, int) db_conn.cached_conditions[self.id_] = self + + +class ConditionsRelations: + """Methods for handling relations to Conditions, for Todo and Process.""" + + 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_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')