mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-23 17:08:41 -06:00
Fixes #1037: Fixed error on VLAN import with duplicate VLAN group names
This commit is contained in:
parent
78adaecb89
commit
7766e1f684
@ -586,27 +586,51 @@ class VLANForm(BootstrapMixin, CustomFieldForm):
|
|||||||
|
|
||||||
|
|
||||||
class VLANFromCSVForm(forms.ModelForm):
|
class VLANFromCSVForm(forms.ModelForm):
|
||||||
site = forms.ModelChoiceField(queryset=Site.objects.all(), required=False, to_field_name='name',
|
site = forms.ModelChoiceField(
|
||||||
error_messages={'invalid_choice': 'Site not found.'})
|
queryset=Site.objects.all(), required=False, to_field_name='name',
|
||||||
group = forms.ModelChoiceField(queryset=VLANGroup.objects.all(), required=False, to_field_name='name',
|
error_messages={'invalid_choice': 'Site not found.'}
|
||||||
error_messages={'invalid_choice': 'VLAN group not found.'})
|
)
|
||||||
tenant = forms.ModelChoiceField(Tenant.objects.all(), to_field_name='name', required=False,
|
group_name = forms.CharField(required=False)
|
||||||
error_messages={'invalid_choice': 'Tenant not found.'})
|
tenant = forms.ModelChoiceField(
|
||||||
|
Tenant.objects.all(), to_field_name='name', required=False,
|
||||||
|
error_messages={'invalid_choice': 'Tenant not found.'}
|
||||||
|
)
|
||||||
status_name = forms.ChoiceField(choices=[(s[1], s[0]) for s in VLAN_STATUS_CHOICES])
|
status_name = forms.ChoiceField(choices=[(s[1], s[0]) for s in VLAN_STATUS_CHOICES])
|
||||||
role = forms.ModelChoiceField(queryset=Role.objects.all(), required=False, to_field_name='name',
|
role = forms.ModelChoiceField(
|
||||||
error_messages={'invalid_choice': 'Invalid role.'})
|
queryset=Role.objects.all(), required=False, to_field_name='name',
|
||||||
|
error_messages={'invalid_choice': 'Invalid role.'}
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = VLAN
|
model = VLAN
|
||||||
fields = ['site', 'group', 'vid', 'name', 'tenant', 'status_name', 'role', 'description']
|
fields = ['site', 'group_name', 'vid', 'name', 'tenant', 'status_name', 'role', 'description']
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
|
||||||
|
super(VLANFromCSVForm, self).clean()
|
||||||
|
|
||||||
|
# Validate VLANGroup
|
||||||
|
group_name = self.cleaned_data.get('group_name')
|
||||||
|
if group_name:
|
||||||
|
try:
|
||||||
|
vlan_group = VLANGroup.objects.get(site=self.cleaned_data.get('site'), name=group_name)
|
||||||
|
except VLANGroup.DoesNotExist:
|
||||||
|
self.add_error('group_name', "Invalid VLAN group {}.".format(group_name))
|
||||||
|
|
||||||
def save(self, *args, **kwargs):
|
def save(self, *args, **kwargs):
|
||||||
m = super(VLANFromCSVForm, self).save(commit=False)
|
|
||||||
|
vlan = super(VLANFromCSVForm, self).save(commit=False)
|
||||||
|
|
||||||
|
# Assign VLANGroup by site and name
|
||||||
|
if self.cleaned_data['group_name']:
|
||||||
|
vlan.group = VLANGroup.objects.get(site=self.cleaned_data['site'], name=self.cleaned_data['group_name'])
|
||||||
|
|
||||||
# Assign VLAN status by name
|
# Assign VLAN status by name
|
||||||
m.status = dict(self.fields['status_name'].choices)[self.cleaned_data['status_name']]
|
vlan.status = dict(self.fields['status_name'].choices)[self.cleaned_data['status_name']]
|
||||||
|
|
||||||
if kwargs.get('commit'):
|
if kwargs.get('commit'):
|
||||||
m.save()
|
vlan.save()
|
||||||
return m
|
return vlan
|
||||||
|
|
||||||
|
|
||||||
class VLANImportForm(BootstrapMixin, BulkImportForm):
|
class VLANImportForm(BootstrapMixin, BulkImportForm):
|
||||||
|
Loading…
Reference in New Issue
Block a user