feat: integrate auto-photo-save into item creation endpoint

- Extend ItemCreate schema with optional extracted_image_bytes (base64) and image_processing (dict)
- Update create_item endpoint to call _auto_save_photo_from_extraction after item creation
- Decode base64 image bytes and pass crop_bounds, rotation_degrees to helper
- Don't block item creation if photo save fails (log warning instead)
- Item returned with photo_path, photo_thumbnail_path populated if save succeeded
- Full backward compatibility: old clients without image fields work unchanged
- Add 5 integration tests covering all scenarios:
  - Create item WITH image_processing → photo auto-saved
  - Create item WITHOUT image_processing → no photo (backward compatible)
  - Create item WITH invalid image_processing → item created, photo skipped
  - Create item WITH crop_bounds=None → item created, photo skipped
  - Create item WITH bytes but NO processing metadata → item created, photo skipped
- All 158 backend tests passing, zero regressions
This commit is contained in:
2026-04-21 19:00:06 +03:00
parent 20fc352f6f
commit 4f63b3b99e
6 changed files with 196 additions and 4 deletions

View File

@@ -140,11 +140,37 @@ def create_item(
db.add(models.Color(name=item.color))
db.commit()
db_item = models.Item(**item.model_dump())
# Exclude image_processing fields from database item creation (backward compatible)
item_data = item.model_dump(exclude={"extracted_image_bytes", "image_processing"})
db_item = models.Item(**item_data)
db.add(db_item)
db.commit()
db.refresh(db_item)
# NEW: Auto-save photo if extracted_image_bytes and image_processing provided
if item.extracted_image_bytes and item.image_processing:
try:
import base64
image_bytes = base64.b64decode(item.extracted_image_bytes)
photo_result = _auto_save_photo_from_extraction(
item_id=db_item.id,
image_bytes=image_bytes,
crop_bounds=item.image_processing.get("crop_bounds"),
rotation_degrees=item.image_processing.get("rotation_degrees", 0),
db=db
)
if photo_result["status"] == "ok":
db.refresh(db_item) # Reload to get updated photo fields
else:
from ..logger import log
log.warning(f"Photo auto-save skipped for item {db_item.id}: {photo_result.get('reason')}")
except Exception as e:
from ..logger import log
log.error(f"Exception during auto-save for item {db_item.id}: {e}")
# Don't fail item creation
# Audit log the creation — [M-02] user_id from token, not from body
# Capture full snapshot
item_snapshot = {