Преглед изворни кода

Added validation to Cable model

Jeremy Stretch пре 7 година
родитељ
комит
903dfb6f07
1 измењених фајлова са 34 додато и 9 уклоњено
  1. 34 9
      netbox/dcim/models.py

+ 34 - 9
netbox/dcim/models.py

@@ -2413,12 +2413,46 @@ class Cable(ChangeLoggedModel):
             ('termination_b_type', 'termination_b_id'),
         )
 
+    def __init__(self, *args, **kwargs):
+
+        super(Cable, self).__init__(*args, **kwargs)
+
+        # Create an ID string for use by __str__(). We have to save a copy of pk since it's nullified after .delete()
+        # is called.
+        self.id_string = '#{}'.format(self.pk)
+
     def __str__(self):
         return self.label or self.id_string
 
     def get_absolute_url(self):
         return reverse('dcim:cable', args=[self.pk])
 
+    def clean(self):
+
+        # Check that termination types are compatible
+        type_a = self.termination_a_type.model
+        type_b = self.termination_b_type.model
+        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
+            ))
+
+        # Check for an existing Cable connected to either termination object
+        if self.termination_a.cable is not None:
+            raise ValidationError("{} already has a cable attached (#{})".format(
+                self.termination_a, self.termination_a.cable_id
+            ))
+        if self.termination_b.cable is not None:
+            raise ValidationError("{} already has a cable attached (#{})".format(
+                self.termination_b, self.termination_b.cable_id
+            ))
+
+        # Validate length and length_unit
+        if self.length and not self.length_unit:
+            raise ValidationError("Must specify a unit when setting a cable length")
+        if self.length_unit and self.length is None:
+            self.length_unit = None
+
     def save(self, *args, **kwargs):
 
         # Store the given length (if any) in meters for use in database ordering
@@ -2427,15 +2461,6 @@ class Cable(ChangeLoggedModel):
 
         super(Cable, self).save(*args, **kwargs)
 
-    def __init__(self, *args, **kwargs):
-
-        super(Cable, self).__init__(*args, **kwargs)
-
-        # Create an ID string for use by __str__(). We have to save a copy of pk since it's nullified after .delete()
-        # is called.
-        self.id_string = '#{}'.format(self.pk)
-
-
     def get_path_endpoints(self):
         """
         Traverse both ends of a cable path and return its connected endpoints. Note that one or both endpoints may be