Clean up validation

This commit is contained in:
jeremystretch 2022-07-06 12:49:00 -04:00
parent 29a46d2c18
commit 4bb4bbce14

View File

@ -906,13 +906,11 @@ class L2VPNTerminationForm(NetBoxModelForm):
label='L2VPN', label='L2VPN',
fetch_trigger='open' fetch_trigger='open'
) )
device = DynamicModelChoiceField( device = DynamicModelChoiceField(
queryset=Device.objects.all(), queryset=Device.objects.all(),
required=False, required=False,
query_params={} query_params={}
) )
vlan = DynamicModelChoiceField( vlan = DynamicModelChoiceField(
queryset=VLAN.objects.all(), queryset=VLAN.objects.all(),
required=False, required=False,
@ -920,7 +918,6 @@ class L2VPNTerminationForm(NetBoxModelForm):
'available_on_device': '$device' 'available_on_device': '$device'
} }
) )
interface = DynamicModelChoiceField( interface = DynamicModelChoiceField(
queryset=Interface.objects.all(), queryset=Interface.objects.all(),
required=False, required=False,
@ -928,13 +925,11 @@ class L2VPNTerminationForm(NetBoxModelForm):
'device_id': '$device' 'device_id': '$device'
} }
) )
virtual_machine = DynamicModelChoiceField( virtual_machine = DynamicModelChoiceField(
queryset=VirtualMachine.objects.all(), queryset=VirtualMachine.objects.all(),
required=False, required=False,
query_params={} query_params={}
) )
vminterface = DynamicModelChoiceField( vminterface = DynamicModelChoiceField(
queryset=VMInterface.objects.all(), queryset=VMInterface.objects.all(),
required=False, required=False,
@ -967,20 +962,12 @@ class L2VPNTerminationForm(NetBoxModelForm):
super().clean() super().clean()
interface = self.cleaned_data.get('interface') interface = self.cleaned_data.get('interface')
vlan = self.cleaned_data.get('vlan')
vminterface = self.cleaned_data.get('vminterface') vminterface = self.cleaned_data.get('vminterface')
vlan = self.cleaned_data.get('vlan')
if not (interface or vlan or vminterface): if not (interface or vminterface or vlan):
raise ValidationError('You must have either a interface or a VLAN') raise ValidationError('A termination must specify an interface or VLAN.')
if len([x for x in (interface, vminterface, vlan) if x]) > 1:
raise ValidationError('A termination can only have one terminating object (an interface or VLAN).')
if interface and vlan and vminterface: self.instance.assigned_object = interface or vminterface or vlan
raise ValidationError('Cannot assign a interface, vlan and vminterface')
elif interface and vlan:
raise ValidationError('Cannot assign both a interface and vlan')
elif interface and vminterface:
raise ValidationError('Cannot assign both a interface and vminterface')
elif vlan and vminterface:
raise ValidationError('Cannot assign both a vlan and vminterface')
obj = interface or vlan or vminterface
self.instance.assigned_object = obj