Add truncate_middle filter for middle-ellipsis on long filenames

This commit is contained in:
Brian Tiemann
2026-01-21 19:39:31 -05:00
parent e81ccb9be6
commit ba6124f0e0
2 changed files with 14 additions and 1 deletions

View File

@@ -43,7 +43,7 @@ IMAGEATTACHMENT_IMAGE = """
<a href="{{ record.image.url }}" target="_blank" class="image-preview" data-bs-placement="top"> <a href="{{ record.image.url }}" target="_blank" class="image-preview" data-bs-placement="top">
<i class="mdi mdi-image"></i></a> <i class="mdi mdi-image"></i></a>
{% endif %} {% endif %}
<a href="{{ record.get_absolute_url }}">{{ record }}</a> <a href="{{ record.get_absolute_url }}">{{ record.filename|truncate_middle:16 }}</a>
""" """
NOTIFICATION_ICON = """ NOTIFICATION_ICON = """

View File

@@ -252,3 +252,16 @@ def isodatetime(value, spec='seconds'):
else: else:
return '' return ''
return mark_safe(f'<span title="{naturaltime(value)}">{text}</span>') return mark_safe(f'<span title="{naturaltime(value)}">{text}</span>')
@register.filter
def truncate_middle(value, length):
if len(value) <= length:
return value
# Calculate split points for the two parts
half_len = (length - 3) // 2 # 3 for the '...'
first_part = value[:half_len]
second_part = value[len(value) - (length - 3 - half_len):]
return mark_safe(f"{first_part}&hellip;{second_part}")