Skip to content

Commit 7f17f90

Browse files
committed
Fixes: #18336 - Perform Rack object validation of u_height and starting_unit on rack_type if present (#18395)
* Perform Rack object validation of u_height and starting_unit on rack_type if present * Calculate effective values before doing validation
1 parent b6e30f2 commit 7f17f90

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

netbox/dcim/models/racks.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -374,22 +374,27 @@ def clean(self):
374374
if not self._state.adding:
375375
mounted_devices = Device.objects.filter(rack=self).exclude(position__isnull=True).order_by('position')
376376

377+
effective_u_height = self.rack_type.u_height if self.rack_type else self.u_height
378+
effective_starting_unit = self.rack_type.starting_unit if self.rack_type else self.starting_unit
379+
377380
# Validate that Rack is tall enough to house the highest mounted Device
378381
if top_device := mounted_devices.last():
379-
min_height = top_device.position + top_device.device_type.u_height - self.starting_unit
380-
if self.u_height < min_height:
382+
min_height = top_device.position + top_device.device_type.u_height - effective_starting_unit
383+
if effective_u_height < min_height:
384+
field = 'rack_type' if self.rack_type else 'u_height'
381385
raise ValidationError({
382-
'u_height': _(
386+
field: _(
383387
"Rack must be at least {min_height}U tall to house currently installed devices."
384388
).format(min_height=min_height)
385389
})
386390

387391
# Validate that the Rack's starting unit is less than or equal to the position of the lowest mounted Device
388392
if last_device := mounted_devices.first():
389-
if self.starting_unit > last_device.position:
393+
if effective_starting_unit > last_device.position:
394+
field = 'rack_type' if self.rack_type else 'starting_unit'
390395
raise ValidationError({
391-
'starting_unit': _("Rack unit numbering must begin at {position} or less to house "
392-
"currently installed devices.").format(position=last_device.position)
396+
field: _("Rack unit numbering must begin at {position} or less to house "
397+
"currently installed devices.").format(position=last_device.position)
393398
})
394399

395400
# Validate that Rack was assigned a Location of its same site, if applicable

0 commit comments

Comments
 (0)