mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-05 01:16:27 +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:
commit
01d9a1a596
3 changed files with 323 additions and 288 deletions
File diff suppressed because it is too large
Load diff
|
@ -353,6 +353,12 @@ class MachineType(RevMixin, AclMixin, models.Model):
|
|||
"""Get all interfaces of the current 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
|
||||
def can_use_all(user_request, *_args, **_kwargs):
|
||||
"""Check if an user can use all machine types.
|
||||
|
@ -594,6 +600,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 +1427,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 +2444,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 +2454,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 +2475,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)
|
||||
|
@ -2464,8 +2485,7 @@ def machinetype_post_save(**kwargs):
|
|||
parent IP type).
|
||||
"""
|
||||
machinetype = kwargs["instance"]
|
||||
for interface in machinetype.all_interfaces():
|
||||
interface.update_type()
|
||||
machinetype.update_domains()
|
||||
|
||||
|
||||
@receiver(post_save, sender=Domain)
|
||||
|
|
|
@ -35,6 +35,7 @@ from __future__ import unicode_literals
|
|||
from django.contrib import messages
|
||||
from django.contrib.auth.decorators import login_required, permission_required
|
||||
from django.db.models import ProtectedError, F
|
||||
from django.db import IntegrityError
|
||||
from django.forms import modelformset_factory
|
||||
from django.http import HttpResponse
|
||||
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)
|
||||
if iptype.is_valid():
|
||||
if iptype.changed_data:
|
||||
try:
|
||||
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 form(
|
||||
{"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)
|
||||
if machinetype.is_valid():
|
||||
if machinetype.changed_data:
|
||||
try:
|
||||
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 form(
|
||||
{"machinetypeform": machinetype, "action_name": _("Edit")},
|
||||
|
|
Loading…
Reference in a new issue