diff --git a/machines/migrations/0014_auto_20160706_1220.py b/machines/migrations/0014_auto_20160706_1220.py index c7d7bf04..c03da02a 100644 --- a/machines/migrations/0014_auto_20160706_1220.py +++ b/machines/migrations/0014_auto_20160706_1220.py @@ -15,6 +15,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='interface', name='dns', - field=models.CharField(unique=True, validators=[machines.models.full_domain_validator], max_length=255, help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"), + field=models.CharField(unique=True, max_length=255, help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"), ), ] diff --git a/machines/migrations/0015_auto_20160707_0105.py b/machines/migrations/0015_auto_20160707_0105.py index 90882c51..b06a96e4 100644 --- a/machines/migrations/0015_auto_20160707_0105.py +++ b/machines/migrations/0015_auto_20160707_0105.py @@ -15,6 +15,6 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='interface', name='dns', - field=models.CharField(unique=True, validators=[machines.models.full_domain_validator], help_text="Obligatoire et unique, doit se terminer en .example et ne pas comporter d'autres points", max_length=255), + field=models.CharField(unique=True, help_text="Obligatoire et unique, doit se terminer en .example et ne pas comporter d'autres points", max_length=255), ), ] diff --git a/machines/migrations/0016_auto_20160708_1633.py b/machines/migrations/0016_auto_20160708_1633.py index dddc1f09..7b00b79c 100644 --- a/machines/migrations/0016_auto_20160708_1633.py +++ b/machines/migrations/0016_auto_20160708_1633.py @@ -23,7 +23,7 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='interface', name='dns', - field=models.CharField(unique=True, max_length=255, validators=[machines.models.full_domain_validator], help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"), + field=models.CharField(unique=True, max_length=255, help_text="Obligatoire et unique, doit se terminer en .rez et ne pas comporter d'autres points"), ), migrations.AddField( model_name='machinetype', diff --git a/machines/models.py b/machines/models.py index c3a5e5bb..c4ad724a 100644 --- a/machines/models.py +++ b/machines/models.py @@ -2,31 +2,6 @@ from django.db import models from django.forms import ValidationError from macaddress.fields import MACAddressField -import re - -def full_domain_validator(interface): - """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """ - HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(? 63: - raise ValidationError( - "Le nom de domaine '%(label)s' est trop long (maximum de 63 caractères).", - params={'label': dns}, - ) - if not HOSTNAME_LABEL_PATTERN.match(dns_short): - raise ValidationError( - "Ce nom de domaine '%(label)s' contient des carractères interdits.", - params={'label': dns}, - ) - return dns - class Machine(models.Model): user = models.ForeignKey('users.User', on_delete=models.PROTECT) type = models.ForeignKey('MachineType', on_delete=models.PROTECT) @@ -66,9 +41,6 @@ class Interface(models.Model): def __str__(self): return self.dns - def clean(self): - self.dns=full_domain_validator(self) - class IpList(models.Model): ipv4 = models.GenericIPAddressField(protocol='IPv4', unique=True) diff --git a/machines/views.py b/machines/views.py index c7685c95..f20a6aa6 100644 --- a/machines/views.py +++ b/machines/views.py @@ -8,11 +8,33 @@ from django.template import Context, RequestContext, loader from django.contrib import messages from django.contrib.auth.decorators import login_required, permission_required from django.db.models import ProtectedError +from django.forms import ValidationError +import re from .forms import NewMachineForm, EditMachineForm, EditInterfaceForm, AddInterfaceForm, NewInterfaceForm, MachineTypeForm, DelMachineTypeForm, ExtensionForm, DelExtensionForm, BaseEditInterfaceForm, BaseEditMachineForm from .models import Machine, Interface, IpList, MachineType, Extension from users.models import User +def full_domain_validator(request, interface, machine): + """ Validation du nom de domaine, extensions dans type de machine, prefixe pas plus long que 63 caractères """ + HOSTNAME_LABEL_PATTERN = re.compile("(?!-)[A-Z\d-]+(? 63: + messages.error(request, + "Le nom de domaine %s est trop long (maximum de 63 caractères)." % dns) + return False + if not HOSTNAME_LABEL_PATTERN.match(dns_short): + messages.error(request, + "Ce nom de domaine %s contient des carractères interdits." % dns) + return False + return True + def unassign_ips(user): machines = Interface.objects.filter(machine=Machine.objects.filter(user=user)) for machine in machines: @@ -63,16 +85,17 @@ def new_machine(request, userid): if machine.is_valid() and interface.is_valid(): new_machine = machine.save(commit=False) new_machine.user = user - new_machine.save() new_interface = interface.save(commit=False) - new_interface.machine = new_machine - if free_ip() 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") - new_interface.save() - messages.success(request, "La machine a été crée") - return redirect("/users/profil/" + userid) + if full_domain_validator(request, new_interface, new_machine): + new_machine.save() + new_interface.machine = new_machine + if free_ip() 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") + new_interface.save() + messages.success(request, "La machine a été crée") + return redirect("/users/profil/" + userid) return form({'machineform': machine, 'interfaceform': interface}, 'machines/machine.html', request) @login_required @@ -92,10 +115,13 @@ def edit_machine(request, interfaceid): machine_form = EditMachineForm(request.POST or None, instance=interface.machine) interface_form = EditInterfaceForm(request.POST or None, instance=interface) if machine_form.is_valid() and interface_form.is_valid(): - machine_form.save() - interface_form.save() - messages.success(request, "La machine a été modifiée") - return redirect("/users/profil/" + str(interface.machine.user.id)) + new_interface = interface_form.save(commit=False) + new_machine = machine_form.save(commit=False) + if full_domain_validator(request, new_interface, new_machine): + new_machine.save() + new_interface.save() + messages.success(request, "La machine a été modifiée") + return redirect("/users/profil/" + str(interface.machine.user.id)) return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request) @login_required @@ -117,13 +143,14 @@ def new_interface(request, machineid): machine_form.save() new_interface = interface_form.save(commit=False) new_interface.machine = machine - if free_ip() 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") - new_interface.save() - messages.success(request, "L'interface a été ajoutée") - return redirect("/machines/") + if full_domain_validator(request, new_interface, machine): + if free_ip() 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") + new_interface.save() + messages.success(request, "L'interface a été ajoutée") + return redirect("/machines/") return form({'machineform': machine_form, 'interfaceform': interface_form}, 'machines/machine.html', request) @login_required diff --git a/re2o/settings.py b/re2o/settings.py index be2eea1a..e110269d 100644 --- a/re2o/settings.py +++ b/re2o/settings.py @@ -76,16 +76,13 @@ TEMPLATES = [ 'django.template.context_processors.request', 'django.contrib.auth.context_processors.auth', 'django.contrib.messages.context_processors.messages', + 'django.core.context_processors.request', 're2o.context_processors.context_user', ], }, }, ] -TEMPLATE_CONTEXT_PROCESSORS = ( - 'django.core.context_processors.request', - ) - WSGI_APPLICATION = 're2o.wsgi.application'