feat(backend): products and categories point at the media library (platform-v2 step 5.2)

- products.media_id / categories.media_id (Alembic 0027): the old
  /static/product_images photos are processed into the library (named after
  their product; missing or unreadable files logged and skipped; the old
  folder is left on disk) and products.image_url goes. The static mount is gone.
- APIs give `image` {id, name, thumb, card, full}; a product's image_url is its
  card size. media_id on create/update (null clears); media.not_a_picture for
  a video. POST /api/products/{id}/image is gone: uploads go to /api/media.
- Products and categories register their media usage: a photo in use can't
  be deleted.
- Media: AVIF pictures (Pillow 12.3), GET /api/media/{id} gives each stored
  size's bytes and resolution and the uploader's name; upload processing moved
  into services/media.ingest (also used by the data move).
- QR menu photo push (contract C-3): the library's card size, WebP.
- tests: test_product_photos.py (7, incl. the 0027 data move); 497 passed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-17 15:57:50 +02:00
co-authored by Claude Opus 5.5
parent 238f8ec5a0
commit 5ad4c4ef55
15 changed files with 398 additions and 99 deletions
+7 -3
View File
@@ -67,6 +67,10 @@ def test_a_jpeg_gets_three_sizes_served_with_cache_headers(client, owner_h, medi
assert (img.format, img.size[0]) == ("WEBP", side) and abs(img.size[1] - side * 2 / 3) <= 1
orig = client.get(m["urls"]["original"])
assert orig.headers["content-type"] == "image/jpeg" and len(orig.content) == m["bytes"]
detail = client.get(f"/api/media/{m['id']}", headers=owner_h).json()
assert detail["uploaded_by_name"] and detail["used_by"] == []
assert {k: (v["width"], v["height"]) for k, v in detail["files"].items()} == {"thumb": (200, 133), "card": (600, 400), "full": (1920, 1280), "original": (3000, 2000)}
assert detail["files"]["original"]["bytes"] == m["bytes"] and detail["files"]["thumb"]["bytes"] < detail["files"]["card"]["bytes"]
sha = m["sha256"]
assert files_left(media_dir) == [f"{sha[:2]}/{sha}/{n}" for n in ("card.webp", "full.webp", "original.jpg", "thumb.webp")]
@@ -146,17 +150,17 @@ def test_delete_frees_the_files_but_a_file_in_use_is_refused(client, owner_h, me
import services.media as store
used = up(client, owner_h, jpeg(300, 300, (9, 9, 9)), name="used.jpg").json()
free = up(client, owner_h, jpeg(300, 300, (8, 8, 8)), name="free.jpg").json()
store.register_usage("product", lambda db, mid: [{"id": 7, "name": "Pork souvlaki"}] if mid == used["id"] else [])
store.register_usage("stand_in", lambda db, mid: [{"id": 7, "name": "Pork souvlaki"}] if mid == used["id"] else [])
try:
r = client.delete(f"/api/media/{used['id']}", headers=owner_h)
assert r.status_code == 409 and err(r) == "media.in_use"
assert r.json()["error"]["params"]["used_by"] == [{"kind": "product", "id": 7, "name": "Pork souvlaki"}]
assert r.json()["error"]["params"]["used_by"] == [{"kind": "stand_in", "id": 7, "name": "Pork souvlaki"}]
assert "Pork souvlaki" in r.json()["error"]["message"]
assert client.get(f"/api/media/{used['id']}", headers=owner_h).json()["used_by"][0]["name"] == "Pork souvlaki"
assert client.get(used["urls"]["card"]).status_code == 200
assert client.delete(f"/api/media/{free['id']}", headers=owner_h).status_code == 204
finally:
store.unregister_usage("product")
store.unregister_usage("stand_in")
assert client.get(f"/api/media/{free['id']}", headers=owner_h).status_code == 404
assert client.get(free["urls"]["card"]).status_code == 404
assert not any(free["sha256"] in f for f in files_left(media_dir))