1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Fixes #3452: Queue deletion ObjectChanges until after response is sent

This commit is contained in:
Jeremy Stretch
2019-08-26 16:52:05 -04:00
parent 03ac2721bc
commit 6e66f8d68a
8 changed files with 77 additions and 106 deletions

View File

@ -37,18 +37,14 @@ class ComponentTemplateModel(models.Model):
"""
raise NotImplementedError()
def log_change(self, user, request_id, action):
"""
Log an ObjectChange including the parent DeviceType.
"""
ObjectChange(
user=user,
request_id=request_id,
def to_objectchange(self, action):
return ObjectChange(
changed_object=self,
related_object=self.device_type,
object_repr=str(self),
action=action,
related_object=self.device_type,
object_data=serialize_object(self)
).save()
)
class ComponentModel(models.Model):
@ -60,23 +56,21 @@ class ComponentModel(models.Model):
class Meta:
abstract = True
def log_change(self, user, request_id, action):
"""
Log an ObjectChange including the parent Device/VM.
"""
def to_objectchange(self, action):
# Annotate the parent Device/VM
try:
parent = getattr(self, 'device', None) or getattr(self, 'virtual_machine', None)
except ObjectDoesNotExist:
# The parent device/VM has already been deleted
parent = None
ObjectChange(
user=user,
request_id=request_id,
return ObjectChange(
changed_object=self,
related_object=parent,
object_repr=str(self),
action=action,
related_object=parent,
object_data=serialize_object(self)
).save()
)
@property
def parent(self):
@ -2327,27 +2321,20 @@ class Interface(CableTermination, ComponentModel):
return super().save(*args, **kwargs)
def log_change(self, user, request_id, action):
"""
Include the connected Interface (if any).
"""
# It's possible that an Interface can be deleted _after_ its parent Device/VM, in which case trying to resolve
# the component parent will raise DoesNotExist. For more discussion, see
# https://github.com/netbox-community/netbox/issues/2323
def to_objectchange(self, action):
# Annotate the parent Device/VM
try:
parent_obj = self.device or self.virtual_machine
except ObjectDoesNotExist:
parent_obj = None
ObjectChange(
user=user,
request_id=request_id,
return ObjectChange(
changed_object=self,
related_object=parent_obj,
object_repr=str(self),
action=action,
related_object=parent_obj,
object_data=serialize_object(self)
).save()
)
# TODO: Remove in v2.7
@property