8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-05 01:16:27 +00:00

Gestion de l'erreur sur les autres fctions

This commit is contained in:
Dalahro 2017-01-14 12:52:23 +01:00
parent 9be0510f00
commit 5a7c1b36a5
2 changed files with 55 additions and 42 deletions

View file

@ -2,6 +2,7 @@ from django.forms import ModelForm, Form, ValidationError
from django import forms from django import forms
from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType from .models import Domain, Machine, Interface, IpList, MachineType, Extension, Mx, Ns, IpType
from django.db.models import Q from django.db.models import Q
from django.core.validators import validate_email
class EditMachineForm(ModelForm): class EditMachineForm(ModelForm):
class Meta: class Meta:

View file

@ -106,7 +106,7 @@ def new_machine(request, userid):
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
new_machine.user = user new_machine.user = user
if len(interface.cleaned_data['mac_address']) != 12: if len(interface.cleaned_data['mac_address']) != 12:
messages.error(request, u"Adresse mac trop courte") messages.error(request, u"Adresse mac de taille incorrecte")
return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request) return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request)
new_interface = interface.save(commit=False) new_interface = interface.save(commit=False)
new_domain = domain.save(commit=False) new_domain = domain.save(commit=False)
@ -152,27 +152,33 @@ def edit_interface(request, interfaceid):
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 = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)), instance=interface.domain)
if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid(): try:
new_interface = interface_form.save(commit=False) if machine_form.is_valid() and interface_form.is_valid() and domain_form.is_valid():
new_machine = machine_form.save(commit=False) new_interface = interface_form.save(commit=False)
new_domain = domain_form.save(commit=False) new_machine = machine_form.save(commit=False)
if full_domain_validator(request, new_domain): if len(interface_form.cleaned_data['mac_address']) != 12:
with transaction.atomic(), reversion.create_revision(): messages.error(request, u"Adresse mac de taille incorrecte")
new_machine.save() return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
reversion.set_user(request.user) new_domain = domain_form.save(commit=False)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in machine_form.changed_data)) if full_domain_validator(request, new_domain):
if free_ip(new_interface.type.ip_type) and not new_interface.ipv4: with transaction.atomic(), reversion.create_revision():
new_interface = assign_ipv4(new_interface) new_machine.save()
with transaction.atomic(), reversion.create_revision(): reversion.set_user(request.user)
new_interface.save() reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in machine_form.changed_data))
reversion.set_user(request.user) if free_ip(new_interface.type.ip_type) and not new_interface.ipv4:
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in interface_form.changed_data)) new_interface = assign_ipv4(new_interface)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_domain.save() new_interface.save()
reversion.set_user(request.user) reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in domain_form.changed_data)) reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in interface_form.changed_data))
messages.success(request, "La machine a été modifiée") with transaction.atomic(), reversion.create_revision():
return redirect("/users/profil/" + str(interface.machine.user.id)) new_domain.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in domain_form.changed_data))
messages.success(request, "La machine a été modifiée")
return redirect("/users/profil/" + str(interface.machine.user.id))
except TypeError:
messages.error(request, u"Adresse mac invalide")
return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request) return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
@login_required @login_required
@ -210,26 +216,32 @@ def new_interface(request, machineid):
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 = AliasForm(request.POST or None, infra=request.user.has_perms(('infra',)))
if interface_form.is_valid(): try:
new_interface = interface_form.save(commit=False) if interface_form.is_valid() and domain_form.is_valid():
new_interface.machine = machine new_interface = interface_form.save(commit=False)
new_domain = domain_form.save(commit=False) if len(interface_form.cleaned_data['mac_address']) != 12:
if full_domain_validator(request, new_domain): messages.error(request, u"Adresse mac de taille incorrecte")
if free_ip(new_interface.type.ip_type) and not new_interface.ipv4: return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
new_interface = assign_ipv4(new_interface) new_interface.machine = machine
elif not new_interface.ipv4: new_domain = domain_form.save(commit=False)
messages.error(request, u"Il n'y a plus d'ip disponibles") if full_domain_validator(request, new_domain):
with transaction.atomic(), reversion.create_revision(): if free_ip(new_interface.type.ip_type) and not new_interface.ipv4:
new_interface.save() new_interface = assign_ipv4(new_interface)
reversion.set_user(request.user) elif not new_interface.ipv4:
reversion.set_comment("Création") messages.error(request, u"Il n'y a plus d'ip disponibles")
new_domain.interface_parent = new_interface with transaction.atomic(), reversion.create_revision():
with transaction.atomic(), reversion.create_revision(): new_interface.save()
new_domain.save() reversion.set_user(request.user)
reversion.set_user(request.user) reversion.set_comment("Création")
reversion.set_comment("Création") new_domain.interface_parent = new_interface
messages.success(request, "L'interface a été ajoutée") with transaction.atomic(), reversion.create_revision():
return redirect("/users/profil/" + str(machine.user.id)) new_domain.save()
reversion.set_user(request.user)
reversion.set_comment("Création")
messages.success(request, "L'interface a été ajoutée")
return redirect("/users/profil/" + str(machine.user.id))
except TypeError:
messages.error(request, u"Adresse mac invalide")
return form({'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request) return form({'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
@login_required @login_required