8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2025-01-13 11:44:29 +00:00

Reaffectation totale des ip si modification des machinestypes

This commit is contained in:
Gabriel Detraz 2017-07-23 05:03:56 +02:00 committed by root
parent 33fa778a16
commit f9c9c53ad7

View file

@ -34,6 +34,7 @@ from reversion import revisions as reversion
from re2o.settings import MAIN_EXTENSION from re2o.settings import MAIN_EXTENSION
class Machine(models.Model): class Machine(models.Model):
""" Class définissant une machine, object parent user, objets fils interfaces"""
PRETTY_NAME = "Machine" PRETTY_NAME = "Machine"
user = models.ForeignKey('users.User', on_delete=models.PROTECT) user = models.ForeignKey('users.User', on_delete=models.PROTECT)
@ -44,15 +45,20 @@ class Machine(models.Model):
return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name) return str(self.user) + ' - ' + str(self.id) + ' - ' + str(self.name)
class MachineType(models.Model): class MachineType(models.Model):
""" Type de machine, relié à un type d'ip, affecté aux interfaces"""
PRETTY_NAME = "Type de machine" PRETTY_NAME = "Type de machine"
type = models.CharField(max_length=255) type = models.CharField(max_length=255)
ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT, blank=True, null=True) ip_type = models.ForeignKey('IpType', on_delete=models.PROTECT, blank=True, null=True)
def all_interfaces(self):
return Interface.objects.filter(type=self)
def __str__(self): def __str__(self):
return self.type return self.type
class IpType(models.Model): class IpType(models.Model):
""" Type d'ip, définissant un range d'ip, affecté aux machine types"""
PRETTY_NAME = "Type d'ip" PRETTY_NAME = "Type d'ip"
type = models.CharField(max_length=255) type = models.CharField(max_length=255)
@ -89,6 +95,7 @@ class IpType(models.Model):
obj, created = IpList.objects.get_or_create(ip_type=self, ipv4=str(ip)) obj, created = IpList.objects.get_or_create(ip_type=self, ipv4=str(ip))
def del_ip_range(self): def del_ip_range(self):
""" Methode dépréciée, IpList est en mode cascade et supprimé automatiquement"""
if Interface.objects.filter(ipv4__in=self.ip_objects()): if Interface.objects.filter(ipv4__in=self.ip_objects()):
raise ValidationError("Une ou plusieurs ip du range sont affectées, impossible de supprimer le range") raise ValidationError("Une ou plusieurs ip du range sont affectées, impossible de supprimer le range")
for ip in self.ip_objects(): for ip in self.ip_objects():
@ -173,6 +180,11 @@ class Interface(models.Model):
def unassign_ipv4(self): def unassign_ipv4(self):
self.ipv4 = None self.ipv4 = None
def update_type(self):
""" Lorsque le machinetype est changé de type d'ip, on réassigne"""
self.clean()
self.save()
def save(self, *args, **kwargs): def save(self, *args, **kwargs):
self.mac_address = str(EUI(self.mac_address)) or None self.mac_address = str(EUI(self.mac_address)) or None
# On verifie la cohérence en forçant l'extension par la méthode # On verifie la cohérence en forçant l'extension par la méthode
@ -277,3 +289,9 @@ def iptype_post_save(sender, **kwargs):
iptype = kwargs['instance'] iptype = kwargs['instance']
iptype.gen_ip_range() iptype.gen_ip_range()
@receiver(post_save, sender=MachineType)
def machine_post_save(sender, **kwargs):
machinetype = kwargs['instance']
for interface in machinetype.all_interfaces():
interface.update_type()