Added playlist folder management

This commit is contained in:
patben8 committed 2026-09-25 13:26:37 +02:00
1 parent efdd0f6203
commit 88737f2b08
9 files changed
+416 -118

No files matched your search

@@ -236,11 +236,8 @@ class RekordboxAdapter:
return self._store.create_playlist(name, parent_id)
def create_folder(self, name: str, parent_id: str | None = None) -> str:
# Rekordbox CAN hold folders (`capabilities.playlists.folders` is
# True, and pyrekordbox has `add_playlist_folder`), so this is a
# gap rather than a refusal in principle. Traktor is the only
# import target for now, so it is the only one that needed it.
self._refuse("Creating playlist folders")
self._require_writable("Creating playlist folders")
return self._store.create_folder(name, parent_id)
def rename_playlist(self, node_id: str, name: str) -> None:
self._require_writable("Renaming playlists")
@@ -391,6 +391,19 @@ class RekordboxStore:
self._journal.record("playlist", "create", name)
return str(row.ID)
def create_folder(self, name: str, parent_id: str | None = None) -> str:
name = (name or "").strip()
if not name:
raise InvalidCommand("A folder needs a name")
parent = self._playlist(parent_id) if parent_id else None
if parent is not None and int(parent.Attribute or 0) != int(
self._tables.PlaylistType.FOLDER
):
raise InvalidCommand("A folder can only be created inside a folder")
row = self._db.create_playlist_folder(name, parent=parent)
self._journal.record("playlist", "create-folder", name)
return str(row.ID)
def rename_playlist(self, node_id: str, name: str) -> None:
name = (name or "").strip()
if not name:
+46 -1
View File
@@ -168,6 +168,8 @@ class TraktorStore:
kind="folder",
children=[self._to_model(c, path) for c in self._children(node)],
can_contain_children=True,
can_rename=True,
can_delete=True,
)
@staticmethod
@@ -252,6 +254,7 @@ class TraktorStore:
second import from the same stick should land beside the first rather
than next to an identically-named twin.
"""
self._check_folder_name(name)
with self._lock:
parent = self._find_folder(parent_id)
existing = next(
@@ -276,8 +279,38 @@ class TraktorStore:
self.dirty = True
return folder_id
@staticmethod
def _check_folder_name(name: str) -> None:
# A folder is addressed by its path of names, so a "/" in one would
# split it into two path segments and the folder could never be found.
if not name:
raise PlaylistError("A folder needs a name")
if "/" in name:
raise PlaylistError('A folder name cannot contain "/"')
def _rename_folder(self, folder_id: str, name: str) -> None:
"""Rename a folder. Its id is its path, so the id changes with it —
and a sibling folder of the same name would make both unaddressable,
so that is refused rather than silently merged."""
self._check_folder_name(name)
node = self._find_folder(folder_id)
if node is self._root():
raise PlaylistError("Cannot rename the root folder")
parent = self._find_parent_of(node)
if any(
c is not node and (c.type or "FOLDER") == "FOLDER" and c.name == name
for c in self._children(parent)
):
raise PlaylistError(f'There is already a folder called "{name}" here')
node.name = name
self._note("playlist-rename", name)
self.dirty = True
def rename_playlist(self, playlist_uuid: str, name: str) -> None:
with self._lock:
if playlist_uuid.startswith("fld:"):
self._rename_folder(playlist_uuid, name)
return
node = self._find_playlist_node(playlist_uuid)
if node is None:
raise PlaylistError(f"Playlist not found: {playlist_uuid}")
@@ -286,8 +319,20 @@ class TraktorStore:
self.dirty = True
def delete_playlist(self, playlist_uuid: str) -> None:
"""Delete a playlist, or a FOLDER and everything nested in it.
A folder is addressed by its synthetic `fld:` path id. Removing its node
removes its subtree with it — the NML nests children inside the parent's
SUBNODES, so there is nothing elsewhere to clean up; the tracks stay in
the collection.
"""
with self._lock:
node = self._find_playlist_node(playlist_uuid)
if playlist_uuid.startswith("fld:"):
if playlist_uuid == "fld:":
raise PlaylistError("Cannot delete the root folder")
node = self._find_folder(playlist_uuid)
else:
node = self._find_playlist_node(playlist_uuid)
if node is None:
raise PlaylistError(f"Playlist not found: {playlist_uuid}")
parent = self._find_parent_of(node)
+19 -2
View File
@@ -721,13 +721,30 @@ def create_playlist(body: CreatePlaylist) -> PlaylistNode:
)
@app.patch("/api/playlists/{playlist_uuid}")
@app.post("/api/playlists/folders", response_model=PlaylistNode)
def create_folder(body: CreatePlaylist) -> PlaylistNode:
name = body.name.strip() or "New Folder"
new_id = require_adapter().create_folder(name, body.parent_id)
return PlaylistNode(
id=new_id,
name=name,
kind="folder",
can_contain_children=True,
can_rename=True,
can_delete=True,
)
# `:path` for the same reason as delete: a folder id is a path of names.
@app.patch("/api/playlists/{playlist_uuid:path}")
def rename_playlist(playlist_uuid: str, body: RenamePlaylist) -> dict:
require_adapter().rename_playlist(playlist_uuid, body.name.strip())
return {"status": "renamed", "id": playlist_uuid, "name": body.name}
@app.delete("/api/playlists/{playlist_uuid}")
# `:path` because a folder id is a path of names (`fld:a/b`), and the slash
# would otherwise end the segment.
@app.delete("/api/playlists/{playlist_uuid:path}")
def delete_playlist(playlist_uuid: str) -> dict:
require_adapter().delete_playlist(playlist_uuid)
return {"status": "deleted", "id": playlist_uuid}
+38
View File
@@ -134,6 +134,44 @@ still = [
check("playlist gone after delete+save", len(still) == 0)
check("COLLECTION still byte-identical", collection_span(WORK.read_bytes()) == collection_before)
print("== folders: create, rename, delete (nested ids contain a slash) ==")
outer = client.post("/api/playlists/folders", json={"name": "Konduktor Test Folder"}).json()["id"]
inner = client.post(
"/api/playlists/folders", json={"name": "Inner Draft", "parent_id": outer}
).json()["id"]
check("nested folder id is its path", inner == "fld:Konduktor Test Folder/Inner Draft")
r = client.patch(f"/api/playlists/{inner.replace('/', '%2F')}", json={"name": "Inner"})
check("nested folder rename accepted", r.status_code == 200)
inner = "fld:Konduktor Test Folder/Inner"
nodes = flat(client.get("/api/playlists").json())
check("renamed folder found by its new path", any(n["id"] == inner for n in nodes))
check("folder name with a slash refused",
client.post("/api/playlists/folders", json={"name": "a/b"}).status_code == 400)
client.post("/api/playlists/folders", json={"name": "Sibling", "parent_id": outer})
check("rename onto a sibling folder's name refused",
client.patch(f"/api/playlists/{outer}%2FSibling", json={"name": "Inner"}).status_code == 400)
nested_uuid = client.post(
"/api/playlists", json={"name": "Nested", "parent_id": inner}
).json()["id"]
nodes = flat(client.get("/api/playlists").json())
check("nested folder offered as deletable",
any(n["id"] == inner and n["can_delete"] for n in nodes))
r = client.delete(f"/api/playlists/{inner.replace('/', '%2F')}")
check("nested folder delete accepted", r.status_code == 200)
nodes = flat(client.get("/api/playlists").json())
check("folder and its playlist gone",
not any(n["id"] in (inner, nested_uuid) for n in nodes))
check("parent folder kept", any(n["id"] == outer for n in nodes))
client.delete(f"/api/playlists/{outer}")
client.post("/api/save")
coll3 = TraktorCollection(path=WORK)
check("no test folder in saved file",
not any(n.name == "Konduktor Test Folder" for n in walk(coll3.nml.playlists.node)))
check("COLLECTION still byte-identical after folder delete",
collection_span(WORK.read_bytes()) == collection_before)
check("unknown folder is 404",
client.delete("/api/playlists/fld:No Such Folder").status_code == 404)
print("== original untouched ==")
check(
"original collection.nml never modified",
+19
View File
@@ -237,6 +237,25 @@ with tempfile.TemporaryDirectory() as d:
len(final.playlist_tree()) == len(before_tree),
f"{len(final.playlist_tree())} vs {len(before_tree)}")
# A folder, a playlist inside it, and deleting the folder takes both.
folder_id = final.create_folder("Konduktor Test Folder")
inner_id = final.create_playlist("Konduktor Inner", folder_id)
final.save()
folder_reopened = RekordboxAdapter(work)
folder = next((n for n in folder_reopened.playlist_tree() if n.id == folder_id), None)
check("a folder round-trips as a folder",
folder is not None and folder.kind == "folder")
check("a playlist created inside it is its child",
folder is not None and [c.id for c in folder.children] == [inner_id])
folder_reopened.delete_playlist(folder_id)
folder_reopened.save()
after_folder = RekordboxAdapter(work)
flat_ids = lambda ns: [x for n in ns for x in [n.id] + flat_ids(n.children)]
check("deleting the folder removes it AND its playlist",
not {folder_id, inner_id} & set(flat_ids(after_folder.playlist_tree())))
folder_reopened.close()
after_folder.close()
# ---- F: a cue write touches the cue row AND its mirror ---------------
print("== F: a hot cue write is localized, and keeps the mirror in step ==")
clean_copy(REAL, work, closing=[adapter, again, final, reopened])