Explorar o código

Fixes #7741: Fix 404 when attaching multiple images in succession

jeremystretch %!s(int64=4) %!d(string=hai) anos
pai
achega
3ad773beb3

+ 1 - 0
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

+ 2 - 0
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
 

+ 1 - 5
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
 

+ 1 - 1
netbox/templates/inc/image_attachments_panel.html

@@ -44,7 +44,7 @@
   </div>
   {% if perms.extras.add_imageattachment %}
     <div class="card-footer text-end noprint">
-      <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">
+      <a href="{% url 'extras:imageattachment_add' %}?content_type={{ object|content_type_id }}&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
         <i class="mdi mdi-plus-thick" aria-hidden="true"></i> Attach an image
       </a>
     </div>

+ 20 - 0
netbox/utilities/templatetags/helpers.py

@@ -6,6 +6,7 @@ from typing import Dict, Any
 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):
     """