From 3ad773beb3bfadf2b6ecaa5dd843acba3a2fb49e Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Tue, 9 Nov 2021 16:46:58 -0500 Subject: [PATCH] Fixes #7741: Fix 404 when attaching multiple images in succession --- docs/release-notes/version-3.0.md | 1 + netbox/extras/models/models.py | 2 ++ netbox/extras/views.py | 6 +----- .../inc/image_attachments_panel.html | 2 +- netbox/utilities/templatetags/helpers.py | 20 +++++++++++++++++++ 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/docs/release-notes/version-3.0.md b/docs/release-notes/version-3.0.md index ef99c4a0e7e..7a300542182 100644 --- a/docs/release-notes/version-3.0.md +++ b/docs/release-notes/version-3.0.md @@ -10,6 +10,7 @@ ### Bug Fixes * [#7701](https://github.com/netbox-community/netbox/issues/7701) - Fix conflation of assigned IP status & role in interface tables +* [#7741](https://github.com/netbox-community/netbox/issues/7741) - Fix 404 when attaching multiple images in succession * [#7752](https://github.com/netbox-community/netbox/issues/7752) - Fix minimum version check under Python v3.10 * [#7766](https://github.com/netbox-community/netbox/issues/7766) - Add missing outer dimension columns to rack table * [#7780](https://github.com/netbox-community/netbox/issues/7780) - Preserve mutli-line values during CSV file import diff --git a/netbox/extras/models/models.py b/netbox/extras/models/models.py index 75f5242d385..2c56f2f0f6e 100644 --- a/netbox/extras/models/models.py +++ b/netbox/extras/models/models.py @@ -357,6 +357,8 @@ class ImageAttachment(BigIDModel): objects = RestrictedQuerySet.as_manager() + clone_fields = ('content_type', 'object_id') + class Meta: ordering = ('name', 'pk') # name may be non-unique diff --git a/netbox/extras/views.py b/netbox/extras/views.py index d39f50c79aa..b0387c73d40 100644 --- a/netbox/extras/views.py +++ b/netbox/extras/views.py @@ -475,11 +475,7 @@ class ImageAttachmentEditView(generic.ObjectEditView): def alter_obj(self, instance, request, args, kwargs): if not instance.pk: # Assign the parent object based on URL kwargs - try: - app_label, model = request.GET.get('content_type').split('.') - except (AttributeError, ValueError): - raise Http404("Content type not specified") - content_type = get_object_or_404(ContentType, app_label=app_label, model=model) + content_type = get_object_or_404(ContentType, pk=request.GET.get('content_type')) instance.parent = get_object_or_404(content_type.model_class(), pk=request.GET.get('object_id')) return instance diff --git a/netbox/templates/inc/image_attachments_panel.html b/netbox/templates/inc/image_attachments_panel.html index ca7312901c0..9706a7ffe01 100644 --- a/netbox/templates/inc/image_attachments_panel.html +++ b/netbox/templates/inc/image_attachments_panel.html @@ -44,7 +44,7 @@
{% if perms.extras.add_imageattachment %} diff --git a/netbox/utilities/templatetags/helpers.py b/netbox/utilities/templatetags/helpers.py index 1695c8257d8..5b553432137 100644 --- a/netbox/utilities/templatetags/helpers.py +++ b/netbox/utilities/templatetags/helpers.py @@ -6,6 +6,7 @@ import yaml from django import template from django.conf import settings +from django.contrib.contenttypes.models import ContentType from django.template.defaultfilters import date from django.urls import NoReverseMatch, reverse from django.utils import timezone @@ -78,6 +79,25 @@ def meta(obj, attr): return getattr(obj._meta, attr, '') +@register.filter() +def content_type(obj): + """ + Return the ContentType for the given object. + """ + return ContentType.objects.get_for_model(obj) + + +@register.filter() +def content_type_id(obj): + """ + Return the ContentType ID for the given object. + """ + content_type = ContentType.objects.get_for_model(obj) + if content_type: + return content_type.pk + return None + + @register.filter() def viewname(model, action): """