X-Git-Url: https://plomlompom.com/repos/%7B%7Bprefix%7D%7D/copy_structured?a=blobdiff_plain;f=plomtask%2Fconditions.py;h=4b012491d6151ed74995849378fa8dc1c04f20f8;hb=ac5a85f6d0186d714415ce7e2b51597bf5dca248;hp=9a442000a99befa563582346826244739bf13ae7;hpb=2d0d3a138de69e5e09208936ac094b53b0785c0b;p=plomtask diff --git a/plomtask/conditions.py b/plomtask/conditions.py index 9a44200..4b01249 100644 --- a/plomtask/conditions.py +++ b/plomtask/conditions.py @@ -7,7 +7,7 @@ from plomtask.misc import VersionedAttribute from plomtask.exceptions import NotFoundException -class Condition(BaseModel): +class Condition(BaseModel[int]): """Non Process-dependency for ProcessSteps and Todos.""" table_name = 'conditions' to_save = ['is_active'] @@ -35,12 +35,13 @@ class Condition(BaseModel): def all(cls, db_conn: DatabaseConnection) -> list[Condition]: """Collect all Conditions and their VersionedAttributes.""" conditions = {} - for id_, condition in db_conn.cached_conditions.items(): + for id_, condition in cls.cache_.items(): conditions[id_] = condition already_recorded = conditions.keys() for id_ in db_conn.column_all('conditions', 'id'): if id_ not in already_recorded: condition = cls.by_id(db_conn, id_) + assert isinstance(condition.id_, int) conditions[condition.id_] = condition return list(conditions.values()) @@ -65,4 +66,26 @@ class Condition(BaseModel): self.title.save(db_conn) 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')