Changed Localization to JSON String
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import json
|
||||
|
||||
from shared.firebase import get_db, get_bucket
|
||||
from shared.exceptions import NotFoundError
|
||||
from melodies.models import MelodyCreate, MelodyUpdate, MelodyInDB
|
||||
@@ -5,16 +7,22 @@ from melodies.models import MelodyCreate, MelodyUpdate, MelodyInDB
|
||||
COLLECTION = "melodies"
|
||||
|
||||
|
||||
def _parse_localized_string(value: str) -> dict:
|
||||
"""Parse a JSON-encoded localized string into a dict. Returns {} on failure."""
|
||||
if not value:
|
||||
return {}
|
||||
try:
|
||||
parsed = json.loads(value)
|
||||
if isinstance(parsed, dict):
|
||||
return parsed
|
||||
except (json.JSONDecodeError, TypeError):
|
||||
pass
|
||||
return {}
|
||||
|
||||
|
||||
def _doc_to_melody(doc) -> MelodyInDB:
|
||||
"""Convert a Firestore document snapshot to a MelodyInDB model."""
|
||||
data = doc.to_dict()
|
||||
# Backward compat: if name/description are plain strings, wrap as dict
|
||||
info = data.get("information", {})
|
||||
if isinstance(info.get("name"), str):
|
||||
info["name"] = {"en": info["name"]} if info["name"] else {}
|
||||
if isinstance(info.get("description"), str):
|
||||
info["description"] = {"en": info["description"]} if info["description"] else {}
|
||||
data["information"] = info
|
||||
return MelodyInDB(id=doc.id, **data)
|
||||
|
||||
|
||||
@@ -48,14 +56,16 @@ def list_melodies(
|
||||
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 melody.information.name.values()
|
||||
for v in name_dict.values()
|
||||
if isinstance(v, str)
|
||||
)
|
||||
desc_match = any(
|
||||
search_lower in v.lower()
|
||||
for v in melody.information.description.values()
|
||||
for v in desc_dict.values()
|
||||
if isinstance(v, str)
|
||||
)
|
||||
tag_match = any(search_lower in t.lower() for t in melody.information.customTags)
|
||||
|
||||
Reference in New Issue
Block a user