Clean up RackType attr inheritance

This commit is contained in:
Jeremy Stretch 2024-07-16 08:22:13 -04:00
parent 479c319634
commit fdb5bb6a1a
3 changed files with 24 additions and 41 deletions

View File

@ -263,12 +263,6 @@ class RackForm(TenancyForm, NetBoxModelForm):
FieldSet('tenant_group', 'tenant', name=_('Tenancy')), FieldSet('tenant_group', 'tenant', name=_('Tenancy')),
) )
# Fields which cannot be set locally if a RackType is assigned
RACKTYPE_FIELDS = [
'form_factor', 'width', 'u_height', 'starting_unit', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit',
'mounting_depth', 'weight', 'weight_unit', 'max_weight'
]
class Meta: class Meta:
model = Rack model = Rack
fields = [ fields = [
@ -290,7 +284,7 @@ class RackForm(TenancyForm, NetBoxModelForm):
# Omit RackType-defined fields if rack_type is set # Omit RackType-defined fields if rack_type is set
if get_field_value(self, 'rack_type'): if get_field_value(self, 'rack_type'):
for field_name in self.RACKTYPE_FIELDS: for field_name in Rack.RACKTYPE_FIELDS:
del self.fields[field_name] del self.fields[field_name]
else: else:
self.fieldsets = ( self.fieldsets = (

View File

@ -86,7 +86,7 @@ class Migration(migrations.Migration):
blank=True, blank=True,
null=True, null=True,
on_delete=django.db.models.deletion.PROTECT, on_delete=django.db.models.deletion.PROTECT,
related_name='instances', related_name='racks',
to='dcim.racktype', to='dcim.racktype',
), ),
), ),

View File

@ -41,7 +41,6 @@ class RackBase(WeightMixin, PrimaryModel):
""" """
Base class for RackType & Rack. Holds Base class for RackType & Rack. Holds
""" """
# Rack type
form_factor = models.CharField( form_factor = models.CharField(
choices=RackFormFactorChoices, choices=RackFormFactorChoices,
max_length=50, max_length=50,
@ -177,8 +176,6 @@ class RackType(RackBase):
raise ValidationError(_("Must specify a unit when setting a maximum weight")) raise ValidationError(_("Must specify a unit when setting a maximum weight"))
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
update = self.pk
# Store the given max weight (if any) in grams for use in database ordering # Store the given max weight (if any) in grams for use in database ordering
if self.max_weight and self.weight_unit: if self.max_weight and self.weight_unit:
self._abs_max_weight = to_grams(self.max_weight, self.weight_unit) self._abs_max_weight = to_grams(self.max_weight, self.weight_unit)
@ -190,22 +187,12 @@ class RackType(RackBase):
self.outer_unit = '' self.outer_unit = ''
super().save(*args, **kwargs) super().save(*args, **kwargs)
if update:
# Update all racks associated with this rack_type # Update all Racks associated with this RackType
self.instances.update( for rack in self.racks.all():
form_factor=self.form_factor, rack.snapshot()
width=self.width, rack.copy_racktype_attrs()
u_height=self.u_height, rack.save()
starting_unit=self.starting_unit,
desc_units=self.desc_units,
outer_width=self.outer_width,
outer_depth=self.outer_depth,
outer_unit=self.outer_unit,
weight=self.weight,
weight_unit=self.weight_unit,
max_weight=self.max_weight,
mounting_depth=self.mounting_depth
)
@property @property
def units(self): def units(self):
@ -244,10 +231,16 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, RackBase):
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.
""" """
# Fields which cannot be set locally if a RackType is assigned
RACKTYPE_FIELDS = [
'form_factor', 'width', 'u_height', 'starting_unit', 'desc_units', 'outer_width', 'outer_depth', 'outer_unit',
'mounting_depth', 'weight', 'weight_unit', 'max_weight'
]
rack_type = models.ForeignKey( rack_type = models.ForeignKey(
to='dcim.RackType', to='dcim.RackType',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='instances', related_name='racks',
blank=True, blank=True,
null=True, null=True,
) )
@ -396,19 +389,7 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, RackBase):
}) })
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
if (not self.pk) and self.rack_type: self.copy_racktype_attrs()
self.form_factor = self.rack_type.form_factor
self.width = self.rack_type.width
self.u_height = self.rack_type.u_height
self.starting_unit = self.rack_type.starting_unit
self.desc_units = self.rack_type.desc_units
self.outer_width = self.rack_type.outer_width
self.outer_depth = self.rack_type.outer_depth
self.outer_unit = self.rack_type.outer_unit
self.weight = self.rack_type.weight
self.weight_unit = self.rack_type.weight_unit
self.max_weight = self.rack_type.max_weight
self.mounting_depth = self.rack_type.mounting_depth
# Store the given max weight (if any) in grams for use in database ordering # Store the given max weight (if any) in grams for use in database ordering
if self.max_weight and self.weight_unit: if self.max_weight and self.weight_unit:
@ -422,6 +403,14 @@ class Rack(ContactsMixin, ImageAttachmentsMixin, RackBase):
super().save(*args, **kwargs) super().save(*args, **kwargs)
def copy_racktype_attrs(self):
"""
Copy physical attributes from the assigned RackType (if any).
"""
if self.rack_type:
for field_name in self.RACKTYPE_FIELDS:
setattr(self, field_name, getattr(self.rack_type, field_name))
@property @property
def units(self): def units(self):
""" """