From b791b0edc5bdf6951b8a8a8665f2923efc46a140 Mon Sep 17 00:00:00 2001 From: Dalahro Date: Fri, 13 Jan 2017 22:25:03 +0100 Subject: [PATCH] =?UTF-8?q?Normalement=20ca=20gere=20les=20bugs=20d'ajout?= =?UTF-8?q?=20et=20verifie=20la=20validit=C3=A9=20du=20domaine?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machines/forms.py | 3 ++- machines/views.py | 58 ++++++++++++++++++++++++++--------------------- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index 0c7d9cd4..ffdf4239 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -69,7 +69,8 @@ class AliasForm(ModelForm): fields = ['name','extension'] def __init__(self, *args, **kwargs): - infra = kwargs.pop('infra') + if 'infra' in kwargs: + infra = kwargs.pop('infra') if 'name_user' in kwargs: name_user = kwargs.pop('name_user') nb_machine = kwargs.pop('nb_machine') diff --git a/machines/views.py b/machines/views.py index 381fe259..ab5bf567 100644 --- a/machines/views.py +++ b/machines/views.py @@ -101,32 +101,38 @@ def new_machine(request, userid): 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) - if machine.is_valid() and interface.is_valid(): - new_machine = machine.save(commit=False) - new_machine.user = user - new_interface = interface.save(commit=False) - new_domain = domain.save(commit=False) - if full_domain_validator(request, new_domain): - with transaction.atomic(), reversion.create_revision(): - new_machine.save() - reversion.set_user(request.user) - reversion.set_comment("Création") - new_interface.machine = new_machine - if free_ip(new_interface.type.ip_type) and not new_interface.ipv4: - new_interface = assign_ipv4(new_interface) - elif not new_interface.ipv4: - messages.error(request, u"Il n'y a plus d'ip disponibles") - with transaction.atomic(), reversion.create_revision(): - new_interface.save() - reversion.set_user(request.user) - reversion.set_comment("Création") - new_domain.interface_parent = new_interface - with transaction.atomic(), reversion.create_revision(): - new_domain.save() - reversion.set_user(request.user) - reversion.set_comment("Création") - messages.success(request, "La machine a été crée") - return redirect("/users/profil/" + user) + try: + if machine.is_valid() and interface.is_valid() and domain.is_valid(): + new_machine = machine.save(commit=False) + new_machine.user = user + if len(interface.cleaned_data['mac_address']) != 12: + messages.error(request, u"Adresse mac trop courte") + return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request) + new_interface = interface.save(commit=False) + new_domain = domain.save(commit=False) + if full_domain_validator(request, new_domain): + with transaction.atomic(), reversion.create_revision(): + new_machine.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + new_interface.machine = new_machine + if free_ip(new_interface.type.ip_type) and not new_interface.ipv4: + new_interface = assign_ipv4(new_interface) + elif not new_interface.ipv4: + messages.error(request, u"Il n'y a plus d'ip disponibles") + with transaction.atomic(), reversion.create_revision(): + new_interface.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + new_domain.interface_parent = new_interface + with transaction.atomic(), reversion.create_revision(): + new_domain.save() + reversion.set_user(request.user) + reversion.set_comment("Création") + messages.success(request, "La machine a été crée") + return redirect("/users/profil/" + str(user.id)) + except TypeError: + messages.error(request, u"Adresse mac invalide") return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request) @login_required