Skip to content

Commit 6939ae4

Browse files
authored
14467 change ChoiceField separator from comma to colon (#14469)
* 14467 change ChoiceField separator from comma to colon * 14467 fix test * 14467 fix test * 14467 use regex for colon detection * 14467 update tests
1 parent 81fa426 commit 6939ae4

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

netbox/extras/forms/model_forms.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import re
23

34
from django import forms
45
from django.conf import settings
@@ -95,8 +96,8 @@ class CustomFieldChoiceSetForm(BootstrapMixin, forms.ModelForm):
9596
required=False,
9697
help_text=mark_safe(_(
9798
'Enter one choice per line. An optional label may be specified for each choice by appending it with a '
98-
'comma. Example:'
99-
) + ' <code>choice1,First Choice</code>')
99+
'colon. Example:'
100+
) + ' <code>choice1:First Choice</code>')
100101
)
101102

102103
class Meta:
@@ -107,7 +108,7 @@ def clean_extra_choices(self):
107108
data = []
108109
for line in self.cleaned_data['extra_choices'].splitlines():
109110
try:
110-
value, label = line.split(',', maxsplit=1)
111+
value, label = re.split(r'(?<!\\):', line, maxsplit=1)
111112
except ValueError:
112113
value, label = line, line
113114
data.append((value, label))

netbox/extras/tests/test_views.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ def setUpTestData(cls):
9898

9999
cls.form_data = {
100100
'name': 'Choice Set X',
101-
'extra_choices': '\n'.join(['X1,Choice 1', 'X2,Choice 2', 'X3,Choice 3'])
101+
'extra_choices': '\n'.join(['X1:Choice 1', 'X2:Choice 2', 'X3:Choice 3'])
102102
}
103103

104104
cls.csv_data = (
@@ -119,6 +119,13 @@ def setUpTestData(cls):
119119
'description': 'New description',
120120
}
121121

122+
# This is here as extra_choices field splits on colon, but is returned
123+
# from DB as comma separated.
124+
def assertInstanceEqual(self, instance, data, exclude=None, api=False):
125+
if 'extra_choices' in data:
126+
data['extra_choices'] = data['extra_choices'].replace(':', ',')
127+
return super().assertInstanceEqual(instance, data, exclude, api)
128+
122129

123130
class CustomLinkTestCase(ViewTestCases.PrimaryObjectViewTestCase):
124131
model = CustomLink

netbox/utilities/forms/widgets/misc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ def format_value(self, value):
6565
if not value:
6666
return None
6767
if type(value) is list:
68-
return '\n'.join([f'{k},{v}' for k, v in value])
68+
return '\n'.join([f'{k}:{v}' for k, v in value])
6969
return value

0 commit comments

Comments
 (0)