mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-24 17:38:37 -06:00
Consolidate CableCSVForm validation logic
This commit is contained in:
parent
8461832a7b
commit
3cbe8ddf02
@ -3810,58 +3810,36 @@ class CableCSVForm(CSVModelForm):
|
|||||||
'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
|
'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
|
||||||
}
|
}
|
||||||
|
|
||||||
# TODO: Merge the clean() methods for either end
|
def _clean_side(self, side):
|
||||||
def clean_side_a_name(self):
|
"""
|
||||||
|
Derive a Cable's A/B termination objects.
|
||||||
|
|
||||||
device = self.cleaned_data.get('side_a_device')
|
:param side: 'a' or 'b'
|
||||||
content_type = self.cleaned_data.get('side_a_type')
|
"""
|
||||||
name = self.cleaned_data.get('side_a_name')
|
assert side in 'ab', f"Invalid side designation: {side}"
|
||||||
|
|
||||||
|
device = self.cleaned_data.get(f'side_{side}_device')
|
||||||
|
content_type = self.cleaned_data.get(f'side_{side}_type')
|
||||||
|
name = self.cleaned_data.get(f'side_{side}_name')
|
||||||
if not device or not content_type or not name:
|
if not device or not content_type or not name:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
model = content_type.model_class()
|
model = content_type.model_class()
|
||||||
try:
|
try:
|
||||||
termination_object = model.objects.get(
|
termination_object = model.objects.get(device=device, name=name)
|
||||||
device=device,
|
|
||||||
name=name
|
|
||||||
)
|
|
||||||
if termination_object.cable is not None:
|
if termination_object.cable is not None:
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(f"Side {side.upper()}: {device} {termination_object} is already connected")
|
||||||
"Side A: {} {} is already connected".format(device, termination_object)
|
|
||||||
)
|
|
||||||
except ObjectDoesNotExist:
|
except ObjectDoesNotExist:
|
||||||
raise forms.ValidationError(
|
raise forms.ValidationError(f"{side.upper()} side termination not found: {device} {name}")
|
||||||
"A side termination not found: {} {}".format(device, name)
|
|
||||||
)
|
|
||||||
|
|
||||||
self.instance.termination_a = termination_object
|
setattr(self.instance, f'termination_{side}', termination_object)
|
||||||
return termination_object
|
return termination_object
|
||||||
|
|
||||||
|
def clean_side_a_name(self):
|
||||||
|
return self._clean_side('a')
|
||||||
|
|
||||||
def clean_side_b_name(self):
|
def clean_side_b_name(self):
|
||||||
|
return self._clean_side('b')
|
||||||
device = self.cleaned_data.get('side_b_device')
|
|
||||||
content_type = self.cleaned_data.get('side_b_type')
|
|
||||||
name = self.cleaned_data.get('side_b_name')
|
|
||||||
if not device or not content_type or not name:
|
|
||||||
return None
|
|
||||||
|
|
||||||
model = content_type.model_class()
|
|
||||||
try:
|
|
||||||
termination_object = model.objects.get(
|
|
||||||
device=device,
|
|
||||||
name=name
|
|
||||||
)
|
|
||||||
if termination_object.cable is not None:
|
|
||||||
raise forms.ValidationError(
|
|
||||||
"Side B: {} {} is already connected".format(device, termination_object)
|
|
||||||
)
|
|
||||||
except ObjectDoesNotExist:
|
|
||||||
raise forms.ValidationError(
|
|
||||||
"B side termination not found: {} {}".format(device, name)
|
|
||||||
)
|
|
||||||
|
|
||||||
self.instance.termination_b = termination_object
|
|
||||||
return termination_object
|
|
||||||
|
|
||||||
def clean_length_unit(self):
|
def clean_length_unit(self):
|
||||||
# Avoid trying to save as NULL
|
# Avoid trying to save as NULL
|
||||||
|
Loading…
Reference in New Issue
Block a user