From e53c4fac47725a049899c57e71841ef6abc236bc Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Mon, 14 Aug 2023 15:17:06 -0400 Subject: [PATCH] Move config_template field to RenderConfigMixin --- netbox/dcim/migrations/0170_configtemplate.py | 2 +- netbox/dcim/models/devices.py | 29 ++++++------------- netbox/dcim/models/mixins.py | 29 +++++++++++++++++++ .../0036_virtualmachine_config_template.py | 2 +- .../virtualization/models/virtualmachines.py | 21 ++------------ 5 files changed, 42 insertions(+), 41 deletions(-) diff --git a/netbox/dcim/migrations/0170_configtemplate.py b/netbox/dcim/migrations/0170_configtemplate.py index b1aac0ad2..f9508424d 100644 --- a/netbox/dcim/migrations/0170_configtemplate.py +++ b/netbox/dcim/migrations/0170_configtemplate.py @@ -13,7 +13,7 @@ class Migration(migrations.Migration): migrations.AddField( model_name='device', name='config_template', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='devices', to='extras.configtemplate'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='%(class)ss', to='extras.configtemplate'), ), migrations.AddField( model_name='devicerole', diff --git a/netbox/dcim/models/devices.py b/netbox/dcim/models/devices.py index 6b8e92743..857251caf 100644 --- a/netbox/dcim/models/devices.py +++ b/netbox/dcim/models/devices.py @@ -24,7 +24,7 @@ from utilities.choices import ColorChoices from utilities.fields import ColorField, CounterCacheField, NaturalOrderingField from utilities.tracking import TrackingModelMixin from .device_components import * -from .mixins import WeightMixin +from .mixins import RenderConfigMixin, WeightMixin __all__ = ( @@ -525,7 +525,14 @@ def update_interface_bridges(device, interface_templates, module=None): interface.save() -class Device(ContactsMixin, ImageAttachmentsMixin, PrimaryModel, ConfigContextModel, TrackingModelMixin): +class Device( + ContactsMixin, + ImageAttachmentsMixin, + RenderConfigMixin, + ConfigContextModel, + TrackingModelMixin, + PrimaryModel +): """ A Device represents a piece of physical hardware mounted within a Rack. Each Device is assigned a DeviceType, DeviceRole, and (optionally) a Platform. Device names are not required, however if one is set it must be unique. @@ -686,13 +693,6 @@ class Device(ContactsMixin, ImageAttachmentsMixin, PrimaryModel, ConfigContextMo validators=[MaxValueValidator(255)], help_text=_('Virtual chassis master election priority') ) - config_template = models.ForeignKey( - to='extras.ConfigTemplate', - on_delete=models.PROTECT, - related_name='devices', - blank=True, - null=True - ) latitude = models.DecimalField( verbose_name=_('latitude'), max_digits=8, @@ -1070,17 +1070,6 @@ class Device(ContactsMixin, ImageAttachmentsMixin, PrimaryModel, ConfigContextMo def interfaces_count(self): return self.vc_interfaces().count() - def get_config_template(self): - """ - Return the appropriate ConfigTemplate (if any) for this Device. - """ - if self.config_template: - return self.config_template - if self.role.config_template: - return self.role.config_template - if self.platform and self.platform.config_template: - return self.platform.config_template - def get_vc_master(self): """ If this Device is a VirtualChassis member, return the VC master. Otherwise, return None. diff --git a/netbox/dcim/models/mixins.py b/netbox/dcim/models/mixins.py index f787c8e97..95f6d41fe 100644 --- a/netbox/dcim/models/mixins.py +++ b/netbox/dcim/models/mixins.py @@ -4,6 +4,11 @@ from django.utils.translation import gettext_lazy as _ from dcim.choices import * from utilities.utils import to_grams +__all__ = ( + 'RenderConfigMixin', + 'WeightMixin', +) + class WeightMixin(models.Model): weight = models.DecimalField( @@ -44,3 +49,27 @@ class WeightMixin(models.Model): # Validate weight and weight_unit if self.weight and not self.weight_unit: raise ValidationError(_("Must specify a unit when setting a weight")) + + +class RenderConfigMixin(models.Model): + config_template = models.ForeignKey( + to='extras.ConfigTemplate', + on_delete=models.PROTECT, + related_name='%(class)ss', + blank=True, + null=True + ) + + class Meta: + abstract = True + + def get_config_template(self): + """ + Return the appropriate ConfigTemplate (if any) for this Device. + """ + if self.config_template: + return self.config_template + if self.role.config_template: + return self.role.config_template + if self.platform and self.platform.config_template: + return self.platform.config_template diff --git a/netbox/virtualization/migrations/0036_virtualmachine_config_template.py b/netbox/virtualization/migrations/0036_virtualmachine_config_template.py index f3f03ce33..0456eea81 100644 --- a/netbox/virtualization/migrations/0036_virtualmachine_config_template.py +++ b/netbox/virtualization/migrations/0036_virtualmachine_config_template.py @@ -15,6 +15,6 @@ class Migration(migrations.Migration): migrations.AddField( model_name='virtualmachine', name='config_template', - field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='virtual_machines', to='extras.configtemplate'), + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, related_name='%(class)ss', to='extras.configtemplate'), ), ] diff --git a/netbox/virtualization/models/virtualmachines.py b/netbox/virtualization/models/virtualmachines.py index 441bb182d..eb6c2a8b0 100644 --- a/netbox/virtualization/models/virtualmachines.py +++ b/netbox/virtualization/models/virtualmachines.py @@ -8,6 +8,7 @@ from django.urls import reverse from django.utils.translation import gettext_lazy as _ from dcim.models import BaseInterface +from dcim.models.mixins import RenderConfigMixin from extras.models import ConfigContextModel from extras.querysets import ConfigContextModelQuerySet from netbox.config import get_config @@ -25,7 +26,7 @@ __all__ = ( ) -class VirtualMachine(ContactsMixin, PrimaryModel, ConfigContextModel): +class VirtualMachine(ContactsMixin, RenderConfigMixin, ConfigContextModel, PrimaryModel): """ A virtual machine which runs inside a Cluster. """ @@ -123,13 +124,6 @@ class VirtualMachine(ContactsMixin, PrimaryModel, ConfigContextModel): null=True, verbose_name=_('disk (GB)') ) - config_template = models.ForeignKey( - to='extras.ConfigTemplate', - on_delete=models.PROTECT, - related_name='virtual_machines', - blank=True, - null=True - ) # Counter fields interface_count = CounterCacheField( @@ -241,17 +235,6 @@ class VirtualMachine(ContactsMixin, PrimaryModel, ConfigContextModel): else: return None - def get_config_template(self): - """ - Return the appropriate ConfigTemplate (if any) for this Device. - """ - if self.config_template: - return self.config_template - if self.role.config_template: - return self.role.config_template - if self.platform and self.platform.config_template: - return self.platform.config_template - class VMInterface(NetBoxModel, BaseInterface, TrackingModelMixin): virtual_machine = models.ForeignKey(