Closes #18204: Miscellaneous improvements to the display of image attachments (#19914)
Some checks failed
CI / build (20.x, 3.10) (push) Has been cancelled
CI / build (20.x, 3.11) (push) Has been cancelled
CI / build (20.x, 3.12) (push) Has been cancelled

* Show human-friendly values for file size

* Introduce optional dedicated columns for name & filename

* Add combined dimensions column

* Restore image preview on hover

* Remove object_type from default columns list

* Parent column is not orderable

* Filter/search image attachments by filename

* Correct table column name
This commit is contained in:
Jeremy Stretch 2025-07-22 09:44:30 -04:00 committed by GitHub
parent 59e1d3a607
commit 6df0a02d8d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 37 additions and 12 deletions

View File

@ -458,6 +458,7 @@ class ImageAttachmentFilterSet(ChangeLoggedModelFilterSet):
return queryset return queryset
return queryset.filter( return queryset.filter(
Q(name__icontains=value) | Q(name__icontains=value) |
Q(image__icontains=value) |
Q(description__icontains=value) Q(description__icontains=value)
) )

View File

@ -19,6 +19,7 @@ class ImageAttachmentIndex(SearchIndex):
model = models.ImageAttachment model = models.ImageAttachment
fields = ( fields = (
('name', 100), ('name', 100),
('filename', 110),
('description', 500), ('description', 500),
) )
display_attrs = ('description',) display_attrs = ('description',)

View File

@ -1,6 +1,7 @@
import json import json
import django_tables2 as tables import django_tables2 as tables
from django.template.defaultfilters import filesizeformat
from django.utils.html import format_html from django.utils.html import format_html
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -38,10 +39,11 @@ __all__ = (
IMAGEATTACHMENT_IMAGE = """ IMAGEATTACHMENT_IMAGE = """
{% if record.image %} {% if record.image %}
<a class="image-preview" href="{{ record.image.url }}" target="_blank">{{ record }}</a> <a class="image-preview" href="{{ record.image.url }}" target="_blank">
{% else %} <i class="mdi mdi-image"></i>
&mdash; </a>
{% endif %} {% endif %}
<a href="{{ record.get_absolute_url }}">{{ record }}</a>
""" """
NOTIFICATION_ICON = """ NOTIFICATION_ICON = """
@ -230,29 +232,50 @@ class ImageAttachmentTable(NetBoxTable):
verbose_name=_('ID'), verbose_name=_('ID'),
linkify=False linkify=False
) )
image = columns.TemplateColumn(
verbose_name=_('Image'),
template_code=IMAGEATTACHMENT_IMAGE,
)
name = tables.Column(
verbose_name=_('Name'),
linkify=True,
)
filename = tables.Column(
verbose_name=_('Filename'),
linkify=lambda record: record.image.url,
orderable=False,
)
dimensions = columns.TemplateColumn(
verbose_name=_('Dimensions'),
orderable=False,
template_code="{{ record.image_width }}×{{ record.image_height }}",
)
object_type = columns.ContentTypeColumn( object_type = columns.ContentTypeColumn(
verbose_name=_('Object Type'), verbose_name=_('Object Type'),
) )
parent = tables.Column( parent = tables.Column(
verbose_name=_('Parent'), verbose_name=_('Parent'),
linkify=True linkify=True,
) orderable=False,
image = tables.TemplateColumn(
verbose_name=_('Image'),
template_code=IMAGEATTACHMENT_IMAGE,
) )
size = tables.Column( size = tables.Column(
orderable=False, orderable=False,
verbose_name=_('Size (Bytes)') verbose_name=_('Size')
) )
class Meta(NetBoxTable.Meta): class Meta(NetBoxTable.Meta):
model = ImageAttachment model = ImageAttachment
fields = ( fields = (
'pk', 'object_type', 'parent', 'image', 'name', 'description', 'image_height', 'image_width', 'size', 'pk', 'object_type', 'parent', 'image', 'name', 'filename', 'description', 'image_height', 'image_width',
'created', 'last_updated', 'size', 'created', 'last_updated',
) )
default_columns = ('object_type', 'parent', 'image', 'name', 'description', 'size', 'created') default_columns = ('image', 'parent', 'description', 'dimensions', 'size')
def render_size(self, value):
return filesizeformat(value)
def value_size(self, value):
return value
class SavedFilterTable(NetBoxTable): class SavedFilterTable(NetBoxTable):