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

Fixes #2622: Enable filtering cables by multiple types/colors

Jeremy Stretch 7 лет назад
Родитель
Сommit
08b4b24296
5 измененных файлов с 39 добавлено и 36 удалено
  1. 1 0
      CHANGELOG.md
  2. 7 0
      netbox/dcim/filters.py
  3. 4 3
      netbox/dcim/forms.py
  4. 26 7
      netbox/utilities/constants.py
  5. 1 26
      netbox/utilities/forms.py

+ 1 - 0
CHANGELOG.md

@@ -37,6 +37,7 @@ NetBox now supports modeling physical cables for console, power, and interface c
 
 ## Changes From v2.5-beta2
 
+* [#2622](https://github.com/digitalocean/netbox/issues/2622) - Enable filtering cables by multiple types/colors
 * [#2624](https://github.com/digitalocean/netbox/issues/2624) - Delete associated content type and permissions when removing InterfaceConnection model
 
 ## API Changes

+ 7 - 0
netbox/dcim/filters.py

@@ -7,6 +7,7 @@ from netaddr.core import AddrFormatError
 
 from extras.filters import CustomFieldFilterSet
 from tenancy.models import Tenant
+from utilities.constants import COLOR_CHOICES
 from utilities.filters import NullableCharFieldFilter, NumericInFilter, TagFilter
 from virtualization.models import Cluster
 from .constants import *
@@ -929,6 +930,12 @@ class CableFilter(django_filters.FilterSet):
         method='search',
         label='Search',
     )
+    type = django_filters.MultipleChoiceFilter(
+        choices=CABLE_TYPE_CHOICES
+    )
+    color = django_filters.MultipleChoiceFilter(
+        choices=COLOR_CHOICES
+    )
 
     class Meta:
         model = Cable

+ 4 - 3
netbox/dcim/forms.py

@@ -2501,9 +2501,10 @@ class CableFilterForm(BootstrapMixin, forms.Form):
         annotate_field='type',
         required=False
     )
-    color = forms.ChoiceField(
-        choices=add_blank_choice(COLOR_CHOICES),
-        widget=ColorSelect(),
+    color = AnnotatedMultipleChoiceField(
+        choices=COLOR_CHOICES,
+        annotate=Cable.objects.all(),
+        annotate_field='color',
         required=False
     )
 

+ 26 - 7
netbox/utilities/constants.py

@@ -1,7 +1,26 @@
-from utilities.forms import ChainedModelMultipleChoiceField
-
-
-# Fields which are used on ManyToMany relationships
-M2M_FIELD_TYPES = [
-    ChainedModelMultipleChoiceField,
-]
+COLOR_CHOICES = (
+    ('aa1409', 'Dark red'),
+    ('f44336', 'Red'),
+    ('e91e63', 'Pink'),
+    ('ff66ff', 'Fuschia'),
+    ('9c27b0', 'Purple'),
+    ('673ab7', 'Dark purple'),
+    ('3f51b5', 'Indigo'),
+    ('2196f3', 'Blue'),
+    ('03a9f4', 'Light blue'),
+    ('00bcd4', 'Cyan'),
+    ('009688', 'Teal'),
+    ('2f6a31', 'Dark green'),
+    ('4caf50', 'Green'),
+    ('8bc34a', 'Light green'),
+    ('cddc39', 'Lime'),
+    ('ffeb3b', 'Yellow'),
+    ('ffc107', 'Amber'),
+    ('ff9800', 'Orange'),
+    ('ff5722', 'Dark orange'),
+    ('795548', 'Brown'),
+    ('c0c0c0', 'Light grey'),
+    ('9e9e9e', 'Grey'),
+    ('607d8b', 'Dark grey'),
+    ('111111', 'Black'),
+)

+ 1 - 26
netbox/utilities/forms.py

@@ -10,34 +10,9 @@ from django.db.models import Count
 from django.urls import reverse_lazy
 from mptt.forms import TreeNodeMultipleChoiceField
 
+from .constants import *
 from .validators import EnhancedURLValidator
 
-COLOR_CHOICES = (
-    ('aa1409', 'Dark red'),
-    ('f44336', 'Red'),
-    ('e91e63', 'Pink'),
-    ('ff66ff', 'Fuschia'),
-    ('9c27b0', 'Purple'),
-    ('673ab7', 'Dark purple'),
-    ('3f51b5', 'Indigo'),
-    ('2196f3', 'Blue'),
-    ('03a9f4', 'Light blue'),
-    ('00bcd4', 'Cyan'),
-    ('009688', 'Teal'),
-    ('2f6a31', 'Dark green'),
-    ('4caf50', 'Green'),
-    ('8bc34a', 'Light green'),
-    ('cddc39', 'Lime'),
-    ('ffeb3b', 'Yellow'),
-    ('ffc107', 'Amber'),
-    ('ff9800', 'Orange'),
-    ('ff5722', 'Dark orange'),
-    ('795548', 'Brown'),
-    ('c0c0c0', 'Light grey'),
-    ('9e9e9e', 'Grey'),
-    ('607d8b', 'Dark grey'),
-    ('111111', 'Black'),
-)
 NUMERIC_EXPANSION_PATTERN = r'\[((?:\d+[?:,-])+\d+)\]'
 ALPHANUMERIC_EXPANSION_PATTERN = r'\[((?:[a-zA-Z0-9]+[?:,-])+[a-zA-Z0-9]+)\]'
 IP4_EXPANSION_PATTERN = r'\[((?:[0-9]{1,3}[?:,-])+[0-9]{1,3})\]'