Commit acf5b158 authored by Gabriel Detraz's avatar Gabriel Detraz Committed by root

Cohérence, l'extension découle maintenant du type de la machine, et l'ip est...

Cohérence, l'extension découle maintenant du type de la machine, et l'ip est assigné aussi en fonction du type
parent c0e3a9c4
......@@ -104,15 +104,20 @@ class AliasForm(ModelForm):
def __init__(self, *args, **kwargs):
if 'infra' in kwargs:
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:
name_user = kwargs.pop('name_user')
nb_machine = kwargs.pop('nb_machine')
initial = kwargs.get('initial', {})
initial['name'] = name_user.lower()+str(nb_machine)
kwargs['initial'] = initial
super(AliasForm, self).__init__(*args, **kwargs)
if not infra:
self.fields['extension'].queryset = Extension.objects.filter(need_infra=False)
super(DomainForm, self).__init__(*args, **kwargs)
class DelAliasForm(ModelForm):
alias = forms.ModelMultipleChoiceField(queryset=Domain.objects.all(), label="Alias actuels", widget=forms.CheckboxSelectMultiple)
......
......@@ -158,8 +158,7 @@ class Interface(models.Model):
return str(EUI(self.mac_address, dialect=mac_bare)).lower()
def clean(self, *args, **kwargs):
self.mac_address = str(EUI(self.mac_address)) or None
if not self.ipv4:
if not self.ipv4 or self.type.ip_type != self.ipv4.ip_type:
self.assign_ipv4()
def assign_ipv4(self):
......@@ -174,6 +173,13 @@ class Interface(models.Model):
def unassign_ipv4(self):
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):
try:
domain = self.domain
......@@ -192,6 +198,13 @@ class Domain(models.Model):
class Meta:
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):
""" 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:
......@@ -206,8 +219,13 @@ class Domain(models.Model):
raise ValidationError("Ce nom de domaine %s contient des carractères interdits." % dns)
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):
return str(self.name) + str(self.extension)
return str(self.name) + str(self.get_extension)
class IpList(models.Model):
PRETTY_NAME = "Addresses ipv4"
......
......@@ -50,7 +50,7 @@ class InterfaceSerializer(serializers.ModelSerializer):
return obj.domain.name
def get_interface_extension(self, obj):
return obj.domain.extension.name
return obj.domain.get_extension.name
def get_macaddress(self, obj):
return str(obj.mac_address)
......
......@@ -44,7 +44,7 @@ from reversion.models import Version
import re
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 users.models import User
from users.models import all_has_access
......@@ -52,7 +52,7 @@ from preferences.models import GeneralOption, OptionalMachine
def all_active_interfaces():
"""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():
""" 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)
interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
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:
if machine.is_valid() and interface.is_valid() and domain.is_valid():
new_machine = machine.save(commit=False)
......@@ -133,7 +133,7 @@ def edit_interface(request, interfaceid):
else:
machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
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:
if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid():
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)
return redirect("/users/profil/" + str(request.user.id))
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:
if interface_form.is_valid() and domain_form.is_valid():
new_interface = interface_form.save(commit=False)
......@@ -553,7 +553,7 @@ def del_alias(request, interfaceid):
def index(request):
options, created = GeneralOption.objects.get_or_create()
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)
page = request.GET.get('page')
try:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment