mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Refactored log_change() methods for device components
This commit is contained in:
parent
5f66710fcd
commit
6dd62dc891
@ -27,25 +27,40 @@ from .fields import ASNField, MACAddressField
|
|||||||
from .querysets import InterfaceQuerySet
|
from .querysets import InterfaceQuerySet
|
||||||
|
|
||||||
|
|
||||||
class ComponentModel(models.Model):
|
class ComponentTemplateModel(models.Model):
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
raise NotImplementedError(
|
|
||||||
"ComponentModel must implement get_component_parent()"
|
|
||||||
)
|
|
||||||
|
|
||||||
def log_change(self, user, request_id, action):
|
def log_change(self, user, request_id, action):
|
||||||
"""
|
"""
|
||||||
Log an ObjectChange including the parent Device/VM.
|
Log an ObjectChange including the parent DeviceType.
|
||||||
"""
|
"""
|
||||||
ObjectChange(
|
ObjectChange(
|
||||||
user=user,
|
user=user,
|
||||||
request_id=request_id,
|
request_id=request_id,
|
||||||
changed_object=self,
|
changed_object=self,
|
||||||
related_object=self.get_component_parent(),
|
related_object=self.device_type,
|
||||||
|
action=action,
|
||||||
|
object_data=serialize_object(self)
|
||||||
|
).save()
|
||||||
|
|
||||||
|
|
||||||
|
class ComponentModel(models.Model):
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
abstract = True
|
||||||
|
|
||||||
|
def log_change(self, user, request_id, action):
|
||||||
|
"""
|
||||||
|
Log an ObjectChange including the parent Device/VM.
|
||||||
|
"""
|
||||||
|
parent = self.device if self.device is not None else getattr(self, 'virtual_machine', None)
|
||||||
|
ObjectChange(
|
||||||
|
user=user,
|
||||||
|
request_id=request_id,
|
||||||
|
changed_object=self,
|
||||||
|
related_object=parent,
|
||||||
action=action,
|
action=action,
|
||||||
object_data=serialize_object(self)
|
object_data=serialize_object(self)
|
||||||
).save()
|
).save()
|
||||||
@ -871,7 +886,7 @@ class DeviceType(ChangeLoggedModel, CustomFieldModel):
|
|||||||
return bool(self.subdevice_role is False)
|
return bool(self.subdevice_role is False)
|
||||||
|
|
||||||
|
|
||||||
class ConsolePortTemplate(ComponentModel):
|
class ConsolePortTemplate(ComponentTemplateModel):
|
||||||
"""
|
"""
|
||||||
A template for a ConsolePort to be created for a new Device.
|
A template for a ConsolePort to be created for a new Device.
|
||||||
"""
|
"""
|
||||||
@ -891,11 +906,8 @@ class ConsolePortTemplate(ComponentModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device_type
|
|
||||||
|
|
||||||
|
class ConsoleServerPortTemplate(ComponentTemplateModel):
|
||||||
class ConsoleServerPortTemplate(ComponentModel):
|
|
||||||
"""
|
"""
|
||||||
A template for a ConsoleServerPort to be created for a new Device.
|
A template for a ConsoleServerPort to be created for a new Device.
|
||||||
"""
|
"""
|
||||||
@ -915,11 +927,8 @@ class ConsoleServerPortTemplate(ComponentModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device_type
|
|
||||||
|
|
||||||
|
class PowerPortTemplate(ComponentTemplateModel):
|
||||||
class PowerPortTemplate(ComponentModel):
|
|
||||||
"""
|
"""
|
||||||
A template for a PowerPort to be created for a new Device.
|
A template for a PowerPort to be created for a new Device.
|
||||||
"""
|
"""
|
||||||
@ -939,11 +948,8 @@ class PowerPortTemplate(ComponentModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device_type
|
|
||||||
|
|
||||||
|
class PowerOutletTemplate(ComponentTemplateModel):
|
||||||
class PowerOutletTemplate(ComponentModel):
|
|
||||||
"""
|
"""
|
||||||
A template for a PowerOutlet to be created for a new Device.
|
A template for a PowerOutlet to be created for a new Device.
|
||||||
"""
|
"""
|
||||||
@ -963,11 +969,8 @@ class PowerOutletTemplate(ComponentModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device_type
|
|
||||||
|
|
||||||
|
class InterfaceTemplate(ComponentTemplateModel):
|
||||||
class InterfaceTemplate(ComponentModel):
|
|
||||||
"""
|
"""
|
||||||
A template for a physical data interface on a new Device.
|
A template for a physical data interface on a new Device.
|
||||||
"""
|
"""
|
||||||
@ -997,11 +1000,8 @@ class InterfaceTemplate(ComponentModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device_type
|
|
||||||
|
|
||||||
|
class DeviceBayTemplate(ComponentTemplateModel):
|
||||||
class DeviceBayTemplate(ComponentModel):
|
|
||||||
"""
|
"""
|
||||||
A template for a DeviceBay to be created for a new parent Device.
|
A template for a DeviceBay to be created for a new parent Device.
|
||||||
"""
|
"""
|
||||||
@ -1021,9 +1021,6 @@ class DeviceBayTemplate(ComponentModel):
|
|||||||
def __str__(self):
|
def __str__(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device_type
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
# Devices
|
# Devices
|
||||||
@ -1562,9 +1559,6 @@ class ConsolePort(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return self.device.get_absolute_url()
|
return self.device.get_absolute_url()
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device
|
|
||||||
|
|
||||||
def to_csv(self):
|
def to_csv(self):
|
||||||
return (
|
return (
|
||||||
self.cs_port.device.identifier if self.cs_port else None,
|
self.cs_port.device.identifier if self.cs_port else None,
|
||||||
@ -1614,9 +1608,6 @@ class ConsoleServerPort(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return self.device.get_absolute_url()
|
return self.device.get_absolute_url()
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device
|
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
||||||
# Check that the parent device's DeviceType is a console server
|
# Check that the parent device's DeviceType is a console server
|
||||||
@ -1671,9 +1662,6 @@ class PowerPort(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return self.device.get_absolute_url()
|
return self.device.get_absolute_url()
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device
|
|
||||||
|
|
||||||
def to_csv(self):
|
def to_csv(self):
|
||||||
return (
|
return (
|
||||||
self.power_outlet.device.identifier if self.power_outlet else None,
|
self.power_outlet.device.identifier if self.power_outlet else None,
|
||||||
@ -1723,9 +1711,6 @@ class PowerOutlet(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return self.device.get_absolute_url()
|
return self.device.get_absolute_url()
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device
|
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
||||||
# Check that the parent device's DeviceType is a PDU
|
# Check that the parent device's DeviceType is a PDU
|
||||||
@ -1831,9 +1816,6 @@ class Interface(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return reverse('dcim:interface', kwargs={'pk': self.pk})
|
return reverse('dcim:interface', kwargs={'pk': self.pk})
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device or self.virtual_machine
|
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
||||||
# Check that the parent device's DeviceType is a network device
|
# Check that the parent device's DeviceType is a network device
|
||||||
@ -1913,7 +1895,7 @@ class Interface(ComponentModel):
|
|||||||
# the component parent will raise DoesNotExist. For more discussion, see
|
# the component parent will raise DoesNotExist. For more discussion, see
|
||||||
# https://github.com/digitalocean/netbox/issues/2323
|
# https://github.com/digitalocean/netbox/issues/2323
|
||||||
try:
|
try:
|
||||||
parent_obj = self.get_component_parent()
|
parent_obj = self.device or self.virtual_machine
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
parent_obj = None
|
parent_obj = None
|
||||||
|
|
||||||
@ -1929,7 +1911,6 @@ class Interface(ComponentModel):
|
|||||||
})
|
})
|
||||||
).save()
|
).save()
|
||||||
|
|
||||||
# TODO: Replace `parent` with get_component_parent() (from ComponentModel)
|
|
||||||
@property
|
@property
|
||||||
def parent(self):
|
def parent(self):
|
||||||
return self.device or self.virtual_machine
|
return self.device or self.virtual_machine
|
||||||
@ -2103,9 +2084,6 @@ class DeviceBay(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return self.device.get_absolute_url()
|
return self.device.get_absolute_url()
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device
|
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
|
|
||||||
# Validate that the parent Device can have DeviceBays
|
# Validate that the parent Device can have DeviceBays
|
||||||
@ -2194,9 +2172,6 @@ class InventoryItem(ComponentModel):
|
|||||||
def get_absolute_url(self):
|
def get_absolute_url(self):
|
||||||
return self.device.get_absolute_url()
|
return self.device.get_absolute_url()
|
||||||
|
|
||||||
def get_component_parent(self):
|
|
||||||
return self.device
|
|
||||||
|
|
||||||
def to_csv(self):
|
def to_csv(self):
|
||||||
return (
|
return (
|
||||||
self.device.name or '{' + self.device.pk + '}',
|
self.device.name or '{' + self.device.pk + '}',
|
||||||
|
Loading…
Reference in New Issue
Block a user