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

Closes #23539: Deprecate export_table() and export_yaml() on ObjectListView (#23540)

Jeremy Stretch 1 день назад
Родитель
Сommit
583b6ce1ca

+ 3 - 0
docs/plugins/development/views.md

@@ -157,6 +157,9 @@ Below are the class definitions for NetBox's multi-object views. These views han
         - export_table
         - export_template
 
+!!! warning "Deprecation Warning"
+    The `export_table()` and `export_yaml()` methods of `ObjectListView` have been deprecated as of NetBox v4.8.0, and are scheduled for removal in NetBox v5.0.
+
 ::: netbox.views.generic.BulkImportView
     options:
       members:

+ 34 - 0
netbox/netbox/tests/test_views.py

@@ -12,6 +12,7 @@ from django.utils import timezone
 from core.models import DataFile, DataSource
 from dcim.choices import DeviceStatusChoices, InterfaceTypeChoices, SiteStatusChoices
 from dcim.models import Device, DeviceRole, DeviceType, Interface, Manufacturer, Site, VirtualChassis
+from dcim.tables import SiteTable
 from extras.events import enqueue_event
 from extras.models import ImageAttachment
 from extras.validators import CustomValidator
@@ -21,6 +22,7 @@ from ipam.views import VLANGroupVLANsView
 from netbox.choices import CSVDelimiterChoices, ImportFormatChoices, ImportMethodChoices
 from netbox.constants import EMPTY_TABLE_TEXT
 from netbox.search.backends import search_backend
+from netbox.views import generic
 from users.models import User
 from utilities.testing import TestCase
 from utilities.views import get_action_url
@@ -219,6 +221,38 @@ class BulkActionButtonsTestCase(TestCase):
         self.assertIn('name="_edit"', content)
 
 
+# TODO: Remove in NetBox v5.0
+class ObjectListViewExportDeprecationTestCase(TestCase):
+    """
+    Verify that the deprecated export methods of ObjectListView raise deprecation warnings. (Ref: #23539)
+    """
+    def test_export_table(self):
+        view = generic.ObjectListView()
+        view.queryset = Site.objects.all()
+
+        with self.assertWarnsRegex(DeprecationWarning, r'export_table\(\)'):
+            view.export_table(SiteTable(Site.objects.all()))
+
+    def test_export_yaml(self):
+        view = generic.ObjectListView()
+        view.queryset = DeviceType.objects.all()
+
+        with self.assertWarnsRegex(DeprecationWarning, r'export_yaml\(\)'):
+            view.export_yaml()
+
+    def test_override_export_table(self):
+        with self.assertWarnsRegex(DeprecationWarning, r'export_table\(\)'):
+            class TestListView(generic.ObjectListView):
+                def export_table(self, table, columns=None, filename=None, delimiter=None):
+                    pass
+
+    def test_override_export_yaml(self):
+        with self.assertWarnsRegex(DeprecationWarning, r'export_yaml\(\)'):
+            class TestListView(generic.ObjectListView):
+                def export_yaml(self):
+                    pass
+
+
 class SearchViewTestCase(TestCase):
 
     @classmethod

+ 27 - 2
netbox/netbox/views/generic/bulk_views.py

@@ -1,5 +1,6 @@
 import logging
 import re
+import warnings
 from collections import Counter
 from contextlib import contextmanager
 from copy import deepcopy
@@ -101,6 +102,18 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
     filterset_form = None
     actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkRename, BulkDelete)
 
+    def __init_subclass__(cls, **kwargs):
+        super().__init_subclass__(**kwargs)
+        # TODO: Remove in NetBox v5.0
+        for method in ('export_table', 'export_yaml'):
+            if method in cls.__dict__:
+                warnings.warn(
+                    f"{cls.__name__} overrides ObjectListView.{method}(), which is deprecated and will be removed in "
+                    f"NetBox v5.0.",
+                    DeprecationWarning,
+                    stacklevel=2,
+                )
+
     def get_required_permission(self):
         return get_permission_for_model(self.queryset.model, 'view')
 
@@ -108,17 +121,24 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
     # Export methods
     #
 
+    # TODO: Remove in NetBox v5.0
     def export_yaml(self):
         """
-        Export the queryset of objects as concatenated YAML documents.
+        Deprecated: Export the queryset of objects as concatenated YAML documents.
         """
+        warnings.warn(
+            "ObjectListView.export_yaml() is deprecated and will be removed in NetBox v5.0.",
+            DeprecationWarning,
+            stacklevel=2,
+        )
         yaml_data = [obj.to_yaml() for obj in self.queryset]
 
         return '---\n'.join(yaml_data)
 
+    # TODO: Remove in NetBox v5.0
     def export_table(self, table, columns=None, filename=None, delimiter=None):
         """
-        Export all table data in CSV format.
+        Deprecated: Export all table data in CSV format.
 
         Args:
             table: The Table instance to export
@@ -127,6 +147,11 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
                 from the queryset model name.
             delimiter: The character used to separate columns (a comma is used by default)
         """
+        warnings.warn(
+            "ObjectListView.export_table() is deprecated and will be removed in NetBox v5.0.",
+            DeprecationWarning,
+            stacklevel=2,
+        )
         exclude_columns = {'pk', 'actions'}
         all_columns = [col_name for col_name, _ in table.selected_columns + table.available_columns]
         if columns: