Просмотр исходного кода

Introduce a common URL for the creation of image attachments

jeremystretch 4 лет назад
Родитель
Сommit
460e3fd5d6

+ 1 - 5
netbox/dcim/urls.py

@@ -1,6 +1,6 @@
 from django.urls import path
 
-from extras.views import ImageAttachmentEditView, ObjectChangeLogView, ObjectJournalView
+from extras.views import ObjectChangeLogView, ObjectJournalView
 from ipam.views import ServiceEditView
 from utilities.views import SlugRedirectView
 from . import views
@@ -43,7 +43,6 @@ urlpatterns = [
     path('sites/<int:pk>/delete/', views.SiteDeleteView.as_view(), name='site_delete'),
     path('sites/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='site_changelog', kwargs={'model': Site}),
     path('sites/<int:pk>/journal/', ObjectJournalView.as_view(), name='site_journal', kwargs={'model': Site}),
-    path('sites/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='site_add_image', kwargs={'model': Site}),
 
     # Locations
     path('locations/', views.LocationListView.as_view(), name='location_list'),
@@ -55,7 +54,6 @@ urlpatterns = [
     path('locations/<int:pk>/edit/', views.LocationEditView.as_view(), name='location_edit'),
     path('locations/<int:pk>/delete/', views.LocationDeleteView.as_view(), name='location_delete'),
     path('locations/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='location_changelog', kwargs={'model': Location}),
-    path('locations/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='location_add_image', kwargs={'model': Location}),
 
     # Rack roles
     path('rack-roles/', views.RackRoleListView.as_view(), name='rackrole_list'),
@@ -92,7 +90,6 @@ urlpatterns = [
     path('racks/<int:pk>/delete/', views.RackDeleteView.as_view(), name='rack_delete'),
     path('racks/<int:pk>/changelog/', ObjectChangeLogView.as_view(), name='rack_changelog', kwargs={'model': Rack}),
     path('racks/<int:pk>/journal/', ObjectJournalView.as_view(), name='rack_journal', kwargs={'model': Rack}),
-    path('racks/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='rack_add_image', kwargs={'model': Rack}),
 
     # Manufacturers
     path('manufacturers/', views.ManufacturerListView.as_view(), name='manufacturer_list'),
@@ -229,7 +226,6 @@ urlpatterns = [
     path('devices/<int:pk>/lldp-neighbors/', views.DeviceLLDPNeighborsView.as_view(), name='device_lldp_neighbors'),
     path('devices/<int:pk>/config/', views.DeviceConfigView.as_view(), name='device_config'),
     path('devices/<int:device>/services/assign/', ServiceEditView.as_view(), name='device_service_assign'),
-    path('devices/<int:object_id>/images/add/', ImageAttachmentEditView.as_view(), name='device_add_image', kwargs={'model': Device}),
 
     # Console ports
     path('console-ports/', views.ConsolePortListView.as_view(), name='consoleport_list'),

+ 1 - 0
netbox/extras/urls.py

@@ -78,6 +78,7 @@ urlpatterns = [
          kwargs={'model': models.ConfigContext}),
 
     # Image attachments
+    path('image-attachments/add/', views.ImageAttachmentEditView.as_view(), name='imageattachment_add'),
     path('image-attachments/<int:pk>/edit/', views.ImageAttachmentEditView.as_view(), name='imageattachment_edit'),
     path('image-attachments/<int:pk>/delete/', views.ImageAttachmentDeleteView.as_view(), name='imageattachment_delete'),
 

+ 13 - 9
netbox/extras/views.py

@@ -472,22 +472,26 @@ class ImageAttachmentEditView(generic.ObjectEditView):
     queryset = ImageAttachment.objects.all()
     model_form = forms.ImageAttachmentForm
 
-    def alter_obj(self, imageattachment, request, args, kwargs):
-        if not imageattachment.pk:
+    def alter_obj(self, instance, request, args, kwargs):
+        if not instance.pk:
             # Assign the parent object based on URL kwargs
-            model = kwargs.get('model')
-            imageattachment.parent = get_object_or_404(model, pk=kwargs['object_id'])
-        return imageattachment
+            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)
+            instance.parent = get_object_or_404(content_type.model_class(), pk=request.GET.get('object_id'))
+        return instance
 
-    def get_return_url(self, request, imageattachment):
-        return imageattachment.parent.get_absolute_url()
+    def get_return_url(self, request, obj=None):
+        return obj.parent.get_absolute_url() if obj else super().get_return_url(request)
 
 
 class ImageAttachmentDeleteView(generic.ObjectDeleteView):
     queryset = ImageAttachment.objects.all()
 
-    def get_return_url(self, request, imageattachment):
-        return imageattachment.parent.get_absolute_url()
+    def get_return_url(self, request, obj=None):
+        return obj.parent.get_absolute_url() if obj else super().get_return_url(request)
 
 
 #

+ 1 - 1
netbox/templates/dcim/device.html

@@ -299,7 +299,7 @@
                 </div>
                 {% if perms.extras.add_imageattachment %}
                 <div class="card-footer text-end noprint">
-                    <a href="{% url 'dcim:device_add_image' object_id=object.pk %}" class="btn btn-primary btn-sm">
+                    <a href="{% url 'extras:imageattachment_add' %}?content_type=dcim.device&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
                         <span class="mdi mdi-plus-thick" aria-hidden="true"></span>
                         Attach an Image
                     </a>

+ 1 - 1
netbox/templates/dcim/location.html

@@ -68,7 +68,7 @@
         </div>
         {% if perms.extras.add_imageattachment %}
           <div class="card-footer text-end noprint">
-              <a href="{% url 'dcim:location_add_image' object_id=object.pk %}" class="btn btn-primary btn-sm">
+              <a href="{% url 'extras:imageattachment_add' %}?content_type=dcim.location&object_id={{ object.pk }}" class="btn btn-primary btn-sm">
                   <span class="mdi mdi-plus-thick" aria-hidden="true"></span>
                   Attach an Image
               </a>

+ 1 - 1
netbox/templates/dcim/rack.html

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

+ 1 - 1
netbox/templates/dcim/site.html

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