collection list, move, delete

This commit is contained in:
michalcourson
2026-02-20 20:21:08 -05:00
parent d6f4d4166b
commit 60355d176c
18 changed files with 437 additions and 2064 deletions

View File

@ -104,10 +104,7 @@ class AudioRecorder:
"volume": 1.0,
}
meta.add_clip_to_collection("Uncategorized",
{
clip_metadata
})
meta.add_clip_to_collection("Uncategorized", clip_metadata )
return clip_metadata

View File

@ -17,71 +17,82 @@ class MetaDataManager:
self.collections = json.load(f)
else:
self.collections = {}
if(collections := self.collections.get("Uncategorized")) is None:
self.collections["Uncategorized"] = []
if(collections := next((c for c in self.collections if c.get("name") == "Uncategorized"), None)) is None:
self.collections.append({"name": "Uncategorized", "id": 0, "clips": []})
self.save_metadata()
def create_collection(self, name):
if name in self.collections:
if any(c.get("name") == name for c in self.collections):
raise ValueError(f"Collection '{name}' already exists.")
self.collections[name] = []
new_id = max((c.get("id", 0) for c in self.collections), default=0) + 1
self.collections.append({"name": name, "id": new_id, "clips": []})
self.save_metadata()
def delete_collection(self, name):
if name not in self.collections:
collection = next((c for c in self.collections if c.get("name") == name), None)
if collection is None:
raise ValueError(f"Collection '{name}' does not exist.")
del self.collections[name]
self.collections.remove(collection)
self.save_metadata()
def add_clip_to_collection(self, collection_name, clip_metadata):
if collection_name not in self.collections:
collection = next((c for c in self.collections if c.get("name") == collection_name), None)
if collection is None:
raise ValueError(f"Collection '{collection_name}' does not exist.")
self.collections[collection_name].append(clip_metadata)
collection["clips"].append(clip_metadata)
self.save_metadata()
def remove_clip_from_collection(self, collection_name, clip_metadata):
if collection_name not in self.collections:
collection = next((c for c in self.collections if c.get("name") == collection_name), None)
if collection is None:
raise ValueError(f"Collection '{collection_name}' does not exist.")
# Remove all clips with the same file name as clip_metadata["file_name"]
in_list = any(clip.get("filename") == clip_metadata.get("filename") for clip in self.collections[collection_name])
in_list = any(clip.get("filename") == clip_metadata.get("filename") for clip in collection["clips"])
if not in_list:
raise ValueError(f"Clip with filename '{clip_metadata.get('filename')}' not found in collection '{collection_name}'.")
self.collections[collection_name] = [
clip for clip in self.collections[collection_name]
collection["clips"] = [
clip for clip in collection["clips"]
if clip.get("filename") != clip_metadata.get("filename")
]
self.save_metadata()
def move_clip_to_collection(self, source_collection, target_collection, clip_metadata):
self.remove_clip_from_collection(source_collection, clip_metadata)
self.add_clip_to_collection(target_collection, clip_metadata)
def edit_clip_in_collection(self, collection_name, new_clip_metadata):
if collection_name not in self.collections:
collection = next((c for c in self.collections if c.get("name") == collection_name), None)
if collection is None:
raise ValueError(f"Collection '{collection_name}' does not exist.")
# Find the index of the clip with the same file name as old_clip_metadata["file_name"]
index = next((i for i, clip in enumerate(self.collections[collection_name]) if clip.get("filename") == new_clip_metadata.get("filename")), None)
index = next((i for i, clip in enumerate(collection["clips"]) if clip.get("filename") == new_clip_metadata.get("filename")), None)
if index is None:
raise ValueError(f"Clip with filename '{new_clip_metadata.get('filename')}' not found in collection '{collection_name}'.")
self.collections[collection_name][index] = new_clip_metadata
collection["clips"][index] = new_clip_metadata
self.save_metadata()
def get_collections(self):
return list(self.collections.keys())
return list(map(lambda c: {"name": c.get("name"), "id": c.get("id")}, self.collections))
def get_clips_in_collection(self, collection_name):
if collection_name not in self.collections:
collection = next((c for c in self.collections if c.get("name") == collection_name), None)
if collection is None:
raise ValueError(f"Collection '{collection_name}' does not exist.")
return self.collections[collection_name]
return collection["clips"]
def reorder_clips_in_collection(self, collection_name, new_order):
if collection_name not in self.collections:
collection = next((c for c in self.collections if c.get("name") == collection_name), None)
if collection is None:
raise ValueError(f"Collection '{collection_name}' does not exist.")
existing_filenames = {clip.get("filename") for clip in self.collections[collection_name]}
existing_filenames = {clip.get("filename") for clip in collection["clips"]}
new_filenames = {clip.get("filename") for clip in new_order}
if not new_filenames.issubset(existing_filenames):
raise ValueError("New order contains clips that do not exist in the collection.")
self.collections[collection_name] = new_order
collection["clips"] = new_order
self.save_metadata()
def save_metadata(self):

View File

@ -73,6 +73,19 @@ def remove_clip_from_collection():
except ValueError as e:
return jsonify({'status': 'error', 'message': str(e)}), 400
@metadata_bp.route('/meta/collection/clips/move', methods=['POST'])
def move_clip_to_collection():
meta_manager = MetaDataManager()
sourceCollection = request.json.get('sourceCollection')
targetCollection = request.json.get('targetCollection')
clip_metadata = request.json.get('clip')
try:
meta_manager.move_clip_to_collection(sourceCollection, targetCollection, clip_metadata)
collections = meta_manager.collections
return jsonify({'status': 'success', 'collections': collections})
except ValueError as e:
return jsonify({'status': 'error', 'message': str(e)}), 400
@metadata_bp.route('/meta/collection/clips/edit', methods=['POST'])
def edit_clip_in_collection():
meta_manager = MetaDataManager()