Skip to content

Commit 3ad773b

Browse files
committed
Fixes #7741: Fix 404 when attaching multiple images in succession
1 parent be91235 commit 3ad773b

File tree

5 files changed

+25
-6
lines changed

5 files changed

+25
-6
lines changed

docs/release-notes/version-3.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
### Bug Fixes
1111

1212
* [#7701](https://github.com/netbox-community/netbox/issues/7701) - Fix conflation of assigned IP status & role in interface tables
13+
* [#7741](https://github.com/netbox-community/netbox/issues/7741) - Fix 404 when attaching multiple images in succession
1314
* [#7752](https://github.com/netbox-community/netbox/issues/7752) - Fix minimum version check under Python v3.10
1415
* [#7766](https://github.com/netbox-community/netbox/issues/7766) - Add missing outer dimension columns to rack table
1516
* [#7780](https://github.com/netbox-community/netbox/issues/7780) - Preserve mutli-line values during CSV file import

netbox/extras/models/models.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ class ImageAttachment(BigIDModel):
357357

358358
objects = RestrictedQuerySet.as_manager()
359359

360+
clone_fields = ('content_type', 'object_id')
361+
360362
class Meta:
361363
ordering = ('name', 'pk') # name may be non-unique
362364

netbox/extras/views.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,7 @@ class ImageAttachmentEditView(generic.ObjectEditView):
475475
def alter_obj(self, instance, request, args, kwargs):
476476
if not instance.pk:
477477
# Assign the parent object based on URL kwargs
478-
try:
479-
app_label, model = request.GET.get('content_type').split('.')
480-
except (AttributeError, ValueError):
481-
raise Http404("Content type not specified")
482-
content_type = get_object_or_404(ContentType, app_label=app_label, model=model)
478+
content_type = get_object_or_404(ContentType, pk=request.GET.get('content_type'))
483479
instance.parent = get_object_or_404(content_type.model_class(), pk=request.GET.get('object_id'))
484480
return instance
485481

netbox/templates/inc/image_attachments_panel.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ <h5 class="card-header">
4444
</div>
4545
{% if perms.extras.add_imageattachment %}
4646
<div class="card-footer text-end noprint">
47-
<a href="{% url 'extras:imageattachment_add' %}?content_type={{ object|meta:"app_label" }}.{{ object|meta:"model_name" }}&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
47+
<a href="{% url 'extras:imageattachment_add' %}?content_type={{ object|content_type_id }}&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
4848
<i class="mdi mdi-plus-thick" aria-hidden="true"></i> Attach an image
4949
</a>
5050
</div>

netbox/utilities/templatetags/helpers.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import yaml
77
from django import template
88
from django.conf import settings
9+
from django.contrib.contenttypes.models import ContentType
910
from django.template.defaultfilters import date
1011
from django.urls import NoReverseMatch, reverse
1112
from django.utils import timezone
@@ -78,6 +79,25 @@ def meta(obj, attr):
7879
return getattr(obj._meta, attr, '')
7980

8081

82+
@register.filter()
83+
def content_type(obj):
84+
"""
85+
Return the ContentType for the given object.
86+
"""
87+
return ContentType.objects.get_for_model(obj)
88+
89+
90+
@register.filter()
91+
def content_type_id(obj):
92+
"""
93+
Return the ContentType ID for the given object.
94+
"""
95+
content_type = ContentType.objects.get_for_model(obj)
96+
if content_type:
97+
return content_type.pk
98+
return None
99+
100+
81101
@register.filter()
82102
def viewname(model, action):
83103
"""

0 commit comments

Comments
 (0)