From 7a6075b741674c34b78005da6161786375924a28 Mon Sep 17 00:00:00 2001 From: chapeau Date: Tue, 9 Jun 2020 11:27:35 +0200 Subject: [PATCH] Update domains when ip_type or machine_type are updated --- machines/models.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/machines/models.py b/machines/models.py index 9a044096..9be66c98 100644 --- a/machines/models.py +++ b/machines/models.py @@ -353,6 +353,9 @@ class MachineType(RevMixin, AclMixin, models.Model): """Get all interfaces of the current machine type (self).""" return Interface.objects.filter(machine_type=self) + def save(self, *args, **kwargs): + super(MachineType, self).save(*args, **kwargs) + @staticmethod def can_use_all(user_request, *_args, **_kwargs): """Check if an user can use all machine types. @@ -594,6 +597,10 @@ class IpType(RevMixin, AclMixin, models.Model): else: return None + def all_machine_types(self): + """Get all machine types associated with this ip type (self).""" + return MachineType.objects.filter(ip_type=self) + def clean(self): """ Check if: @@ -1417,6 +1424,10 @@ class Interface(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model): interface.save() reversion.set_comment("IPv4 assignment") + def all_domains(self): + """Get all domains associated with this interface (self).""" + return Domain.objects.filter(interface_parent=self) + def update_type(self): """Reassign addresses when the IP type of the machine type changed.""" self.clean() @@ -2430,7 +2441,8 @@ def machine_post_delete(**kwargs): @receiver(post_save, sender=Interface) def interface_post_save(**kwargs): - """Synchronise LDAP and regen firewall/DHCP after an interface is edited. + """Synchronise LDAP, regen firewall/DHCP after an interface is edited + and update associated domains """ interface = kwargs["instance"] interface.sync_ipv6() @@ -2439,6 +2451,10 @@ def interface_post_save(**kwargs): # Regen services regen("dhcp") regen("mac_ip_list") + # Update associated domains + for domain in interface.all_domains(): + domain.clean() + domain.save() @receiver(post_delete, sender=Interface) @@ -2456,6 +2472,8 @@ def iptype_post_save(**kwargs): iptype = kwargs["instance"] iptype.gen_ip_range() iptype.check_replace_prefixv6() + for machinetype in iptype.all_machine_types(): + machinetype.save() @receiver(post_save, sender=MachineType)