Parcourir la source

Fixes #1279: Fix primary_ip assignment during IP address import

Jeremy Stretch il y a 7 ans
Parent
commit
afdf5750b5
2 fichiers modifiés avec 12 ajouts et 18 suppressions
  1. 12 5
      netbox/ipam/forms.py
  2. 0 13
      netbox/ipam/views.py

+ 12 - 5
netbox/ipam/forms.py

@@ -641,16 +641,23 @@ class IPAddressCSVForm(forms.ModelForm):
 
         # Set interface
         if self.cleaned_data['device'] and self.cleaned_data['interface_name']:
-            self.instance.interface = Interface.objects.get(device=self.cleaned_data['device'],
-                                                            name=self.cleaned_data['interface_name'])
+            self.instance.interface = Interface.objects.get(
+                device=self.cleaned_data['device'],
+                name=self.cleaned_data['interface_name']
+            )
+
+        ipaddress = super(IPAddressCSVForm, self).save(*args, **kwargs)
+
         # Set as primary for device
         if self.cleaned_data['is_primary']:
+            device = self.cleaned_data['device']
             if self.instance.address.version == 4:
-                self.instance.primary_ip4_for = self.cleaned_data['device']
+                device.primary_ip4 = ipaddress
             elif self.instance.address.version == 6:
-                self.instance.primary_ip6_for = self.cleaned_data['device']
+                device.primary_ip6 = ipaddress
+            device.save()
 
-        return super(IPAddressCSVForm, self).save(*args, **kwargs)
+        return ipaddress
 
 
 class IPAddressBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):

+ 0 - 13
netbox/ipam/views.py

@@ -665,19 +665,6 @@ class IPAddressBulkImportView(PermissionRequiredMixin, BulkImportView):
     table = tables.IPAddressTable
     default_return_url = 'ipam:ipaddress_list'
 
-    def save_obj(self, obj):
-        obj.save()
-
-        # Update primary IP for device if needed. The Device must be updated directly in the database; otherwise we risk
-        # overwriting a previous IP assignment from the same import (see #861).
-        try:
-            if obj.family == 4 and obj.primary_ip4_for:
-                Device.objects.filter(pk=obj.primary_ip4_for.pk).update(primary_ip4=obj)
-            elif obj.family == 6 and obj.primary_ip6_for:
-                Device.objects.filter(pk=obj.primary_ip6_for.pk).update(primary_ip6=obj)
-        except Device.DoesNotExist:
-            pass
-
 
 class IPAddressBulkEditView(PermissionRequiredMixin, BulkEditView):
     permission_required = 'ipam.change_ipaddress'