Просмотр исходного кода

Merge pull request #2979 from TakeMeNL/fix/2944

Fixes: #2944 Adding/removing an IP address is not recorded on the associated interface's changelog
Jeremy Stretch 7 лет назад
Родитель
Сommit
4334f1bc65
1 измененных файлов с 24 добавлено и 2 удалено
  1. 24 2
      netbox/ipam/models.py

+ 24 - 2
netbox/ipam/models.py

@@ -1,7 +1,7 @@
 import netaddr
 import netaddr
 from django.conf import settings
 from django.conf import settings
 from django.contrib.contenttypes.fields import GenericRelation
 from django.contrib.contenttypes.fields import GenericRelation
-from django.core.exceptions import ValidationError
+from django.core.exceptions import ValidationError, ObjectDoesNotExist
 from django.core.validators import MaxValueValidator, MinValueValidator
 from django.core.validators import MaxValueValidator, MinValueValidator
 from django.db import models
 from django.db import models
 from django.db.models import Q
 from django.db.models import Q
@@ -10,8 +10,9 @@ from django.urls import reverse
 from taggit.managers import TaggableManager
 from taggit.managers import TaggableManager
 
 
 from dcim.models import Interface
 from dcim.models import Interface
-from extras.models import CustomFieldModel
+from extras.models import CustomFieldModel, ObjectChange
 from utilities.models import ChangeLoggedModel
 from utilities.models import ChangeLoggedModel
+from utilities.utils import serialize_object
 from .constants import *
 from .constants import *
 from .fields import IPNetworkField, IPAddressField
 from .fields import IPNetworkField, IPAddressField
 from .querysets import PrefixQuerySet
 from .querysets import PrefixQuerySet
@@ -629,6 +630,27 @@ class IPAddress(ChangeLoggedModel, CustomFieldModel):
             self.family = self.address.version
             self.family = self.address.version
         super().save(*args, **kwargs)
         super().save(*args, **kwargs)
 
 
+    def log_change(self, user, request_id, action):
+        """
+        Include the connected Interface (if any).
+        """
+
+        # It's possible that an IpAddress can be deleted _after_ its parent Interface, in which case trying to resolve
+        # the component parent will raise DoesNotExist.
+        try:
+            parent_obj = self.interface
+        except ObjectDoesNotExist:
+            parent_obj = None
+
+        ObjectChange(
+            user=user,
+            request_id=request_id,
+            changed_object=self,
+            related_object=parent_obj,
+            action=action,
+            object_data=serialize_object(self)
+        ).save()
+
     def to_csv(self):
     def to_csv(self):
 
 
         # Determine if this IP is primary for a Device
         # Determine if this IP is primary for a Device