소스 검색

Refactor to_objectchange()

jeremystretch 4 년 전
부모
커밋
b67859832a

+ 3 - 7
netbox/circuits/models/circuits.py

@@ -209,13 +209,9 @@ class CircuitTermination(WebhooksMixin, ChangeLoggedModel, LinkTermination):
             raise ValidationError("A circuit termination cannot attach to both a site and a provider network.")
 
     def to_objectchange(self, action):
-        # Annotate the parent Circuit
-        try:
-            circuit = self.circuit
-        except Circuit.DoesNotExist:
-            # Parent circuit has been deleted
-            circuit = None
-        return super().to_objectchange(action, related_object=circuit)
+        objectchange = super().to_objectchange(action)
+        objectchange.related_object = self.circuit
+        return objectchange
 
     @property
     def parent_object(self):

+ 11 - 21
netbox/dcim/models/device_component_templates.py

@@ -70,14 +70,10 @@ class ComponentTemplateModel(WebhooksMixin, ChangeLoggedModel):
         """
         raise NotImplementedError()
 
-    def to_objectchange(self, action, related_object=None):
-        # Annotate the parent DeviceType
-        try:
-            device_type = self.device_type
-        except ObjectDoesNotExist:
-            # The parent DeviceType has already been deleted
-            device_type = None
-        return super().to_objectchange(action, related_object=device_type)
+    def to_objectchange(self, action):
+        objectchange = super().to_objectchange(action)
+        objectchange.related_object = self.device_type
+        return objectchange
 
 
 class ModularComponentTemplateModel(ComponentTemplateModel):
@@ -102,19 +98,13 @@ class ModularComponentTemplateModel(ComponentTemplateModel):
     class Meta:
         abstract = True
 
-    def to_objectchange(self, action, related_object=None):
-        # Annotate the parent DeviceType or ModuleType
-        try:
-            if getattr(self, 'device_type'):
-                return super().to_objectchange(action, related_object=self.device_type)
-        except ObjectDoesNotExist:
-            pass
-        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 to_objectchange(self, action):
+        objectchange = super().to_objectchange(action)
+        if self.device_type is not None:
+            objectchange.related_object = self.device_type
+        elif self.module_type is not None:
+            objectchange.related_object = self.module_type
+        return objectchange
 
     def clean(self):
         super().clean()

+ 3 - 7
netbox/dcim/models/device_components.py

@@ -75,13 +75,9 @@ class ComponentModel(PrimaryModel):
         return self.name
 
     def to_objectchange(self, action):
-        # Annotate the parent Device
-        try:
-            device = self.device
-        except ObjectDoesNotExist:
-            # The parent Device has already been deleted
-            device = None
-        return super().to_objectchange(action, related_object=device)
+        objectchange = super().to_objectchange(action)
+        objectchange.related_object = self.device
+        return super().to_objectchange(action)
 
     @property
     def parent_object(self):

+ 3 - 1
netbox/extras/models/models.py

@@ -418,7 +418,9 @@ class ImageAttachment(WebhooksMixin, ChangeLoggedModel):
             return None
 
     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):

+ 3 - 2
netbox/ipam/models/ip.py

@@ -904,8 +904,9 @@ class IPAddress(PrimaryModel):
         super().save(*args, **kwargs)
 
     def to_objectchange(self, action):
-        # Annotate the assigned object, if any
-        return super().to_objectchange(action, related_object=self.assigned_object)
+        objectchange = super().to_objectchange(action)
+        objectchange.related_object = self.assigned_object
+        return objectchange
 
     @property
     def family(self):

+ 1 - 2
netbox/netbox/models/features.py

@@ -57,7 +57,7 @@ class ChangeLoggingMixin(models.Model):
         logger.debug(f"Taking a snapshot of {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
         by ChangeLoggingMiddleware.
@@ -65,7 +65,6 @@ class ChangeLoggingMixin(models.Model):
         from extras.models import ObjectChange
         objectchange = ObjectChange(
             changed_object=self,
-            related_object=related_object,
             object_repr=str(self)[:200],
             action=action
         )

+ 3 - 2
netbox/virtualization/models.py

@@ -441,8 +441,9 @@ class VMInterface(PrimaryModel, BaseInterface):
             })
 
     def to_objectchange(self, action):
-        # Annotate the parent VirtualMachine
-        return super().to_objectchange(action, related_object=self.virtual_machine)
+        objectchange = super().to_objectchange(action)
+        objectchange.related_object = self.virtual_machine
+        return objectchange
 
     @property
     def parent_object(self):