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
This commit is contained in:
bctiemann 2025-01-17 08:45:17 -05:00 committed by GitHub
parent 993d8f1480
commit 4a1fea3504
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -374,22 +374,27 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, RackBase):
if not self._state.adding: if not self._state.adding:
mounted_devices = Device.objects.filter(rack=self).exclude(position__isnull=True).order_by('position') 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 # Validate that Rack is tall enough to house the highest mounted Device
if top_device := mounted_devices.last(): if top_device := mounted_devices.last():
min_height = top_device.position + top_device.device_type.u_height - self.starting_unit min_height = top_device.position + top_device.device_type.u_height - effective_starting_unit
if self.u_height < min_height: if effective_u_height < min_height:
field = 'rack_type' if self.rack_type else 'u_height'
raise ValidationError({ raise ValidationError({
'u_height': _( field: _(
"Rack must be at least {min_height}U tall to house currently installed devices." "Rack must be at least {min_height}U tall to house currently installed devices."
).format(min_height=min_height) ).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 # 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 last_device := mounted_devices.first():
if self.starting_unit > last_device.position: if effective_starting_unit > last_device.position:
field = 'rack_type' if self.rack_type else 'starting_unit'
raise ValidationError({ raise ValidationError({
'starting_unit': _("Rack unit numbering must begin at {position} or less to house " field: _("Rack unit numbering must begin at {position} or less to house "
"currently installed devices.").format(position=last_device.position) "currently installed devices.").format(position=last_device.position)
}) })
# Validate that Rack was assigned a Location of its same site, if applicable # Validate that Rack was assigned a Location of its same site, if applicable