|
|
@@ -3810,58 +3810,36 @@ class CableCSVForm(CSVModelForm):
|
|
|
'color': mark_safe('RGB color in hexadecimal (e.g. <code>00ff00</code>)'),
|
|
|
}
|
|
|
|
|
|
- # TODO: Merge the clean() methods for either end
|
|
|
- def clean_side_a_name(self):
|
|
|
+ def _clean_side(self, side):
|
|
|
+ """
|
|
|
+ Derive a Cable's A/B termination objects.
|
|
|
+
|
|
|
+ :param side: 'a' or 'b'
|
|
|
+ """
|
|
|
+ assert side in 'ab', f"Invalid side designation: {side}"
|
|
|
|
|
|
- device = self.cleaned_data.get('side_a_device')
|
|
|
- content_type = self.cleaned_data.get('side_a_type')
|
|
|
- name = self.cleaned_data.get('side_a_name')
|
|
|
+ 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:
|
|
|
return None
|
|
|
|
|
|
model = content_type.model_class()
|
|
|
try:
|
|
|
- termination_object = model.objects.get(
|
|
|
- device=device,
|
|
|
- name=name
|
|
|
- )
|
|
|
+ termination_object = model.objects.get(device=device, name=name)
|
|
|
if termination_object.cable is not None:
|
|
|
- raise forms.ValidationError(
|
|
|
- "Side A: {} {} is already connected".format(device, termination_object)
|
|
|
- )
|
|
|
+ raise forms.ValidationError(f"Side {side.upper()}: {device} {termination_object} is already connected")
|
|
|
except ObjectDoesNotExist:
|
|
|
- raise forms.ValidationError(
|
|
|
- "A side termination not found: {} {}".format(device, name)
|
|
|
- )
|
|
|
+ raise forms.ValidationError(f"{side.upper()} side termination not found: {device} {name}")
|
|
|
|
|
|
- self.instance.termination_a = termination_object
|
|
|
+ setattr(self.instance, f'termination_{side}', termination_object)
|
|
|
return termination_object
|
|
|
|
|
|
- def clean_side_b_name(self):
|
|
|
-
|
|
|
- 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)
|
|
|
- )
|
|
|
+ def clean_side_a_name(self):
|
|
|
+ return self._clean_side('a')
|
|
|
|
|
|
- self.instance.termination_b = termination_object
|
|
|
- return termination_object
|
|
|
+ def clean_side_b_name(self):
|
|
|
+ return self._clean_side('b')
|
|
|
|
|
|
def clean_length_unit(self):
|
|
|
# Avoid trying to save as NULL
|