Save thumbnail file path in timeline and use it in serializer

remotes/origin/4.0rc
Álex Hermida 2018-09-19 11:36:19 +02:00 committed by Miguel Gonzalez
parent d55ee21e8e
commit 34c5bd40d3
3 changed files with 15 additions and 7 deletions

View File

@ -15,12 +15,13 @@
from django.conf import settings from django.conf import settings
from taiga.base.utils.thumbnails import get_thumbnail_url from taiga.base.utils.thumbnails import get_thumbnail_url, get_thumbnail
def get_timeline_image_thumbnail_url(attachment): def get_timeline_image_thumbnail_name(attachment):
if attachment.attached_file: if attachment.attached_file:
return get_thumbnail_url(attachment.attached_file, settings.THN_ATTACHMENT_TIMELINE) thumbnail = get_thumbnail(attachment.attached_file, settings.THN_ATTACHMENT_TIMELINE)
return thumbnail.name if thumbnail else None
return None return None
@ -29,6 +30,7 @@ def get_card_image_thumbnail_url(attachment):
return get_thumbnail_url(attachment.attached_file, settings.THN_ATTACHMENT_CARD) return get_thumbnail_url(attachment.attached_file, settings.THN_ATTACHMENT_CARD)
return None return None
def get_attachment_image_preview_url(attachment): def get_attachment_image_preview_url(attachment):
if attachment.attached_file: if attachment.attached_file:
return get_thumbnail_url(attachment.attached_file, settings.THN_ATTACHMENT_PREVIEW) return get_thumbnail_url(attachment.attached_file, settings.THN_ATTACHMENT_PREVIEW)

View File

@ -28,7 +28,7 @@ from taiga.base.utils.iterators import as_tuple
from taiga.base.utils.iterators import as_dict from taiga.base.utils.iterators import as_dict
from taiga.mdrender.service import render as mdrender from taiga.mdrender.service import render as mdrender
from taiga.projects.attachments.services import get_timeline_image_thumbnail_url from taiga.projects.attachments.services import get_timeline_image_thumbnail_name
import os import os
@ -199,12 +199,13 @@ def _generic_extract(obj:object, fields:list, default=None) -> dict:
def extract_attachments(obj) -> list: def extract_attachments(obj) -> list:
for attach in obj.attachments.all(): for attach in obj.attachments.all():
# Force the creation of a thumbnail for the timeline # Force the creation of a thumbnail for the timeline
get_timeline_image_thumbnail_url(attach) thumbnail_file = get_timeline_image_thumbnail_name(attach)
yield {"id": attach.id, yield {"id": attach.id,
"filename": os.path.basename(attach.attached_file.name), "filename": os.path.basename(attach.attached_file.name),
"url": attach.attached_file.url, "url": attach.attached_file.url,
"attached_file": str(attach.attached_file), "attached_file": str(attach.attached_file),
"thumbnail_file": thumbnail_file,
"is_deprecated": attach.is_deprecated, "is_deprecated": attach.is_deprecated,
"description": attach.description, "description": attach.description,
"order": attach.order} "order": attach.order}

View File

@ -85,7 +85,12 @@ class TimelineSerializer(serializers.LightSerializer):
attached_file = file_path[index+1:] attached_file = file_path[index+1:]
item['url'] = default_storage.url(attached_file) item['url'] = default_storage.url(attached_file)
thumb_url = get_thumbnail_url(attached_file,
settings.THN_ATTACHMENT_TIMELINE) if 'thumbnail_file' in item:
thumb_file = item['thumbnail_file']
thumb_url = default_storage.url(thumb_file) if thumb_file else None
else:
thumb_url = get_thumbnail_url(attached_file,
settings.THN_ATTACHMENT_TIMELINE)
item['thumb_url'] = thumb_url item['thumb_url'] = thumb_url