mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Refactor to_objectchange()
This commit is contained in:
parent
eb00e20269
commit
b67859832a
@ -209,13 +209,9 @@ class CircuitTermination(WebhooksMixin, ChangeLoggedModel, LinkTermination):
|
|||||||
raise ValidationError("A circuit termination cannot attach to both a site and a provider network.")
|
raise ValidationError("A circuit termination cannot attach to both a site and a provider network.")
|
||||||
|
|
||||||
def to_objectchange(self, action):
|
def to_objectchange(self, action):
|
||||||
# Annotate the parent Circuit
|
objectchange = super().to_objectchange(action)
|
||||||
try:
|
objectchange.related_object = self.circuit
|
||||||
circuit = self.circuit
|
return objectchange
|
||||||
except Circuit.DoesNotExist:
|
|
||||||
# Parent circuit has been deleted
|
|
||||||
circuit = None
|
|
||||||
return super().to_objectchange(action, related_object=circuit)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent_object(self):
|
def parent_object(self):
|
||||||
|
@ -70,14 +70,10 @@ class ComponentTemplateModel(WebhooksMixin, ChangeLoggedModel):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError()
|
raise NotImplementedError()
|
||||||
|
|
||||||
def to_objectchange(self, action, related_object=None):
|
def to_objectchange(self, action):
|
||||||
# Annotate the parent DeviceType
|
objectchange = super().to_objectchange(action)
|
||||||
try:
|
objectchange.related_object = self.device_type
|
||||||
device_type = self.device_type
|
return objectchange
|
||||||
except ObjectDoesNotExist:
|
|
||||||
# The parent DeviceType has already been deleted
|
|
||||||
device_type = None
|
|
||||||
return super().to_objectchange(action, related_object=device_type)
|
|
||||||
|
|
||||||
|
|
||||||
class ModularComponentTemplateModel(ComponentTemplateModel):
|
class ModularComponentTemplateModel(ComponentTemplateModel):
|
||||||
@ -102,19 +98,13 @@ class ModularComponentTemplateModel(ComponentTemplateModel):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
def to_objectchange(self, action, related_object=None):
|
def to_objectchange(self, action):
|
||||||
# Annotate the parent DeviceType or ModuleType
|
objectchange = super().to_objectchange(action)
|
||||||
try:
|
if self.device_type is not None:
|
||||||
if getattr(self, 'device_type'):
|
objectchange.related_object = self.device_type
|
||||||
return super().to_objectchange(action, related_object=self.device_type)
|
elif self.module_type is not None:
|
||||||
except ObjectDoesNotExist:
|
objectchange.related_object = self.module_type
|
||||||
pass
|
return objectchange
|
||||||
try:
|
|
||||||
if getattr(self, 'module_type'):
|
|
||||||
return super().to_objectchange(action, related_object=self.module_type)
|
|
||||||
except ObjectDoesNotExist:
|
|
||||||
pass
|
|
||||||
return super().to_objectchange(action)
|
|
||||||
|
|
||||||
def clean(self):
|
def clean(self):
|
||||||
super().clean()
|
super().clean()
|
||||||
|
@ -75,13 +75,9 @@ class ComponentModel(PrimaryModel):
|
|||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
def to_objectchange(self, action):
|
def to_objectchange(self, action):
|
||||||
# Annotate the parent Device
|
objectchange = super().to_objectchange(action)
|
||||||
try:
|
objectchange.related_object = self.device
|
||||||
device = self.device
|
return super().to_objectchange(action)
|
||||||
except ObjectDoesNotExist:
|
|
||||||
# The parent Device has already been deleted
|
|
||||||
device = None
|
|
||||||
return super().to_objectchange(action, related_object=device)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent_object(self):
|
def parent_object(self):
|
||||||
|
@ -418,7 +418,9 @@ class ImageAttachment(WebhooksMixin, ChangeLoggedModel):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
def to_objectchange(self, action):
|
def to_objectchange(self, action):
|
||||||
return super().to_objectchange(action, related_object=self.parent)
|
objectchange = super().to_objectchange(action)
|
||||||
|
objectchange.related_object = self.parent
|
||||||
|
return objectchange
|
||||||
|
|
||||||
|
|
||||||
class JournalEntry(WebhooksMixin, ChangeLoggedModel):
|
class JournalEntry(WebhooksMixin, ChangeLoggedModel):
|
||||||
|
@ -904,8 +904,9 @@ class IPAddress(PrimaryModel):
|
|||||||
super().save(*args, **kwargs)
|
super().save(*args, **kwargs)
|
||||||
|
|
||||||
def to_objectchange(self, action):
|
def to_objectchange(self, action):
|
||||||
# Annotate the assigned object, if any
|
objectchange = super().to_objectchange(action)
|
||||||
return super().to_objectchange(action, related_object=self.assigned_object)
|
objectchange.related_object = self.assigned_object
|
||||||
|
return objectchange
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def family(self):
|
def family(self):
|
||||||
|
@ -57,7 +57,7 @@ class ChangeLoggingMixin(models.Model):
|
|||||||
logger.debug(f"Taking a snapshot of {self}")
|
logger.debug(f"Taking a snapshot of {self}")
|
||||||
self._prechange_snapshot = serialize_object(self)
|
self._prechange_snapshot = serialize_object(self)
|
||||||
|
|
||||||
def to_objectchange(self, action, related_object=None):
|
def to_objectchange(self, action):
|
||||||
"""
|
"""
|
||||||
Return a new ObjectChange representing a change made to this object. This will typically be called automatically
|
Return a new ObjectChange representing a change made to this object. This will typically be called automatically
|
||||||
by ChangeLoggingMiddleware.
|
by ChangeLoggingMiddleware.
|
||||||
@ -65,7 +65,6 @@ class ChangeLoggingMixin(models.Model):
|
|||||||
from extras.models import ObjectChange
|
from extras.models import ObjectChange
|
||||||
objectchange = ObjectChange(
|
objectchange = ObjectChange(
|
||||||
changed_object=self,
|
changed_object=self,
|
||||||
related_object=related_object,
|
|
||||||
object_repr=str(self)[:200],
|
object_repr=str(self)[:200],
|
||||||
action=action
|
action=action
|
||||||
)
|
)
|
||||||
|
@ -441,8 +441,9 @@ class VMInterface(PrimaryModel, BaseInterface):
|
|||||||
})
|
})
|
||||||
|
|
||||||
def to_objectchange(self, action):
|
def to_objectchange(self, action):
|
||||||
# Annotate the parent VirtualMachine
|
objectchange = super().to_objectchange(action)
|
||||||
return super().to_objectchange(action, related_object=self.virtual_machine)
|
objectchange.related_object = self.virtual_machine
|
||||||
|
return objectchange
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def parent_object(self):
|
def parent_object(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user