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
View File
@@ -11,6 +11,7 @@ steps 5.2-5.4). Those users register a usage check in services/media.py so a fil
can't be deleted.
"""
from sqlalchemy import Column, ForeignKey, Integer, String
from sqlalchemy.orm import relationship
from core.timeutil import UTCDateTime, utcnow
from database import Base
@@ -31,3 +32,9 @@ class Media(Base):
duration_ms = Column(Integer, nullable=True) # videos (MP4) when the file says
uploaded_by = Column(Integer, ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
created_at = Column(UTCDateTime, default=utcnow, nullable=False)
uploader = relationship("User", lazy="joined")
@property
def uploaded_by_name(self):
return self.uploader.full_name or self.uploader.username if self.uploader is not None else None
+23 -2
View File
@@ -19,6 +19,14 @@ class Category(Base):
general_sort_order = Column(Integer, default=0, nullable=False)
# sub-categories only: if True, the accordion section is expanded by default on the PWA
auto_expanded = Column(Boolean, default=False, nullable=False)
# Its picture in the media library (plan step 5.2); the library refuses to delete it while set
media_id = Column(Integer, ForeignKey("media.id", ondelete="SET NULL"), nullable=True, index=True)
media = relationship("Media", lazy="joined")
@property
def image(self):
from services.media import image_of
return image_of(self.media)
# Products whose PRIMARY category this is (tickets, reports). Every product listed in
# this category, primary or not: `memberships` (ProductCategory, D-23).
@@ -45,7 +53,9 @@ class Product(Base):
lifecycle_status = Column(String, default="active", nullable=False)
# system products only (phase 3S, S.5): what the line is, e.g. "delivery_fee"
system_key = Column(String, nullable=True, unique=True, index=True)
image_url = Column(String, nullable=True)
# Its photo in the media library (plan step 5.2; the old image_url files moved there in 0027)
media_id = Column(Integer, ForeignKey("media.id", ondelete="SET NULL"), nullable=True, index=True)
media = relationship("Media", lazy="joined")
sort_order = Column(Integer, default=0, nullable=False)
# Where it's sold (phase 3S): JSON list of sales points (core/sales_points.py). Prices
@@ -57,7 +67,7 @@ class Product(Base):
digital_name = Column(String, nullable=True)
digital_description = Column(String, nullable=True)
digital_image_url = Column(String, nullable=True)
# sha256 of the image_url file last successfully pushed to the cloud — lets
# sha256 of the photo file (card size) last successfully pushed to the cloud — lets
# cloud_sync skip re-uploading unchanged images on every sync tick
cloud_image_hash = Column(String, nullable=True)
@@ -101,6 +111,17 @@ class Product(Base):
modifier_groups = relationship("ProductModifierGroup", back_populates="product", cascade="all, delete-orphan")
order_items = relationship("OrderItem", back_populates="product")
@property
def image(self):
"""{"id", "name", "thumb", "card", "full"} of its photo, or None."""
from services.media import image_of
return image_of(self.media)
@property
def image_url(self):
"""The card size of its photo (what the phones and the counter show), or None."""
return (self.image or {}).get("card")
class ProductModifierGroup(Base):
"""A named folder/group that can contain options, ingredients, or preference sets."""