@@ -1410,6 +1410,12 @@ class CableImportForm(NetBoxModelImportForm):
1410
1410
required = False ,
1411
1411
help_text = _ ('Length unit' )
1412
1412
)
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
+ )
1413
1419
1414
1420
class Meta :
1415
1421
model = Cable
@@ -1473,6 +1479,24 @@ def _clean_side(self, side):
1473
1479
setattr (self .instance , f'{ side } _terminations' , [termination_object ])
1474
1480
return termination_object
1475
1481
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
+
1476
1500
def clean_side_a_name (self ):
1477
1501
return self ._clean_side ('a' )
1478
1502
@@ -1484,11 +1508,14 @@ def clean_length_unit(self):
1484
1508
length_unit = self .cleaned_data .get ('length_unit' , None )
1485
1509
return length_unit if length_unit is not None else ''
1486
1510
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 ''
1488
1514
#
1489
1515
# Virtual chassis
1490
1516
#
1491
1517
1518
+
1492
1519
class VirtualChassisImportForm (NetBoxModelImportForm ):
1493
1520
master = CSVModelChoiceField (
1494
1521
label = _ ('Master' ),
0 commit comments