Duplicate flows and dashboards: POST /flows/{name}/duplicate and /dashboards/{name}/duplicate copy the working copy into {name}_copy (then _copy_2, ...) as an unpublished draft with " (copy)" on the title. A flow copy takes its node sources as drafts and leaves origin and runtime.json behind; copying code nodes is a superuser's (the save check, now taking the replaced copy). Client regenerated
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -824,6 +824,32 @@ class DashboardStore:
|
||||
self.flows._commit(f"Rename dashboard '{name}' to '{new_name}'")
|
||||
return renamed
|
||||
|
||||
def duplicate(self, name: str) -> DashboardDef:
|
||||
"""Copy the working copy into a new dashboard, as a draft only.
|
||||
|
||||
Named ``{name}_copy``, then ``_copy_2`` and on. Unpublished like a new
|
||||
dashboard, so no panel shows it until someone publishes it.
|
||||
"""
|
||||
with self._lock, self.flows._write_lock:
|
||||
source = self.read(name, draft=True)
|
||||
new_name, count = f"{name}_copy", 1
|
||||
while self.exists(new_name):
|
||||
count += 1
|
||||
new_name = f"{name}_copy_{count}"
|
||||
copy = source.model_copy(
|
||||
update={
|
||||
"name": new_name,
|
||||
"title": f"{source.title} (copy)" if source.title else "",
|
||||
"version": 1,
|
||||
"has_draft": True,
|
||||
}
|
||||
)
|
||||
path = self._draft_file(new_name)
|
||||
path.parent.mkdir(parents=True, exist_ok=True)
|
||||
path.write_text(self._dump(copy))
|
||||
self.flows._commit(f"Duplicate dashboard '{name}' as '{new_name}'")
|
||||
return copy
|
||||
|
||||
def bindings_for(self, flow: str) -> Bindings:
|
||||
"""Every widget bound to a message of ``flow``.
|
||||
|
||||
|
||||
@@ -674,6 +674,46 @@ class FlowStore:
|
||||
self._commit(f"Rename flow '{name}' to '{new_name}'")
|
||||
return self.read_flow(new_name, draft=True)
|
||||
|
||||
def duplicate_flow(self, name: str) -> FlowDef:
|
||||
"""Copy a flow's working copy into a new flow that is only a draft.
|
||||
|
||||
Like a new flow, the copy runs nothing until someone publishes it. It is
|
||||
named ``{name}_copy``, then ``_copy_2`` and on. ``origin`` is dropped,
|
||||
since the copy is a local flow ``fluksio sync`` must not claim, and
|
||||
``runtime.json`` stays behind with everything else about running.
|
||||
"""
|
||||
with self._write_lock:
|
||||
source = self.read_flow(name, draft=True)
|
||||
new_name, count = f"{name}_copy", 1
|
||||
# The directory rather than `exists`: anything left in it would
|
||||
# otherwise end up in the copy.
|
||||
while self._flow_dir(new_name).exists():
|
||||
count += 1
|
||||
new_name = f"{name}_copy_{count}"
|
||||
|
||||
copy = source.model_copy(
|
||||
update={
|
||||
"name": new_name,
|
||||
"title": f"{source.title} (copy)" if source.title else "",
|
||||
"version": 1,
|
||||
"origin": None,
|
||||
}
|
||||
)
|
||||
path = self._draft_file(new_name)
|
||||
path.parent.mkdir(parents=True)
|
||||
path.write_text(copy.model_dump_json(indent=2) + "\n")
|
||||
|
||||
# The bodies the source's editor shows: published, edits over them.
|
||||
# As drafts, so publishing the copy promotes them like a new flow's.
|
||||
target = self._draft_nodes_dir(new_name)
|
||||
for folder in (self._flow_dir(name) / "nodes", self._draft_nodes_dir(name)):
|
||||
for body in sorted(folder.glob("*.py")):
|
||||
target.mkdir(exist_ok=True)
|
||||
shutil.copy2(body, target / body.name)
|
||||
|
||||
self._commit(f"Duplicate flow '{name}' as '{new_name}'")
|
||||
return copy
|
||||
|
||||
@staticmethod
|
||||
def _retarget(flow: FlowDef, old_prefix: str, new_prefix: str) -> bool:
|
||||
"""Point this flow's cross-flow message names at a renamed flow."""
|
||||
|
||||
Reference in New Issue
Block a user