self.explicit_steps: list[ProcessStep] = []
self.suppressed_steps: list[ProcessStep] = []
self.calendarize = calendarize
+ self.n_owners: int | None = None # only set by from_table_row
+
+ @property
+ def as_dict(self) -> dict[str, object]:
+ """Return self as (json.dumps-coompatible) dict."""
+ d = super().as_dict
+ d['explicit_steps'] = [s.as_dict for s in self.explicit_steps]
+ d['suppressed_steps'] = [s.as_dict for s in self.suppressed_steps]
+ return d
@classmethod
def from_table_row(cls, db_conn: DatabaseConnection,
table = f'process_{name}s'
for row_ in db_conn.row_where(table, 'parent', process.id_):
getattr(process, name).history_from_row(row_)
- for row_ in db_conn.row_where('process_steps', 'owner',
- process.id_):
- step = ProcessStep.from_table_row(db_conn, row_)
- process.explicit_steps += [step] # pylint: disable=no-member
- for row_ in db_conn.row_where('process_step_suppressions', 'process',
- process.id_):
- step = ProcessStep.by_id(db_conn, row_[1])
- process.suppressed_steps += [step] # pylint: disable=no-member
for name in ('conditions', 'blockers', 'enables', 'disables'):
table = f'process_{name}'
assert isinstance(process.id_, int)
'process', process.id_):
target = getattr(process, name)
target += [Condition.by_id(db_conn, c_id)]
+ for row_ in db_conn.row_where('process_steps', 'owner', process.id_):
+ step = ProcessStep.from_table_row(db_conn, row_)
+ process.explicit_steps += [step]
+ for row_ in db_conn.row_where('process_step_suppressions', 'process',
+ process.id_):
+ step = ProcessStep.by_id(db_conn, row_[1])
+ process.suppressed_steps += [step]
+ process.n_owners = len(process.used_as_step_by(db_conn))
return process
def used_as_step_by(self, db_conn: DatabaseConnection) -> list[Process]:
walk_steps(step)
assert isinstance(self.id_, int)
- for step in self.explicit_steps:
- step.uncache()
- self.explicit_steps = []
- db_conn.delete_where('process_steps', 'owner', self.id_)
- for step in steps:
- step.save(db_conn)
+ for step in [s for s in self.explicit_steps if s not in steps]:
+ step.remove(db_conn)
+ for step in [s for s in steps if s not in self.explicit_steps]:
if step.parent_step_id is not None:
try:
parent_step = ProcessStep.by_id(db_conn,
except NotFoundException:
step.parent_step_id = None
walk_steps(step)
- self.explicit_steps += [step]
+ step.save(db_conn)
+
+ def set_owners(self, db_conn: DatabaseConnection,
+ owner_ids: list[int]) -> None:
+ """Re-set owners to those identified in owner_ids."""
+ owners_old = self.used_as_step_by(db_conn)
+ losers = [o for o in owners_old if o.id_ not in owner_ids]
+ owners_old_ids = [o.id_ for o in owners_old]
+ winners = [Process.by_id(db_conn, id_) for id_ in owner_ids
+ if id_ not in owners_old_ids]
+ steps_to_remove = []
+ for loser in losers:
+ steps_to_remove += [s for s in loser.explicit_steps
+ if s.step_process_id == self.id_]
+ for step in steps_to_remove:
+ step.remove(db_conn)
+ for winner in winners:
+ assert isinstance(winner.id_, int)
+ assert isinstance(self.id_, int)
+ new_step = ProcessStep(None, winner.id_, self.id_, None)
+ new_explicit_steps = winner.explicit_steps + [new_step]
+ winner.set_steps(db_conn, new_explicit_steps)
def save(self, db_conn: DatabaseConnection) -> None:
"""Add (or re-write) self and connected items to DB."""
self.step_process_id = step_process_id
self.parent_step_id = parent_step_id
+ def save(self, db_conn: DatabaseConnection) -> None:
+ """Update into DB/cache, and owner's .explicit_steps."""
+ super().save(db_conn)
+ owner = Process.by_id(db_conn, self.owner_id)
+ if self not in owner.explicit_steps:
+ for s in [s for s in owner.explicit_steps if s.id_ == self.id_]:
+ s.remove(db_conn)
+ owner.explicit_steps += [self]
+ owner.explicit_steps.sort(key=hash)
+
def remove(self, db_conn: DatabaseConnection) -> None:
"""Remove from DB, and owner's .explicit_steps."""
owner = Process.by_id(db_conn, self.owner_id)