fix: 优化垃圾桶异常批次缩略图

This commit is contained in:
hh
2026-07-16 18:24:00 +08:00
parent 538c5024e2
commit 5fc316112c
2 changed files with 116 additions and 8 deletions
+36 -7
View File
@@ -295,9 +295,36 @@ class AITaskViewSet(TeamScopedViewSetMixin, ReadOnlyModelViewSet):
if product_id:
product_ids.add(product_id)
# 异常批次缩略图只来自该批任务自己的成功成图。批次删除时这些资产会一起软删,
# 这里批量取回并按任务映射,避免逐批查询;商品封面不再参与缩略图回退。
preview_by_task_id = {}
task_ids = [task.id for task in tasks]
generated_assets = (
Asset.objects.filter(
team=self.get_team(),
origin_task_id__in=task_ids,
asset_type=Asset.Type.IMAGE,
is_deleted=True,
purged_at__isnull=True,
)
.prefetch_related("files")
.order_by("created_at", "id")
)
for asset in generated_assets:
files = sorted(
asset.files.all(),
key=lambda item: (not item.is_primary, item.created_at, str(item.id)),
)
for file in files:
preview_url = AssetFileSerializer(file).data.get("preview_url", "")
if preview_url:
preview_by_task_id.setdefault(asset.origin_task_id, preview_url)
break
# 商品仍只用于异常批次标题文字回退,不再关联或预取商品封面资源。
products = Product.objects.filter(
team=self.get_team(), id__in=product_ids, status=Product.Status.ACTIVE, purged_at__isnull=True
).select_related("cover_asset").prefetch_related("cover_asset__files")
).only("id", "title")
product_by_id = {str(product.id): product for product in products}
rows = []
@@ -307,12 +334,14 @@ class AITaskViewSet(TeamScopedViewSetMixin, ReadOnlyModelViewSet):
first = group[0]
payload = first.request_payload or {}
product = product_by_id.get(str(payload.get("product_id") or ""))
cover = ""
if product and product.cover_asset and not product.cover_asset.is_deleted and product.cover_asset.purged_at is None:
files = list(product.cover_asset.files.all())
primary = next((item for item in files if item.is_primary), files[0] if files else None)
if primary:
cover = AssetFileSerializer(primary).data.get("preview_url", "")
cover = next(
(
preview_by_task_id[task.id]
for task in group
if task.status == AITask.Status.SUCCEEDED and task.id in preview_by_task_id
),
"",
)
intended = sum(1 for task in group if not (task.request_payload or {}).get("batch_append"))
rows.append({
"id": str(first.id),