Skip to content

Closes #18990: Add description field to ImageAttachment model #19907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions netbox/extras/api/serializers_/attachments.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ class ImageAttachmentSerializer(ValidatedModelSerializer):
class Meta:
model = ImageAttachment
fields = [
'id', 'url', 'display', 'object_type', 'object_id', 'parent', 'name', 'image',
'id', 'url', 'display', 'object_type', 'object_id', 'parent', 'name', 'image', 'description',
'image_height', 'image_width', 'created', 'last_updated',
]
brief_fields = ('id', 'url', 'display', 'name', 'image')
brief_fields = ('id', 'url', 'display', 'name', 'image', 'description')

def validate(self, data):

Expand Down
7 changes: 5 additions & 2 deletions netbox/extras/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -451,12 +451,15 @@ class ImageAttachmentFilterSet(ChangeLoggedModelFilterSet):

class Meta:
model = ImageAttachment
fields = ('id', 'object_type_id', 'object_id', 'name', 'image_width', 'image_height')
fields = ('id', 'object_type_id', 'object_id', 'name', 'description', 'image_width', 'image_height')

def search(self, queryset, name, value):
if not value.strip():
return queryset
return queryset.filter(name__icontains=value)
return queryset.filter(
Q(name__icontains=value) |
Q(description__icontains=value)
)


class JournalEntryFilterSet(NetBoxModelFilterSet):
Expand Down
7 changes: 5 additions & 2 deletions netbox/extras/forms/model_forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -744,14 +744,17 @@ def clean(self):

class ImageAttachmentForm(forms.ModelForm):
fieldsets = (
FieldSet(ObjectAttribute('parent'), 'name', 'image'),
FieldSet(ObjectAttribute('parent'), 'image', 'name', 'description'),
)

class Meta:
model = ImageAttachment
fields = [
'name', 'image',
'image', 'name', 'description',
]
help_texts = {
'name': _("If no name is specified, the file name will be used.")
}


class JournalEntryForm(NetBoxModelForm):
Expand Down
16 changes: 16 additions & 0 deletions netbox/extras/migrations/0130_imageattachment_description.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('extras', '0129_fix_script_paths'),
]

operations = [
migrations.AddField(
model_name='imageattachment',
name='description',
field=models.CharField(blank=True, max_length=200),
),
]
18 changes: 14 additions & 4 deletions netbox/extras/models/models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import json
import os
import urllib.parse

from django.conf import settings
Expand Down Expand Up @@ -678,6 +679,11 @@ class ImageAttachment(ChangeLoggedModel):
max_length=50,
blank=True
)
description = models.CharField(
verbose_name=_('description'),
max_length=200,
blank=True
)

objects = RestrictedQuerySet.as_manager()

Expand All @@ -692,10 +698,10 @@ class Meta:
verbose_name_plural = _('image attachments')

def __str__(self):
if self.name:
return self.name
filename = self.image.name.rsplit('/', 1)[-1]
return filename.split('_', 2)[2]
return self.name or self.filename

def get_absolute_url(self):
return reverse('extras:imageattachment', args=[self.pk])

def clean(self):
super().clean()
Expand All @@ -719,6 +725,10 @@ def delete(self, *args, **kwargs):
# before the request finishes. (For example, to display a message indicating the ImageAttachment was deleted.)
self.image.name = _name

@property
def filename(self):
return os.path.basename(self.image.name).split('_', 2)[2]

@property
def size(self):
"""
Expand Down
10 changes: 10 additions & 0 deletions netbox/extras/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ class CustomFieldIndex(SearchIndex):
display_attrs = ('description',)


@register_search
class ImageAttachmentIndex(SearchIndex):
model = models.ImageAttachment
fields = (
('name', 100),
('description', 500),
)
display_attrs = ('description',)


@register_search
class JournalEntryIndex(SearchIndex):
model = models.JournalEntry
Expand Down
6 changes: 3 additions & 3 deletions netbox/extras/tables/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,10 @@ class ImageAttachmentTable(NetBoxTable):
class Meta(NetBoxTable.Meta):
model = ImageAttachment
fields = (
'pk', 'object_type', 'parent', 'image', 'name', 'image_height', 'image_width', 'size', 'created',
'last_updated',
'pk', 'object_type', 'parent', 'image', 'name', 'description', 'image_height', 'image_width', 'size',
'created', 'last_updated',
)
default_columns = ('object_type', 'parent', 'image', 'name', 'size', 'created')
default_columns = ('object_type', 'parent', 'image', 'name', 'description', 'size', 'created')


class SavedFilterTable(NetBoxTable):
Expand Down
2 changes: 1 addition & 1 deletion netbox/extras/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ class ImageAttachmentTest(
APIViewTestCases.GraphQLTestCase
):
model = ImageAttachment
brief_fields = ['display', 'id', 'image', 'name', 'url']
brief_fields = ['description', 'display', 'id', 'image', 'name', 'url']

@classmethod
def setUpTestData(cls):
Expand Down
11 changes: 5 additions & 6 deletions netbox/extras/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,11 @@ class ImageAttachmentListView(generic.ObjectListView):
actions = (BulkExport,)


@register_model_view(ImageAttachment)
class ImageAttachmentView(generic.ObjectView):
queryset = ImageAttachment.objects.all()


@register_model_view(ImageAttachment, 'add', detail=False)
@register_model_view(ImageAttachment, 'edit')
class ImageAttachmentEditView(generic.ObjectEditView):
Expand All @@ -1053,9 +1058,6 @@ def alter_object(self, instance, request, args, kwargs):
instance.parent = get_object_or_404(object_type.model_class(), pk=request.GET.get('object_id'))
return instance

def get_return_url(self, request, obj=None):
return obj.parent.get_absolute_url() if obj else super().get_return_url(request)

def get_extra_addanother_params(self, request):
return {
'object_type': request.GET.get('object_type'),
Expand All @@ -1067,9 +1069,6 @@ def get_extra_addanother_params(self, request):
class ImageAttachmentDeleteView(generic.ObjectDeleteView):
queryset = ImageAttachment.objects.all()

def get_return_url(self, request, obj=None):
return obj.parent.get_absolute_url() if obj else super().get_return_url(request)


#
# Journal entries
Expand Down
65 changes: 64 additions & 1 deletion netbox/templates/extras/imageattachment.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,67 @@
{% extends 'generic/object.html' %}
{% load helpers %}
{% load plugins %}
{% load i18n %}

{% block tabs %}
{% block content %}
<div class="row">
<div class="col col-12 col-md-6">
<div class="card">
<h2 class="card-header">{% trans "Image Attachment" %}</h2>
<table class="table table-hover attr-table">
<tr>
<th scope="row">{% trans "Parent Object" %}</th>
<td>{{ object.parent|linkify }}</td>
</tr>
<tr>
<th scope="row">{% trans "Name" %}</th>
<td>{{ object.name|placeholder }}</td>
</tr>
<tr>
<th scope="row">{% trans "Description" %}</th>
<td>{{ object.description|placeholder }}</td>
</tr>
</table>
</div>
{% plugin_left_page object %}
</div>
<div class="col col-12 col-md-6">
<div class="card">
<h2 class="card-header">{% trans "File" %}</h2>
<table class="table table-hover attr-table">
<tr>
<th scope="row">{% trans "Filename" %}</th>
<td>
<a href="{{ object.image.url }}" target="_blank">{{ object.filename }}</a>
<i class="mdi mdi-open-in-new"></i>
</td>
</tr>
<tr>
<th scope="row">{% trans "Dimensions" %}</th>
<td>{{ object.image_width }} × {{ object.image_height }}</td>
</tr>
<tr>
<th scope="row">{% trans "Size" %}</th>
<td>
<span title="{{ object.size }} {% trans "bytes" %}">{{ object.size|filesizeformat }}</span>
</td>
</tr>
</table>
</div>
{% plugin_right_page object %}
</div>
</div>
<div class="row">
<div class="col col-md-12">
<div class="card">
<h2 class="card-header">{% trans "Image" %}</h2>
<div class="card-body">
<a href="{{ object.image.url }}">
<img src="{{ object.image.url }}" height="{{ image.height }}" width="{{ image.width }}" alt="{{ object }}" />
</a>
</div>
</div>
{% plugin_full_width_page object %}
</div>
</div>
{% endblock %}