mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-26 10:28:37 -06:00
Closes #18936: Import cables with color name instead of hex code
This commit is contained in:
parent
e0eb833d7a
commit
8a8c3ce1b9
@ -1396,7 +1396,12 @@ class CableImportForm(NetBoxModelImportForm):
|
|||||||
required=False,
|
required=False,
|
||||||
help_text=_('Length unit')
|
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:
|
class Meta:
|
||||||
model = Cable
|
model = Cable
|
||||||
fields = [
|
fields = [
|
||||||
@ -1440,6 +1445,22 @@ class CableImportForm(NetBoxModelImportForm):
|
|||||||
setattr(self.instance, f'{side}_terminations', [termination_object])
|
setattr(self.instance, f'{side}_terminations', [termination_object])
|
||||||
return 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_parsed == hex_code.lower() or color == label.lower():
|
||||||
|
return hex_code
|
||||||
|
|
||||||
|
raise forms.ValidationError(
|
||||||
|
f"'{color}' is not a valid color. Use a valid color name Example: Red or hex code, Example: f44336"
|
||||||
|
)
|
||||||
|
|
||||||
def clean_side_a_name(self):
|
def clean_side_a_name(self):
|
||||||
return self._clean_side('a')
|
return self._clean_side('a')
|
||||||
|
|
||||||
@ -1451,11 +1472,15 @@ class CableImportForm(NetBoxModelImportForm):
|
|||||||
length_unit = self.cleaned_data.get('length_unit', None)
|
length_unit = self.cleaned_data.get('length_unit', None)
|
||||||
return length_unit if length_unit is not None else ''
|
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
|
# Virtual chassis
|
||||||
#
|
#
|
||||||
|
|
||||||
|
|
||||||
class VirtualChassisImportForm(NetBoxModelImportForm):
|
class VirtualChassisImportForm(NetBoxModelImportForm):
|
||||||
master = CSVModelChoiceField(
|
master = CSVModelChoiceField(
|
||||||
label=_('Master'),
|
label=_('Master'),
|
||||||
|
Loading…
Reference in New Issue
Block a user