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

Merge branch 'develop' into develop-2.9

Jeremy Stretch 5 лет назад
Родитель
Сommit
bc61393256

+ 4 - 0
docs/additional-features/custom-scripts.md

@@ -161,6 +161,10 @@ direction = ChoiceVar(choices=CHOICES)
 
 In the example above, selecting the choice labeled "North" will submit the value `n`.
 
+### MultiChoiceVar
+
+Similar to `ChoiceVar`, but allows for the selection of multiple choices.
+
 ### ObjectVar
 
 A particular object within NetBox. Each ObjectVar must specify a particular model, and allows the user to select one of the available instances. ObjectVar accepts several arguments, listed below.

+ 12 - 0
docs/release-notes/version-2.8.md

@@ -1,5 +1,17 @@
 # NetBox v2.8
 
+## v2.8.10 (FUTURE)
+
+### Enhancements
+
+* [#4885](https://github.com/netbox-community/netbox/issues/4885) - Add MultiChoiceVar for custom scripts
+
+### Bug Fixes
+
+* [#4992](https://github.com/netbox-community/netbox/issues/4992) - Add `display_name` to nested VRF serializer
+
+---
+
 ## v2.8.9 (2020-08-04)
 
 ### Enhancements

+ 8 - 0
netbox/extras/scripts.py

@@ -34,6 +34,7 @@ __all__ = [
     'IPAddressVar',
     'IPAddressWithMaskVar',
     'IPNetworkVar',
+    'MultiChoiceVar',
     'MultiObjectVar',
     'ObjectVar',
     'Script',
@@ -167,6 +168,13 @@ class ChoiceVar(ScriptVariable):
         self.field_attrs['choices'] = choices
 
 
+class MultiChoiceVar(ChoiceVar):
+    """
+    Like ChoiceVar, but allows for the selection of multiple choices.
+    """
+    form_field = forms.MultipleChoiceField
+
+
 class ObjectVar(ScriptVariable):
     """
     A single object within NetBox.

+ 34 - 9
netbox/extras/tests/test_scripts.py

@@ -5,6 +5,12 @@ from netaddr import IPAddress, IPNetwork
 from dcim.models import DeviceRole
 from extras.scripts import *
 
+CHOICES = (
+    ('ff0000', 'Red'),
+    ('00ff00', 'Green'),
+    ('0000ff', 'Blue')
+)
+
 
 class ScriptVariablesTest(TestCase):
 
@@ -101,12 +107,6 @@ class ScriptVariablesTest(TestCase):
 
     def test_choicevar(self):
 
-        CHOICES = (
-            ('ff0000', 'Red'),
-            ('00ff00', 'Green'),
-            ('0000ff', 'Blue')
-        )
-
         class TestScript(Script):
 
             var1 = ChoiceVar(
@@ -114,12 +114,37 @@ class ScriptVariablesTest(TestCase):
             )
 
         # Validate valid choice
-        data = {'var1': CHOICES[0][0]}
+        data = {'var1': 'ff0000'}
+        form = TestScript().as_form(data)
+        self.assertTrue(form.is_valid())
+        self.assertEqual(form.cleaned_data['var1'], 'ff0000')
+
+        # Validate invalid choice
+        data = {'var1': 'taupe'}
+        form = TestScript().as_form(data)
+        self.assertFalse(form.is_valid())
+
+    def test_multichoicevar(self):
+
+        class TestScript(Script):
+
+            var1 = MultiChoiceVar(
+                choices=CHOICES
+            )
+
+        # Validate single choice
+        data = {'var1': ['ff0000']}
+        form = TestScript().as_form(data)
+        self.assertTrue(form.is_valid())
+        self.assertEqual(form.cleaned_data['var1'], ['ff0000'])
+
+        # Validate multiple choices
+        data = {'var1': ('ff0000', '00ff00')}
         form = TestScript().as_form(data)
         self.assertTrue(form.is_valid())
-        self.assertEqual(form.cleaned_data['var1'], CHOICES[0][0])
+        self.assertEqual(form.cleaned_data['var1'], ['ff0000', '00ff00'])
 
-        # Validate invalid choices
+        # Validate invalid choice
         data = {'var1': 'taupe'}
         form = TestScript().as_form(data)
         self.assertFalse(form.is_valid())

+ 1 - 1
netbox/ipam/api/nested_serializers.py

@@ -26,7 +26,7 @@ class NestedVRFSerializer(WritableNestedSerializer):
 
     class Meta:
         model = models.VRF
-        fields = ['id', 'url', 'name', 'rd', 'prefix_count']
+        fields = ['id', 'url', 'name', 'rd', 'display_name', 'prefix_count']
 
 
 #

+ 1 - 1
netbox/ipam/tests/test_api.py

@@ -22,7 +22,7 @@ class AppTest(APITestCase):
 
 class VRFTest(APIViewTestCases.APIViewTestCase):
     model = VRF
-    brief_fields = ['id', 'name', 'prefix_count', 'rd', 'url']
+    brief_fields = ['display_name', 'id', 'name', 'prefix_count', 'rd', 'url']
     create_data = [
         {
             'name': 'VRF 4',