mirror of
https://github.com/pewdiepie-archdaemon/odysseus.git
synced 2026-06-30 16:42:15 -04:00
Normalize gallery CLI text fields (#2012)
This commit is contained in:
+15
-11
@@ -38,23 +38,27 @@ def _preview_text(value, limit: int = 200) -> str:
|
|||||||
return text[:limit]
|
return text[:limit]
|
||||||
|
|
||||||
|
|
||||||
|
def _text_field(value) -> str:
|
||||||
|
return value if isinstance(value, str) else ""
|
||||||
|
|
||||||
|
|
||||||
def _serialize_image(i: "GalleryImage") -> dict:
|
def _serialize_image(i: "GalleryImage") -> dict:
|
||||||
return {
|
return {
|
||||||
"id": i.id,
|
"id": i.id,
|
||||||
"filename": i.filename,
|
"filename": _text_field(i.filename),
|
||||||
"prompt": _preview_text(i.prompt),
|
"prompt": _preview_text(i.prompt),
|
||||||
"model": i.model or "",
|
"model": _text_field(i.model),
|
||||||
"size": i.size or "",
|
"size": _text_field(i.size),
|
||||||
"tags": i.tags or "",
|
"tags": _text_field(i.tags),
|
||||||
"favorite": bool(i.favorite),
|
"favorite": bool(i.favorite),
|
||||||
"album_id": i.album_id or "",
|
"album_id": _text_field(i.album_id),
|
||||||
"session_id": i.session_id or "",
|
"session_id": _text_field(i.session_id),
|
||||||
"width": i.width,
|
"width": i.width,
|
||||||
"height": i.height,
|
"height": i.height,
|
||||||
"file_size": i.file_size,
|
"file_size": i.file_size,
|
||||||
"taken_at": i.taken_at.isoformat() if i.taken_at else "",
|
"taken_at": i.taken_at.isoformat() if i.taken_at else "",
|
||||||
"camera_make": i.camera_make or "",
|
"camera_make": _text_field(i.camera_make),
|
||||||
"camera_model": i.camera_model or "",
|
"camera_model": _text_field(i.camera_model),
|
||||||
"created_at": i.created_at.isoformat() if i.created_at else "",
|
"created_at": i.created_at.isoformat() if i.created_at else "",
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,11 +97,11 @@ def cmd_show(args):
|
|||||||
if not i:
|
if not i:
|
||||||
fail(f"no image with id {args.id!r}")
|
fail(f"no image with id {args.id!r}")
|
||||||
out = _serialize_image(i)
|
out = _serialize_image(i)
|
||||||
out["prompt_full"] = i.prompt or ""
|
out["prompt_full"] = _text_field(i.prompt)
|
||||||
out["ai_tags"] = i.ai_tags or ""
|
out["ai_tags"] = _text_field(i.ai_tags)
|
||||||
out["gps_lat"] = i.gps_lat or ""
|
out["gps_lat"] = i.gps_lat or ""
|
||||||
out["gps_lng"] = i.gps_lng or ""
|
out["gps_lng"] = i.gps_lng or ""
|
||||||
out["file_hash"] = i.file_hash or ""
|
out["file_hash"] = _text_field(i.file_hash)
|
||||||
emit(out, args)
|
emit(out, args)
|
||||||
finally:
|
finally:
|
||||||
db.close()
|
db.close()
|
||||||
|
|||||||
@@ -15,16 +15,21 @@ def test_preview_text_ignores_non_string(monkeypatch):
|
|||||||
assert cli._preview_text(None) == ""
|
assert cli._preview_text(None) == ""
|
||||||
assert cli._preview_text(123) == ""
|
assert cli._preview_text(123) == ""
|
||||||
assert cli._preview_text("p" * 250) == "p" * 200
|
assert cli._preview_text("p" * 250) == "p" * 200
|
||||||
|
assert cli._text_field("ok") == "ok"
|
||||||
|
assert cli._text_field(123) == ""
|
||||||
|
|
||||||
|
|
||||||
def test_serialize_image_does_not_crash_on_non_string_prompt(monkeypatch):
|
def test_serialize_image_does_not_crash_on_non_string_prompt(monkeypatch):
|
||||||
make_core_db_stub(monkeypatch, models=["GalleryImage", "GalleryAlbum"])
|
make_core_db_stub(monkeypatch, models=["GalleryImage", "GalleryAlbum"])
|
||||||
cli = load_script("odysseus-gallery")
|
cli = load_script("odysseus-gallery")
|
||||||
img = SimpleNamespace(
|
img = SimpleNamespace(
|
||||||
id="i1", filename="a.png", prompt=123, model=None, size=None, tags=None,
|
id="i1", filename=123, prompt=123, model=123, size=None, tags=["bad"],
|
||||||
favorite=0, album_id=None, session_id=None, width=1, height=1, file_size=1,
|
favorite=0, album_id=None, session_id=None, width=1, height=1, file_size=1,
|
||||||
taken_at=None, camera_make=None, camera_model=None, created_at=None,
|
taken_at=None, camera_make=123, camera_model=None, created_at=None,
|
||||||
)
|
)
|
||||||
out = cli._serialize_image(img)
|
out = cli._serialize_image(img)
|
||||||
assert out["prompt"] == ""
|
assert out["prompt"] == ""
|
||||||
|
assert out["filename"] == ""
|
||||||
|
assert out["model"] == ""
|
||||||
|
assert out["tags"] == ""
|
||||||
assert out["id"] == "i1"
|
assert out["id"] == "i1"
|
||||||
|
|||||||
Reference in New Issue
Block a user