From 7a9a684ece8758a3b642e1cb5a45255547007517 Mon Sep 17 00:00:00 2001 From: Brian Tiemann Date: Tue, 14 Jan 2025 14:46:31 -0500 Subject: [PATCH 1/2] Perform Rack object validation of u_height and starting_unit on rack_type if present --- netbox/dcim/models/racks.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/netbox/dcim/models/racks.py b/netbox/dcim/models/racks.py index 78eb0ea4a27..c7fce8e4969 100644 --- a/netbox/dcim/models/racks.py +++ b/netbox/dcim/models/racks.py @@ -377,19 +377,23 @@ def clean(self): # Validate that Rack is tall enough to house the highest mounted Device if top_device := mounted_devices.last(): min_height = top_device.position + top_device.device_type.u_height - self.starting_unit - if self.u_height < min_height: + effective_u_height = self.rack_type.u_height if self.rack_type else self.u_height + if effective_u_height < min_height: + field = 'rack_type' if self.rack_type else 'u_height' raise ValidationError({ - 'u_height': _( + field: _( "Rack must be at least {min_height}U tall to house currently installed devices." ).format(min_height=min_height) }) # Validate that the Rack's starting unit is less than or equal to the position of the lowest mounted Device if last_device := mounted_devices.first(): - if self.starting_unit > last_device.position: + effective_starting_unit = self.rack_type.starting_unit if self.rack_type else self.starting_unit + if effective_starting_unit > last_device.position: + field = 'rack_type' if self.rack_type else 'starting_unit' raise ValidationError({ - 'starting_unit': _("Rack unit numbering must begin at {position} or less to house " - "currently installed devices.").format(position=last_device.position) + field: _("Rack unit numbering must begin at {position} or less to house " + "currently installed devices.").format(position=last_device.position) }) # Validate that Rack was assigned a Location of its same site, if applicable From 37a3d3388cfd9e0cb7ed5524c2d2999236abb8e2 Mon Sep 17 00:00:00 2001 From: Brian Tiemann Date: Tue, 14 Jan 2025 14:57:37 -0500 Subject: [PATCH 2/2] Calculate effective values before doing validation --- netbox/dcim/models/racks.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/netbox/dcim/models/racks.py b/netbox/dcim/models/racks.py index c7fce8e4969..7ecbd5d5f87 100644 --- a/netbox/dcim/models/racks.py +++ b/netbox/dcim/models/racks.py @@ -374,10 +374,12 @@ def clean(self): if not self._state.adding: mounted_devices = Device.objects.filter(rack=self).exclude(position__isnull=True).order_by('position') + effective_u_height = self.rack_type.u_height if self.rack_type else self.u_height + effective_starting_unit = self.rack_type.starting_unit if self.rack_type else self.starting_unit + # Validate that Rack is tall enough to house the highest mounted Device if top_device := mounted_devices.last(): - min_height = top_device.position + top_device.device_type.u_height - self.starting_unit - effective_u_height = self.rack_type.u_height if self.rack_type else self.u_height + min_height = top_device.position + top_device.device_type.u_height - effective_starting_unit if effective_u_height < min_height: field = 'rack_type' if self.rack_type else 'u_height' raise ValidationError({ @@ -388,7 +390,6 @@ def clean(self): # Validate that the Rack's starting unit is less than or equal to the position of the lowest mounted Device if last_device := mounted_devices.first(): - effective_starting_unit = self.rack_type.starting_unit if self.rack_type else self.starting_unit if effective_starting_unit > last_device.position: field = 'rack_type' if self.rack_type else 'starting_unit' raise ValidationError({