From 71889df1f0524ef3031d72babe4e1019ea1ceb84 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Thu, 11 Jul 2024 11:10:34 +0700 Subject: [PATCH] 12826 prevent modification of rack fields if rack_type set --- netbox/dcim/forms/model_forms.py | 7 +++++++ netbox/dcim/models/racks.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/netbox/dcim/forms/model_forms.py b/netbox/dcim/forms/model_forms.py index 24c08145f..0b3f8a314 100644 --- a/netbox/dcim/forms/model_forms.py +++ b/netbox/dcim/forms/model_forms.py @@ -271,6 +271,13 @@ class RackForm(TenancyForm, NetBoxModelForm): 'outer_unit', 'mounting_depth', 'weight', 'max_weight', 'weight_unit', 'description', 'comments', 'tags', ] + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + if self.instance.pk and self.instance.rack_type: + for field_name in self.instance.rack_type.RACK_FIELDS: + self.fields[field_name].disabled = True + class RackReservationForm(TenancyForm, NetBoxModelForm): rack = DynamicModelChoiceField( diff --git a/netbox/dcim/models/racks.py b/netbox/dcim/models/racks.py index 43a345b15..b5fed7307 100644 --- a/netbox/dcim/models/racks.py +++ b/netbox/dcim/models/racks.py @@ -42,6 +42,12 @@ class RackType(PrimaryModel, WeightMixin): Devices are housed within Racks. Each rack has a defined height measured in rack units, and a front and rear face. Each Rack is assigned to a Site and (optionally) a Location. """ + RACK_FIELDS = [ + 'type', 'width', 'u_height', 'starting_unit', 'desc_units', 'outer_width', + 'outer_depth', 'outer_unit', 'weight', 'weight_unit', 'max_weight', + 'mounting_depth' + ] + name = models.CharField( verbose_name=_('name'), max_length=100 @@ -424,8 +430,16 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, PrimaryModel, WeightMixin): 'location': _("Location must be from the same site, {site}.").format(site=self.site) }) + if self.rack_type: + for field_name in self.rack_type.RACK_FIELDS: + if getattr(self, field_name, None) != getattr(self.rack_type, field_name, None): + raise ValidationError({ + field_name: _("Cannot modify field {field_name} if rack_type set.").format(field_name=field_name) + }) + def save(self, *args, **kwargs): if (not self.pk) and self.rack_type: + self.type = self.rack_type.type self.width = self.rack_type.width self.u_height = self.rack_type.u_height self.starting_unit = self.rack_type.starting_unit