Просмотр исходного кода

Fixes #4604: Multi-position rear ports may only be connected to other rear ports

Jeremy Stretch 5 лет назад
Родитель
Сommit
e14e217fcd
2 измененных файлов с 19 добавлено и 12 удалено
  1. 1 0
      docs/release-notes/version-2.8.md
  2. 18 12
      netbox/dcim/models/__init__.py

+ 1 - 0
docs/release-notes/version-2.8.md

@@ -5,6 +5,7 @@ v2.8.4 (FUTURE)
 ### Bug Fixes
 ### Bug Fixes
 
 
 * [#4598](https://github.com/netbox-community/netbox/issues/4598) - Display error message when invalid cable length is specified
 * [#4598](https://github.com/netbox-community/netbox/issues/4598) - Display error message when invalid cable length is specified
+* [#4604](https://github.com/netbox-community/netbox/issues/4604) - Multi-position rear ports may only be connected to other rear ports
 
 
 ---
 ---
 
 

+ 18 - 12
netbox/dcim/models/__init__.py

@@ -2182,23 +2182,29 @@ class Cable(ChangeLoggedModel):
 
 
         # Check that termination types are compatible
         # Check that termination types are compatible
         if type_b not in COMPATIBLE_TERMINATION_TYPES.get(type_a):
         if type_b not in COMPATIBLE_TERMINATION_TYPES.get(type_a):
-            raise ValidationError("Incompatible termination types: {} and {}".format(
-                self.termination_a_type, self.termination_b_type
-            ))
+            raise ValidationError(
+                f"Incompatible termination types: {self.termination_a_type} and {self.termination_b_type}"
+            )
 
 
-        # A RearPort with multiple positions must be connected to a component with an equal number of positions
-        if isinstance(self.termination_a, RearPort) and isinstance(self.termination_b, RearPort):
-            if self.termination_a.positions != self.termination_b.positions:
-                raise ValidationError(
-                    "{} has {} positions and {} has {}. Both terminations must have the same number of positions.".format(
-                        self.termination_a, self.termination_a.positions,
-                        self.termination_b, self.termination_b.positions
+        # A RearPort with multiple positions must be connected to a RearPort with an equal number of positions
+        for term_a, term_b in [
+            (self.termination_a, self.termination_b),
+            (self.termination_b, self.termination_a)
+        ]:
+            if isinstance(term_a, RearPort) and term_a.positions > 1:
+                if not isinstance(term_b, RearPort):
+                    raise ValidationError(
+                        "Rear ports with multiple positions may only be connected to other rear ports"
+                    )
+                elif term_a.positions != term_b.positions:
+                    raise ValidationError(
+                        f"{term_a} has {term_a.positions} position(s) but {term_b} has {term_b.positions}. "
+                        f"Both terminations must have the same number of positions."
                     )
                     )
-                )
 
 
         # A termination point cannot be connected to itself
         # A termination point cannot be connected to itself
         if self.termination_a == self.termination_b:
         if self.termination_a == self.termination_b:
-            raise ValidationError("Cannot connect {} to itself".format(self.termination_a_type))
+            raise ValidationError(f"Cannot connect {self.termination_a_type} to itself")
 
 
         # A front port cannot be connected to its corresponding rear port
         # A front port cannot be connected to its corresponding rear port
         if (
         if (