Restore interface assignment for IPAddress CSV import

This commit is contained in:
Jeremy Stretch 2020-06-23 16:09:31 -04:00
parent 548127cc88
commit 459e485555

View File

@ -1,7 +1,7 @@
from django import forms from django import forms
from django.core.validators import MaxValueValidator, MinValueValidator from django.core.validators import MaxValueValidator, MinValueValidator
from dcim.models import Device, Rack, Region, Site from dcim.models import Device, Interface, Rack, Region, Site
from extras.forms import ( from extras.forms import (
AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldModelForm, CustomFieldFilterForm, AddRemoveTagsForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldModelForm, CustomFieldFilterForm,
) )
@ -14,7 +14,7 @@ from utilities.forms import (
ExpandableIPAddressField, ReturnURLForm, SlugField, StaticSelect2, StaticSelect2Multiple, TagFilterField, ExpandableIPAddressField, ReturnURLForm, SlugField, StaticSelect2, StaticSelect2Multiple, TagFilterField,
BOOLEAN_WITH_BLANK_CHOICES, BOOLEAN_WITH_BLANK_CHOICES,
) )
from virtualization.models import VirtualMachine from virtualization.models import VirtualMachine, VMInterface
from .choices import * from .choices import *
from .constants import * from .constants import *
from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF from .models import Aggregate, IPAddress, Prefix, RIR, Role, Service, VLAN, VLANGroup, VRF
@ -729,24 +729,24 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
required=False, required=False,
help_text='Functional role' help_text='Functional role'
) )
# device = CSVModelChoiceField( device = CSVModelChoiceField(
# queryset=Device.objects.all(), queryset=Device.objects.all(),
# required=False, required=False,
# to_field_name='name', to_field_name='name',
# help_text='Parent device of assigned interface (if any)' help_text='Parent device of assigned interface (if any)'
# ) )
# virtual_machine = CSVModelChoiceField( virtual_machine = CSVModelChoiceField(
# queryset=VirtualMachine.objects.all(), queryset=VirtualMachine.objects.all(),
# required=False, required=False,
# to_field_name='name', to_field_name='name',
# help_text='Parent VM of assigned interface (if any)' help_text='Parent VM of assigned interface (if any)'
# ) )
# interface = CSVModelChoiceField( interface = CSVModelChoiceField(
# queryset=Interface.objects.all(), queryset=Interface.objects.none(), # Can also refer to VMInterface
# required=False, required=False,
# to_field_name='name', to_field_name='name',
# help_text='Assigned interface' help_text='Assigned interface'
# ) )
is_primary = forms.BooleanField( is_primary = forms.BooleanField(
help_text='Make this the primary IP for the assigned device', help_text='Make this the primary IP for the assigned device',
required=False required=False
@ -759,23 +759,19 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
def __init__(self, data=None, *args, **kwargs): def __init__(self, data=None, *args, **kwargs):
super().__init__(data, *args, **kwargs) super().__init__(data, *args, **kwargs)
# if data: if data:
#
# # Limit interface queryset by assigned device or virtual machine # Limit interface queryset by assigned device
# if data.get('device'): if data.get('device'):
# params = { self.fields['interface'].queryset = Interface.objects.filter(
# f"device__{self.fields['device'].to_field_name}": data.get('device') **{f"device__{self.fields['device'].to_field_name}": data['device']}
# } )
# elif data.get('virtual_machine'):
# params = { # Limit interface queryset by assigned device
# f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data.get('virtual_machine') elif data.get('virtual_machine'):
# } self.fields['interface'].queryset = VMInterface.objects.filter(
# else: **{f"virtual_machine__{self.fields['virtual_machine'].to_field_name}": data['virtual_machine']}
# params = { )
# 'device': None,
# 'virtual_machine': None,
# }
# self.fields['interface'].queryset = self.fields['interface'].queryset.filter(**params)
def clean(self): def clean(self):
super().clean() super().clean()
@ -790,6 +786,10 @@ class IPAddressCSVForm(CustomFieldModelCSVForm):
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
# Set interface assignment
if self.cleaned_data['interface']:
self.instance.assigned_object = self.cleaned_data['interface']
ipaddress = super().save(*args, **kwargs) ipaddress = super().save(*args, **kwargs)
# Set as primary for device/VM # Set as primary for device/VM