8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-23 07:23:46 +00:00

Merge branch 'extension_domain_update' into 'dev'

Update domains when ip_type or machine_type are updated

See merge request re2o/re2o!543
This commit is contained in:
chirac 2020-06-09 23:32:20 +02:00
commit 01d9a1a596
3 changed files with 323 additions and 288 deletions

File diff suppressed because it is too large Load diff

View file

@ -353,6 +353,12 @@ class MachineType(RevMixin, AclMixin, models.Model):
"""Get all interfaces of the current machine type (self).""" """Get all interfaces of the current machine type (self)."""
return Interface.objects.filter(machine_type=self) return Interface.objects.filter(machine_type=self)
def update_domains(self):
"""Update domains extension with the extension of interface_parent. Called after update of an ip_type or a machine_type object. Exceptions are handled in the views.
(Calling domain.clear() for all domains could take several minutes)
"""
Domain.objects.filter(interface_parent__machine_type=self).update(extension=self.ip_type.extension)
@staticmethod @staticmethod
def can_use_all(user_request, *_args, **_kwargs): def can_use_all(user_request, *_args, **_kwargs):
"""Check if an user can use all machine types. """Check if an user can use all machine types.
@ -594,6 +600,10 @@ class IpType(RevMixin, AclMixin, models.Model):
else: else:
return None 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): def clean(self):
""" """
Check if: Check if:
@ -1417,6 +1427,10 @@ class Interface(RevMixin, AclMixin, FieldPermissionModelMixin, models.Model):
interface.save() interface.save()
reversion.set_comment("IPv4 assignment") 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): def update_type(self):
"""Reassign addresses when the IP type of the machine type changed.""" """Reassign addresses when the IP type of the machine type changed."""
self.clean() self.clean()
@ -2430,7 +2444,8 @@ def machine_post_delete(**kwargs):
@receiver(post_save, sender=Interface) @receiver(post_save, sender=Interface)
def interface_post_save(**kwargs): 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 = kwargs["instance"]
interface.sync_ipv6() interface.sync_ipv6()
@ -2439,6 +2454,10 @@ def interface_post_save(**kwargs):
# Regen services # Regen services
regen("dhcp") regen("dhcp")
regen("mac_ip_list") regen("mac_ip_list")
# Update associated domains
for domain in interface.all_domains():
domain.clean()
domain.save()
@receiver(post_delete, sender=Interface) @receiver(post_delete, sender=Interface)
@ -2456,6 +2475,8 @@ def iptype_post_save(**kwargs):
iptype = kwargs["instance"] iptype = kwargs["instance"]
iptype.gen_ip_range() iptype.gen_ip_range()
iptype.check_replace_prefixv6() iptype.check_replace_prefixv6()
for machinetype in iptype.all_machine_types():
machinetype.save()
@receiver(post_save, sender=MachineType) @receiver(post_save, sender=MachineType)
@ -2464,8 +2485,7 @@ def machinetype_post_save(**kwargs):
parent IP type). parent IP type).
""" """
machinetype = kwargs["instance"] machinetype = kwargs["instance"]
for interface in machinetype.all_interfaces(): machinetype.update_domains()
interface.update_type()
@receiver(post_save, sender=Domain) @receiver(post_save, sender=Domain)

View file

@ -35,6 +35,7 @@ from __future__ import unicode_literals
from django.contrib import messages from django.contrib import messages
from django.contrib.auth.decorators import login_required, permission_required from django.contrib.auth.decorators import login_required, permission_required
from django.db.models import ProtectedError, F from django.db.models import ProtectedError, F
from django.db import IntegrityError
from django.forms import modelformset_factory from django.forms import modelformset_factory
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
@ -506,8 +507,11 @@ def edit_iptype(request, iptype_instance, **_kwargs):
iptype = EditIpTypeForm(request.POST or None, instance=iptype_instance) iptype = EditIpTypeForm(request.POST or None, instance=iptype_instance)
if iptype.is_valid(): if iptype.is_valid():
if iptype.changed_data: if iptype.changed_data:
iptype.save() try:
messages.success(request, _("The IP type was edited.")) iptype.save()
messages.success(request, _("The IP type was edited."))
except IntegrityError as e:
messages.success(request, _("This IP type change would create duplicated domains"))
return redirect(reverse("machines:index-iptype")) return redirect(reverse("machines:index-iptype"))
return form( return form(
{"iptypeform": iptype, "action_name": _("Edit")}, {"iptypeform": iptype, "action_name": _("Edit")},
@ -572,8 +576,11 @@ def edit_machinetype(request, machinetype_instance, **_kwargs):
machinetype = MachineTypeForm(request.POST or None, instance=machinetype_instance) machinetype = MachineTypeForm(request.POST or None, instance=machinetype_instance)
if machinetype.is_valid(): if machinetype.is_valid():
if machinetype.changed_data: if machinetype.changed_data:
machinetype.save() try:
messages.success(request, _("The machine type was edited.")) machinetype.save()
messages.success(request, _("The machine type was edited."))
except IntegrityError as e:
messages.error(request, _("This machine type change would create duplicated domains"))
return redirect(reverse("machines:index-machinetype")) return redirect(reverse("machines:index-machinetype"))
return form( return form(
{"machinetypeform": machinetype, "action_name": _("Edit")}, {"machinetypeform": machinetype, "action_name": _("Edit")},