Fix form validation

This commit is contained in:
Jeremy Stretch 2023-11-09 10:34:03 -05:00
parent ade0eda9e0
commit fc7449b007

View File

@ -517,20 +517,18 @@ class ServiceImportForm(NetBoxModelImportForm):
class Meta: class Meta:
model = Service model = Service
fields = ( fields = (
'device', 'virtual_machine', 'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments', 'tags') 'device', 'virtual_machine', 'ipaddresses', 'name', 'protocol', 'ports', 'description', 'comments', 'tags',
)
def clean_ipaddresses(self): def clean_ipaddresses(self):
parent = self.cleaned_data.get('device') or self.cleaned_data.get('virtual_machine')
for ip_address in self.cleaned_data['ipaddresses']:
if not ip_address.assigned_object or getattr(ip_address.assigned_object, 'parent_object') != parent:
raise forms.ValidationError(
_("{ip} is not assigned to this device/VM.").format(ip=ip_address)
)
device = self.cleaned_data.get('device') return self.cleaned_data['ipaddresses']
virtual_machine = self.cleaned_data.get('virtual_machine')
for ip_address in self.cleaned_data.get('ipaddresses'):
if device and ip_address != device.primary_ip4:
raise forms.ValidationError(f"Device is not assigned to this {ip_address}")
if virtual_machine and ip_address != virtual_machine.primary_ip4:
raise forms.ValidationError(f"Virtual Machine is not assigned to this {ip_address}")
return self.cleaned_data
class L2VPNImportForm(NetBoxModelImportForm): class L2VPNImportForm(NetBoxModelImportForm):