8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-23 20:03:11 +00:00

Cohérence, l'extension découle maintenant du type de la machine, et l'ip est assigné aussi en fonction du type

This commit is contained in:
Gabriel Detraz 2017-07-23 04:22:22 +02:00 committed by root
parent c0e3a9c4f4
commit acf5b1586f
4 changed files with 36 additions and 13 deletions

View file

@ -104,15 +104,20 @@ class AliasForm(ModelForm):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if 'infra' in kwargs: if 'infra' in kwargs:
infra = kwargs.pop('infra') infra = kwargs.pop('infra')
super(AliasForm, self).__init__(*args, **kwargs)
class DomainForm(AliasForm):
class Meta(AliasForm.Meta):
fields = ['name']
def __init__(self, *args, **kwargs):
if 'name_user' in kwargs: if 'name_user' in kwargs:
name_user = kwargs.pop('name_user') name_user = kwargs.pop('name_user')
nb_machine = kwargs.pop('nb_machine') nb_machine = kwargs.pop('nb_machine')
initial = kwargs.get('initial', {}) initial = kwargs.get('initial', {})
initial['name'] = name_user.lower()+str(nb_machine) initial['name'] = name_user.lower()+str(nb_machine)
kwargs['initial'] = initial kwargs['initial'] = initial
super(AliasForm, self).__init__(*args, **kwargs) super(DomainForm, self).__init__(*args, **kwargs)
if not infra:
self.fields['extension'].queryset = Extension.objects.filter(need_infra=False)
class DelAliasForm(ModelForm): class DelAliasForm(ModelForm):
alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple) alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple)

View file

@ -158,8 +158,7 @@ class Interface(models.Model):
return str(EUI(self.mac_address, dialect=mac_bare)).lower() return str(EUI(self.mac_address, dialect=mac_bare)).lower()
def clean(self, *args, **kwargs): def clean(self, *args, **kwargs):
self.mac_address = str(EUI(self.mac_address)) or None if not self.ipv4 or self.type.ip_type != self.ipv4.ip_type:
if not self.ipv4:
self.assign_ipv4() self.assign_ipv4()
def assign_ipv4(self): def assign_ipv4(self):
@ -174,6 +173,13 @@ class Interface(models.Model):
def unassign_ipv4(self): def unassign_ipv4(self):
self.ipv4 = None self.ipv4 = None
def save(self, *args, **kwargs):
self.mac_address = str(EUI(self.mac_address)) or None
# On verifie la cohérence en forçant l'extension par la méthode
if self.type.ip_type != self.ipv4.ip_type:
raise ValidationError("L'ipv4 et le type de la machine ne correspondent pas")
super(Interface, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
try: try:
domain = self.domain domain = self.domain
@ -192,6 +198,13 @@ class Domain(models.Model):
class Meta: class Meta:
unique_together = ("name", "extension") unique_together = ("name", "extension")
@cached_property
def get_extension(self):
if self.interface_parent:
return self.interface_parent.type.ip_type.extension
else:
return self.extension
def clean(self): def clean(self):
""" Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """ """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """
if self.interface_parent and self.cname: if self.interface_parent and self.cname:
@ -206,8 +219,13 @@ class Domain(models.Model):
raise ValidationError("Ce nom de domaine %s contient des carractères interdits." % dns) raise ValidationError("Ce nom de domaine %s contient des carractères interdits." % dns)
return return
def save(self, *args, **kwargs):
# On verifie la cohérence en forçant l'extension par la méthode
self.extension = self.get_extension
super(Domain, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
return str(self.name) + str(self.extension) return str(self.name) + str(self.get_extension)
class IpList(models.Model): class IpList(models.Model):
PRETTY_NAME = "Addresses ipv4" PRETTY_NAME = "Addresses ipv4"

View file

@ -50,7 +50,7 @@ class InterfaceSerializer(serializers.ModelSerializer):
return obj.domain.name return obj.domain.name
def get_interface_extension(self, obj): def get_interface_extension(self, obj):
return obj.domain.extension.name return obj.domain.get_extension.name
def get_macaddress(self, obj): def get_macaddress(self, obj):
return str(obj.mac_address) return str(obj.mac_address)

View file

@ -44,7 +44,7 @@ from reversion.models import Version
import re import re
from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm
from .forms import EditIpTypeForm, IpTypeForm, DelIpTypeForm, AliasForm, DelAliasForm, NsForm, DelNsForm, MxForm, DelMxForm from .forms import EditIpTypeForm, IpTypeForm, DelIpTypeForm, DomainForm, AliasForm, DelAliasForm, NsForm, DelNsForm, MxForm, DelMxForm
from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain from .models import IpType, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, Domain
from users.models import User from users.models import User
from users.models import all_has_access from users.models import all_has_access
@ -52,7 +52,7 @@ from preferences.models import GeneralOption, OptionalMachine
def all_active_interfaces(): def all_active_interfaces():
"""Renvoie l'ensemble des machines autorisées à sortir sur internet """ """Renvoie l'ensemble des machines autorisées à sortir sur internet """
return Interface.objects.filter(machine__in=Machine.objects.filter(user__in=all_has_access()).filter(active=True)).select_related('domain').select_related('machine').select_related('type').select_related('ipv4').select_related('domain__extension').select_related('ipv4__ip_type').distinct() return Interface.objects.filter(machine__in=Machine.objects.filter(user__in=all_has_access()).filter(active=True)).select_related('domain').select_related('machine').select_related('type').select_related('ipv4').select_related('type__ip_type__extension').select_related('ipv4__ip_type').distinct()
def all_active_assigned_interfaces(): def all_active_assigned_interfaces():
""" Renvoie l'ensemble des machines qui ont une ipv4 assignées et disposant de l'accès internet""" """ Renvoie l'ensemble des machines qui ont une ipv4 assignées et disposant de l'accès internet"""
@ -90,7 +90,7 @@ def new_machine(request, userid):
machine = NewMachineForm(request.POST or None) machine = NewMachineForm(request.POST or None)
interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
nb_machine = Interface.objects.filter(machine__user=userid).count() nb_machine = Interface.objects.filter(machine__user=userid).count()
domain = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), name_user=user.surname, nb_machine=nb_machine) domain = DomainForm(request.POST or None, name_user=user.surname, nb_machine=nb_machine)
try: try:
if machine.is_valid() and interface.is_valid() and domain.is_valid(): if machine.is_valid() and interface.is_valid() and domain.is_valid():
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
@ -133,7 +133,7 @@ def edit_interface(request, interfaceid):
else: else:
machine_form = EditMachineForm(request.POST or None, instance=interface.machine) machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
interface_form = EditInterfaceForm(request.POST or None, instance=interface) interface_form = EditInterfaceForm(request.POST or None, instance=interface)
domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), instance=interface.domain) domain_form = DomainForm(request.POST or None, instance=interface.domain)
try: try:
if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid(): if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid():
new_interface = interface_form.save(commit=False) new_interface = interface_form.save(commit=False)
@ -193,7 +193,7 @@ def new_interface(request, machineid):
messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces) messages.error(request, "Vous avez atteint le maximum d'interfaces autorisées que vous pouvez créer vous même (%s) " % max_lambdauser_interfaces)
return redirect("/users/profil/" + str(request.user.id)) return redirect("/users/profil/" + str(request.user.id))
interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) interface_form = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
domain_form = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',))) domain_form = DomainForm(request.POST or None)
try: try:
if interface_form.is_valid() and domain_form.is_valid(): if interface_form.is_valid() and domain_form.is_valid():
new_interface = interface_form.save(commit=False) new_interface = interface_form.save(commit=False)
@ -553,7 +553,7 @@ def del_alias(request, interfaceid):
def index(request): def index(request):
options, created = GeneralOption.objects.get_or_create() options, created = GeneralOption.objects.get_or_create()
pagination_large_number = options.pagination_large_number pagination_large_number = options.pagination_large_number
machines_list = Machine.objects.select_related('user').prefetch_related('interface_set__domain__extension').prefetch_related('interface_set__ipv4__ip_type__extension').prefetch_related('interface_set__type').prefetch_related('interface_set__domain__related_domain__extension').order_by('pk') machines_list = Machine.objects.select_related('user').prefetch_related('interface_set__domain__extension').prefetch_related('interface_set__ipv4__ip_type').prefetch_related('interface_set__type__ip_type__extension').prefetch_related('interface_set__domain__related_domain__extension').order_by('pk')
paginator = Paginator(machines_list, pagination_large_number) paginator = Paginator(machines_list, pagination_large_number)
page = request.GET.get('page') page = request.GET.get('page')
try: try: