浏览代码

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

Jeremy Stretch 1 天之前
父节点
当前提交
583b6ce1ca
共有 3 个文件被更改,包括 64 次插入 和 2 次删除
  1. 3 0
      docs/plugins/development/views.md
  2. 34 0
      netbox/netbox/tests/test_views.py
  3. 27 2
      netbox/netbox/views/generic/bulk_views.py

+ 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_table
         - export_template
         - 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
 ::: netbox.views.generic.BulkImportView
     options:
     options:
       members:
       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 core.models import DataFile, DataSource
 from dcim.choices import DeviceStatusChoices, InterfaceTypeChoices, SiteStatusChoices
 from dcim.choices import DeviceStatusChoices, InterfaceTypeChoices, SiteStatusChoices
 from dcim.models import Device, DeviceRole, DeviceType, Interface, Manufacturer, Site, VirtualChassis
 from dcim.models import Device, DeviceRole, DeviceType, Interface, Manufacturer, Site, VirtualChassis
+from dcim.tables import SiteTable
 from extras.events import enqueue_event
 from extras.events import enqueue_event
 from extras.models import ImageAttachment
 from extras.models import ImageAttachment
 from extras.validators import CustomValidator
 from extras.validators import CustomValidator
@@ -21,6 +22,7 @@ from ipam.views import VLANGroupVLANsView
 from netbox.choices import CSVDelimiterChoices, ImportFormatChoices, ImportMethodChoices
 from netbox.choices import CSVDelimiterChoices, ImportFormatChoices, ImportMethodChoices
 from netbox.constants import EMPTY_TABLE_TEXT
 from netbox.constants import EMPTY_TABLE_TEXT
 from netbox.search.backends import search_backend
 from netbox.search.backends import search_backend
+from netbox.views import generic
 from users.models import User
 from users.models import User
 from utilities.testing import TestCase
 from utilities.testing import TestCase
 from utilities.views import get_action_url
 from utilities.views import get_action_url
@@ -219,6 +221,38 @@ class BulkActionButtonsTestCase(TestCase):
         self.assertIn('name="_edit"', content)
         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):
 class SearchViewTestCase(TestCase):
 
 
     @classmethod
     @classmethod

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

@@ -1,5 +1,6 @@
 import logging
 import logging
 import re
 import re
+import warnings
 from collections import Counter
 from collections import Counter
 from contextlib import contextmanager
 from contextlib import contextmanager
 from copy import deepcopy
 from copy import deepcopy
@@ -101,6 +102,18 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
     filterset_form = None
     filterset_form = None
     actions = (AddObject, BulkImport, BulkExport, BulkEdit, BulkRename, BulkDelete)
     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):
     def get_required_permission(self):
         return get_permission_for_model(self.queryset.model, 'view')
         return get_permission_for_model(self.queryset.model, 'view')
 
 
@@ -108,17 +121,24 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
     # Export methods
     # Export methods
     #
     #
 
 
+    # TODO: Remove in NetBox v5.0
     def export_yaml(self):
     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]
         yaml_data = [obj.to_yaml() for obj in self.queryset]
 
 
         return '---\n'.join(yaml_data)
         return '---\n'.join(yaml_data)
 
 
+    # TODO: Remove in NetBox v5.0
     def export_table(self, table, columns=None, filename=None, delimiter=None):
     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:
         Args:
             table: The Table instance to export
             table: The Table instance to export
@@ -127,6 +147,11 @@ class ObjectListView(BaseMultiObjectView, ActionsMixin, TableMixin):
                 from the queryset model name.
                 from the queryset model name.
             delimiter: The character used to separate columns (a comma is used by default)
             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'}
         exclude_columns = {'pk', 'actions'}
         all_columns = [col_name for col_name, _ in table.selected_columns + table.available_columns]
         all_columns = [col_name for col_name, _ in table.selected_columns + table.available_columns]
         if columns:
         if columns: