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

Fixes #5224: Don't allow a rear port to have fewer positions than the number of mapped front ports

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

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

@@ -16,6 +16,7 @@
 * [#5218](https://github.com/netbox-community/netbox/issues/5218) - Raise validation error if a power port's `allocated_draw` exceeds its `maximum_draw`
 * [#5218](https://github.com/netbox-community/netbox/issues/5218) - Raise validation error if a power port's `allocated_draw` exceeds its `maximum_draw`
 * [#5220](https://github.com/netbox-community/netbox/issues/5220) - Fix API patch request against IP Address endpoint with null assigned_object_type 
 * [#5220](https://github.com/netbox-community/netbox/issues/5220) - Fix API patch request against IP Address endpoint with null assigned_object_type 
 * [#5221](https://github.com/netbox-community/netbox/issues/5221) - Fix bulk component creation for virtual machines
 * [#5221](https://github.com/netbox-community/netbox/issues/5221) - Fix bulk component creation for virtual machines
+* [#5224](https://github.com/netbox-community/netbox/issues/5224) - Don't allow a rear port to have fewer positions than the number of mapped front ports
 
 
 ---
 ---
 
 

+ 17 - 8
netbox/dcim/models/device_components.py

@@ -851,17 +851,16 @@ class FrontPort(CableTermination, ComponentModel):
 
 
         # Validate rear port assignment
         # Validate rear port assignment
         if self.rear_port.device != self.device:
         if self.rear_port.device != self.device:
-            raise ValidationError(
-                "Rear port ({}) must belong to the same device".format(self.rear_port)
-            )
+            raise ValidationError({
+                "rear_port": f"Rear port ({self.rear_port}) must belong to the same device"
+            })
 
 
         # Validate rear port position assignment
         # Validate rear port position assignment
         if self.rear_port_position > self.rear_port.positions:
         if self.rear_port_position > self.rear_port.positions:
-            raise ValidationError(
-                "Invalid rear port position ({}); rear port {} has only {} positions".format(
-                    self.rear_port_position, self.rear_port.name, self.rear_port.positions
-                )
-            )
+            raise ValidationError({
+                "rear_port_position": f"Invalid rear port position ({self.rear_port_position}): Rear port "
+                                      f"{self.rear_port.name} has only {self.rear_port.positions} positions"
+            })
 
 
 
 
 @extras_features('webhooks')
 @extras_features('webhooks')
@@ -891,6 +890,16 @@ class RearPort(CableTermination, ComponentModel):
     def get_absolute_url(self):
     def get_absolute_url(self):
         return reverse('dcim:rearport', kwargs={'pk': self.pk})
         return reverse('dcim:rearport', kwargs={'pk': self.pk})
 
 
+    def clean(self):
+
+        # Check that positions count is greater than or equal to the number of associated FrontPorts
+        frontport_count = self.frontports.count()
+        if self.positions < frontport_count:
+            raise ValidationError({
+                "positions": f"The number of positions cannot be less than the number of mapped front ports "
+                             f"({frontport_count})"
+            })
+
     def to_csv(self):
     def to_csv(self):
         return (
         return (
             self.device.identifier,
             self.device.identifier,