Parcourir la source

Ensure relative coherence of pool and subnet objects, even when they are modified

Baptiste Jonglez il y a 11 ans
Parent
commit
cce9581f30
1 fichiers modifiés avec 8 ajouts et 0 suppressions
  1. 8 0
      coin/resources/models.py

+ 8 - 0
coin/resources/models.py

@@ -30,6 +30,14 @@ class IPPool(models.Model):
             max_subnetsize = 64 if self.inet.version == 6 else 32
             if not self.inet.prefixlen <= self.default_subnetsize <= max_subnetsize:
                 raise ValidationError('Invalid default subnet size')
+            # Check that related subnet are in the pool (useful when
+            # modifying an existing pool that already has subnets
+            # allocated in it)
+            incorrect = [str(subnet) for subnet in self.ipsubnet_set.all()
+                         if not subnet.inet in self.inet]
+            if incorrect:
+                err = 'Some subnets allocated in this pool are outside the pool: {}'.format(incorrect)
+                raise ValidationError(err)
 
     def __unicode__(self):
         return self.name