Added Draft Melodies. Further improvements to the UI

This commit is contained in:
2026-02-18 19:33:59 +02:00
parent a6e0b1d46e
commit aad8942d65
12 changed files with 1080 additions and 518 deletions

View File

@@ -1,10 +1,14 @@
import json
import uuid
import logging
from shared.firebase import get_db, get_bucket
from shared.firebase import get_db as get_firestore, get_bucket
from shared.exceptions import NotFoundError
from melodies.models import MelodyCreate, MelodyUpdate, MelodyInDB
from melodies import database as melody_db
COLLECTION = "melodies"
logger = logging.getLogger("melodies.service")
def _parse_localized_string(value: str) -> dict:
@@ -26,109 +30,171 @@ def _doc_to_melody(doc) -> MelodyInDB:
return MelodyInDB(id=doc.id, **data)
def list_melodies(
def _row_to_melody(row: dict) -> MelodyInDB:
"""Convert a SQLite row (with parsed data) to a MelodyInDB model."""
data = row["data"]
return MelodyInDB(id=row["id"], status=row["status"], **data)
def _matches_filters(melody: MelodyInDB, search: str | None, tone: str | None,
total_notes: int | None) -> bool:
"""Apply client-side filters to a melody."""
if tone and melody.information.melodyTone.value != tone:
return False
if total_notes is not None and melody.information.totalNotes != total_notes:
return False
if search:
search_lower = search.lower()
name_dict = _parse_localized_string(melody.information.name)
desc_dict = _parse_localized_string(melody.information.description)
name_match = any(
search_lower in v.lower() for v in name_dict.values() if isinstance(v, str)
)
desc_match = any(
search_lower in v.lower() for v in desc_dict.values() if isinstance(v, str)
)
tag_match = any(search_lower in t.lower() for t in melody.information.customTags)
if not (name_match or desc_match or tag_match):
return False
return True
async def list_melodies(
search: str | None = None,
melody_type: str | None = None,
tone: str | None = None,
total_notes: int | None = None,
status: str | None = None,
) -> list[MelodyInDB]:
"""List melodies with optional filters."""
db = get_db()
ref = db.collection(COLLECTION)
# Firestore doesn't support full-text search, so we fetch and filter in-memory
# for the name search. Type/tone/totalNotes can be queried server-side.
query = ref
if melody_type:
query = query.where("type", "==", melody_type)
docs = query.stream()
"""List melodies from SQLite with optional filters."""
rows = await melody_db.list_melodies(status=status)
results = []
for doc in docs:
melody = _doc_to_melody(doc)
for row in rows:
melody = _row_to_melody(row)
# Client-side filters
if tone and melody.information.melodyTone.value != tone:
# Server-side type filter
if melody_type and melody.type.value != melody_type:
continue
if total_notes is not None and melody.information.totalNotes != total_notes:
if not _matches_filters(melody, search, tone, total_notes):
continue
if search:
search_lower = search.lower()
name_dict = _parse_localized_string(melody.information.name)
desc_dict = _parse_localized_string(melody.information.description)
name_match = any(
search_lower in v.lower()
for v in name_dict.values()
if isinstance(v, str)
)
desc_match = any(
search_lower in v.lower()
for v in desc_dict.values()
if isinstance(v, str)
)
tag_match = any(search_lower in t.lower() for t in melody.information.customTags)
if not (name_match or desc_match or tag_match):
continue
results.append(melody)
return results
def get_melody(melody_id: str) -> MelodyInDB:
"""Get a single melody by document ID."""
db = get_db()
doc = db.collection(COLLECTION).document(melody_id).get()
if not doc.exists:
raise NotFoundError("Melody")
return _doc_to_melody(doc)
async def get_melody(melody_id: str) -> MelodyInDB:
"""Get a single melody by ID. Checks SQLite first."""
row = await melody_db.get_melody(melody_id)
if row:
return _row_to_melody(row)
raise NotFoundError("Melody")
def create_melody(data: MelodyCreate) -> MelodyInDB:
"""Create a new melody document in Firestore."""
db = get_db()
async def create_melody(data: MelodyCreate, publish: bool = False) -> MelodyInDB:
"""Create a new melody. If publish=True, also push to Firestore."""
melody_id = str(uuid.uuid4())
doc_data = data.model_dump()
_, doc_ref = db.collection(COLLECTION).add(doc_data)
return MelodyInDB(id=doc_ref.id, **doc_data)
status = "published" if publish else "draft"
# Always save to SQLite
await melody_db.insert_melody(melody_id, status, doc_data)
# If publishing, also save to Firestore
if publish:
db = get_firestore()
db.collection(COLLECTION).document(melody_id).set(doc_data)
return MelodyInDB(id=melody_id, status=status, **doc_data)
def update_melody(melody_id: str, data: MelodyUpdate) -> MelodyInDB:
"""Update an existing melody document. Only provided fields are updated."""
db = get_db()
doc_ref = db.collection(COLLECTION).document(melody_id)
doc = doc_ref.get()
if not doc.exists:
async def update_melody(melody_id: str, data: MelodyUpdate) -> MelodyInDB:
"""Update an existing melody. If published, also update Firestore."""
row = await melody_db.get_melody(melody_id)
if not row:
raise NotFoundError("Melody")
existing_data = row["data"]
update_data = data.model_dump(exclude_none=True)
# For nested structs, merge with existing data rather than replacing
existing = doc.to_dict()
# Merge nested structs
for key in ("information", "default_settings"):
if key in update_data and key in existing:
merged = {**existing[key], **update_data[key]}
if key in update_data and key in existing_data:
merged = {**existing_data[key], **update_data[key]}
update_data[key] = merged
doc_ref.update(update_data)
merged_data = {**existing_data, **update_data}
updated_doc = doc_ref.get()
return _doc_to_melody(updated_doc)
# Update SQLite
await melody_db.update_melody(melody_id, merged_data)
# If published, also update Firestore
if row["status"] == "published":
db = get_firestore()
doc_ref = db.collection(COLLECTION).document(melody_id)
doc_ref.set(merged_data)
return MelodyInDB(id=melody_id, status=row["status"], **merged_data)
def delete_melody(melody_id: str) -> None:
"""Delete a melody document and its associated storage files."""
db = get_db()
doc_ref = db.collection(COLLECTION).document(melody_id)
doc = doc_ref.get()
if not doc.exists:
async def publish_melody(melody_id: str) -> MelodyInDB:
"""Publish a draft melody to Firestore."""
row = await melody_db.get_melody(melody_id)
if not row:
raise NotFoundError("Melody")
# Delete associated storage files
doc_data = row["data"]
# Write to Firestore
db = get_firestore()
db.collection(COLLECTION).document(melody_id).set(doc_data)
# Update status in SQLite
await melody_db.update_status(melody_id, "published")
return MelodyInDB(id=melody_id, status="published", **doc_data)
async def unpublish_melody(melody_id: str) -> MelodyInDB:
"""Remove melody from Firestore but keep in SQLite as draft."""
row = await melody_db.get_melody(melody_id)
if not row:
raise NotFoundError("Melody")
# Delete from Firestore
db = get_firestore()
doc_ref = db.collection(COLLECTION).document(melody_id)
doc = doc_ref.get()
if doc.exists:
doc_ref.delete()
# Update status in SQLite
await melody_db.update_status(melody_id, "draft")
return MelodyInDB(id=melody_id, status="draft", **row["data"])
async def delete_melody(melody_id: str) -> None:
"""Delete a melody from SQLite and Firestore (if published), plus storage files."""
row = await melody_db.get_melody(melody_id)
if not row:
raise NotFoundError("Melody")
# Delete from Firestore if published
if row["status"] == "published":
db = get_firestore()
doc_ref = db.collection(COLLECTION).document(melody_id)
doc = doc_ref.get()
if doc.exists:
doc_ref.delete()
# Delete storage files
_delete_storage_files(melody_id)
doc_ref.delete()
# Delete from SQLite
await melody_db.delete_melody(melody_id)
def upload_file(melody_id: str, file_bytes: bytes, filename: str, content_type: str) -> str:
@@ -137,11 +203,9 @@ def upload_file(melody_id: str, file_bytes: bytes, filename: str, content_type:
if not bucket:
raise RuntimeError("Firebase Storage not initialized")
# Determine subfolder based on content type
if content_type in ("application/octet-stream", "application/macbinary"):
storage_path = f"melodies/{melody_id}/binary.bin"
else:
# Audio preview files
ext = filename.rsplit(".", 1)[-1] if "." in filename else "mp3"
storage_path = f"melodies/{melody_id}/preview.{ext}"
@@ -197,3 +261,24 @@ def get_storage_files(melody_id: str) -> dict:
result["preview_url"] = blob.public_url
return result
async def migrate_from_firestore() -> int:
"""One-time migration: import existing Firestore melodies into SQLite as published.
Only runs if the melody_drafts table is empty."""
count = await melody_db.count_melodies()
if count > 0:
return 0
db = get_firestore()
docs = db.collection(COLLECTION).stream()
imported = 0
for doc in docs:
data = doc.to_dict()
await melody_db.insert_melody(doc.id, "published", data)
imported += 1
if imported > 0:
logger.info(f"Migrated {imported} melodies from Firestore to SQLite")
return imported