Skip to content

Commit ffa9a52

Browse files
authored
Closes #18936: add color name support for cable bulk import (#19949)
1 parent 47320f9 commit ffa9a52

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

netbox/dcim/forms/bulk_import.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,12 @@ class CableImportForm(NetBoxModelImportForm):
14101410
required=False,
14111411
help_text=_('Length unit')
14121412
)
1413+
color = forms.CharField(
1414+
label=_('Color'),
1415+
required=False,
1416+
max_length=16,
1417+
help_text=_('Color name (e.g. "Red") or hex code (e.g. "f44336")')
1418+
)
14131419

14141420
class Meta:
14151421
model = Cable
@@ -1473,6 +1479,24 @@ def _clean_side(self, side):
14731479
setattr(self.instance, f'{side}_terminations', [termination_object])
14741480
return termination_object
14751481

1482+
def _clean_color(self, color):
1483+
"""
1484+
Derive a colors hex code
1485+
1486+
:param color: color as hex or color name
1487+
"""
1488+
color_parsed = color.strip().lower()
1489+
1490+
for hex_code, label in ColorChoices.CHOICES:
1491+
if color.lower() == label.lower():
1492+
color_parsed = hex_code
1493+
1494+
if len(color_parsed) > 6:
1495+
raise forms.ValidationError(
1496+
_(f"{color} did not match any used color name and was longer than six characters: invalid hex.")
1497+
)
1498+
return color_parsed
1499+
14761500
def clean_side_a_name(self):
14771501
return self._clean_side('a')
14781502

@@ -1484,11 +1508,14 @@ def clean_length_unit(self):
14841508
length_unit = self.cleaned_data.get('length_unit', None)
14851509
return length_unit if length_unit is not None else ''
14861510

1487-
1511+
def clean_color(self):
1512+
color = self.cleaned_data.get('color', None)
1513+
return self._clean_color(color) if color is not None else ''
14881514
#
14891515
# Virtual chassis
14901516
#
14911517

1518+
14921519
class VirtualChassisImportForm(NetBoxModelImportForm):
14931520
master = CSVModelChoiceField(
14941521
label=_('Master'),

0 commit comments

Comments
 (0)