1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Merge branch 'develop' into develop-2.9

This commit is contained in:
Jeremy Stretch
2020-08-13 10:06:03 -04:00
6 changed files with 60 additions and 11 deletions

View File

@ -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.

View File

@ -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

View File

@ -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.

View File

@ -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'], CHOICES[0][0])
self.assertEqual(form.cleaned_data['var1'], 'ff0000')
# Validate invalid choices
# 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'], ['ff0000', '00ff00'])
# Validate invalid choice
data = {'var1': 'taupe'}
form = TestScript().as_form(data)
self.assertFalse(form.is_valid())

View File

@ -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']
#

View File

@ -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',