44 lines
1.1 KiB
Python
44 lines
1.1 KiB
Python
import logging
|
|
|
|
from django.core.files.base import ContentFile
|
|
|
|
from tasks.media import attachment_media_kind, render_attachment_thumbnail
|
|
|
|
from .models import ProcurementItemAttachment
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def procurement_attachment_media_kind(
|
|
attachment: ProcurementItemAttachment,
|
|
) -> str | None:
|
|
return attachment_media_kind(attachment)
|
|
|
|
|
|
def ensure_procurement_attachment_thumbnail(
|
|
attachment: ProcurementItemAttachment,
|
|
) -> bool:
|
|
if attachment.thumbnail:
|
|
return True
|
|
if not procurement_attachment_media_kind(attachment):
|
|
return False
|
|
|
|
try:
|
|
thumbnail = render_attachment_thumbnail(attachment)
|
|
attachment.thumbnail.save(
|
|
(
|
|
f"procurement-item-{attachment.item_id}-"
|
|
f"attachment-{attachment.id}.webp"
|
|
),
|
|
ContentFile(thumbnail),
|
|
save=True,
|
|
)
|
|
return True
|
|
except Exception:
|
|
logger.exception(
|
|
"Could not generate thumbnail for procurement attachment %s",
|
|
attachment.id,
|
|
)
|
|
return False
|