12826 prevent modification of rack fields if rack_type set

This commit is contained in:
Arthur Hanson 2024-07-11 11:10:34 +07:00
parent cb296ce047
commit 71889df1f0
2 changed files with 21 additions and 0 deletions

View File

@ -271,6 +271,13 @@ class RackForm(TenancyForm, NetBoxModelForm):
'outer_unit', 'mounting_depth', 'weight', 'max_weight', 'weight_unit', 'description', 'comments', 'tags', '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): class RackReservationForm(TenancyForm, NetBoxModelForm):
rack = DynamicModelChoiceField( rack = DynamicModelChoiceField(

View File

@ -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. 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. 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( name = models.CharField(
verbose_name=_('name'), verbose_name=_('name'),
max_length=100 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) '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): def save(self, *args, **kwargs):
if (not self.pk) and self.rack_type: if (not self.pk) and self.rack_type:
self.type = self.rack_type.type
self.width = self.rack_type.width self.width = self.rack_type.width
self.u_height = self.rack_type.u_height self.u_height = self.rack_type.u_height
self.starting_unit = self.rack_type.starting_unit self.starting_unit = self.rack_type.starting_unit