Przeglądaj źródła

Restore backward compatibility for export_yaml() methods on views

Jeremy Stretch 4 dni temu
rodzic
commit
53e143555c

+ 18 - 0
netbox/netbox/tests/test_exporters.py

@@ -258,6 +258,24 @@ class ExportViewTestCase(TestCase):
                 response = CustomSiteListView.as_view()(request)
                 self.assertEqual(response['Content-Disposition'], 'attachment; filename="custom.csv"')
 
+    def test_export_yaml_override(self):
+        """
+        Overriding the deprecated export_yaml() still applies to YAML exports.
+        """
+        class CustomDeviceTypeListView(DeviceTypeListView):
+            def export_yaml(self):
+                return 'custom'
+
+        self.add_permissions('dcim.view_devicetype')
+        url = reverse('dcim:devicetype_list')
+        for params in ({'export': ''}, {'export': '', 'export_format': 'yaml'}):
+            with self.subTest(params=params):
+                request = RequestFactory().get(url, params)
+                request.user = self.user
+                response = CustomDeviceTypeListView.as_view()(request)
+                self.assertEqual(response['Content-Type'], 'text/yaml')
+                self.assertEqual(response.content, b'custom')
+
     def test_export_yaml(self):
         """
         The deprecated export_yaml() returns the YAML export of the view's queryset.

+ 3 - 0
netbox/netbox/views/generic/bulk_views.py

@@ -268,6 +268,9 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
             else:
                 exporter = get_exporter('csv')
             if not isinstance(exporter, TableExporter):
+                # TODO: Remove in NetBox v5.0 along with export_yaml()
+                if exporter.slug == 'yaml' and type(self).export_yaml is not ObjectListView.export_yaml:
+                    return self.get_export_response(exporter, self.export_yaml())
                 data = exporter.export(request=request, queryset=self.queryset)
                 return self.get_export_response(exporter, data)