Skip to content

Closes #18936: add color name support for cable bulk import #19949

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion netbox/dcim/forms/bulk_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,12 @@ class CableImportForm(NetBoxModelImportForm):
required=False,
help_text=_('Length unit')
)
color = forms.CharField(
label=_('Color'),
required=False,
max_length=16,
help_text=_('Color name (e.g. "Red") or hex code (e.g. "f44336")')
)

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

def _clean_color(self, color):
"""
Derive a colors hex code

:param color: color as hex or color name
"""
color_parsed = color.strip().lower()

for hex_code, label in ColorChoices.CHOICES:
if color.lower() == label.lower():
color_parsed = hex_code

if len(color_parsed) > 6:
raise forms.ValidationError(
_(f"{color} did not match any used color name and was longer than six characters: invalid hex.")
)
return color_parsed

def clean_side_a_name(self):
return self._clean_side('a')

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


def clean_color(self):
color = self.cleaned_data.get('color', None)
return self._clean_color(color) if color is not None else ''
#
# Virtual chassis
#


class VirtualChassisImportForm(NetBoxModelImportForm):
master = CSVModelChoiceField(
label=_('Master'),
Expand Down