فهرست منبع

Classify available prefixes only when sorting by classification

Stop computing a classification for every available-space row in
add_requested_prefixes(). List data now sorts by the classification
property, while querysets sort by the stored field via
PrefixTable.order_classification().
Jeremy Stretch 2 روز پیش
والد
کامیت
66956b90e2
3فایلهای تغییر یافته به همراه14 افزوده شده و 6 حذف شده
  1. 6 2
      netbox/ipam/tables/ip.py
  2. 7 0
      netbox/ipam/tests/test_tables.py
  3. 1 4
      netbox/ipam/utils.py

+ 6 - 2
netbox/ipam/tables/ip.py

@@ -187,11 +187,11 @@ class PrefixTable(TenancyColumnsMixin, ContactsColumnMixin, PrimaryModelTable):
         verbose_name=_('Status'),
         verbose_name=_('Status'),
         default=AVAILABLE_LABEL
         default=AVAILABLE_LABEL
     )
     )
-    # Read the property so unsaved prefixes display correctly
+    # Read the property so unsaved prefixes display and sort correctly
     classification = columns.ChoiceFieldColumn(
     classification = columns.ChoiceFieldColumn(
         verbose_name=_('Classification'),
         verbose_name=_('Classification'),
         accessor='get_classification_display',
         accessor='get_classification_display',
-        order_by=('_classification',)
+        order_by=('classification',)
     )
     )
     vrf = tables.TemplateColumn(
     vrf = tables.TemplateColumn(
         template_code=VRF_LINK,
         template_code=VRF_LINK,
@@ -254,6 +254,10 @@ class PrefixTable(TenancyColumnsMixin, ContactsColumnMixin, PrimaryModelTable):
             'class': lambda record: 'success' if not record.pk else '',
             'class': lambda record: 'success' if not record.pk else '',
         }
         }
 
 
+    def order_classification(self, queryset, is_descending):
+        # Querysets sort by the stored field; list data sorts by the property
+        return queryset.order_by('-_classification' if is_descending else '_classification'), True
+
 
 
 #
 #
 # IP ranges
 # IP ranges

+ 7 - 0
netbox/ipam/tests/test_tables.py

@@ -226,6 +226,13 @@ class PrefixTableTestCase(TableTestCases.StandardTableTestCase):
         table = PrefixTable(data, order_by='-classification')
         table = PrefixTable(data, order_by='-classification')
         self.assertEqual([prefix.prefix for prefix in table.data], [IPNetwork('11.0.0.0/8'), IPNetwork('10.0.0.0/8')])
         self.assertEqual([prefix.prefix for prefix in table.data], [IPNetwork('11.0.0.0/8'), IPNetwork('10.0.0.0/8')])
 
 
+    def test_classification_ordering_queryset(self):
+        """Saved prefixes sort by their stored classification."""
+        Prefix.objects.create(prefix=IPNetwork('8.8.8.0/24'))
+        Prefix.objects.create(prefix=IPNetwork('10.0.0.0/8'))
+        table = PrefixTable(Prefix.objects.all(), order_by='classification')
+        self.assertEqual([prefix.prefix for prefix in table.data], [IPNetwork('10.0.0.0/8'), IPNetwork('8.8.8.0/24')])
+
 
 
 class IPRangeTableTestCase(TableTestCases.StandardTableTestCase):
 class IPRangeTableTestCase(TableTestCases.StandardTableTestCase):
     table = IPRangeTable
     table = IPRangeTable

+ 1 - 4
netbox/ipam/utils.py

@@ -70,10 +70,7 @@ def add_requested_prefixes(parent, prefix_list, show_available=True, show_assign
         # saved Prefix instances with real pks, bulk delete will fail for mixed-type selections
         # saved Prefix instances with real pks, bulk delete will fail for mixed-type selections
         # due to single-model form validation. See: https://github.com/netbox-community/netbox/issues/21176
         # due to single-model form validation. See: https://github.com/netbox-community/netbox/issues/21176
         available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list])
         available_prefixes = netaddr.IPSet(parent) ^ netaddr.IPSet([p.prefix for p in prefix_list])
-        available_prefixes = [
-            Prefix(prefix=p, status=None, _classification=get_prefix_classification(p))
-            for p in available_prefixes.iter_cidrs()
-        ]
+        available_prefixes = [Prefix(prefix=p, status=None) for p in available_prefixes.iter_cidrs()]
         child_prefixes = child_prefixes + available_prefixes
         child_prefixes = child_prefixes + available_prefixes
 
 
     # Add assigned prefixes to the table if requested
     # Add assigned prefixes to the table if requested