Commit 85703c40 authored by Gabriel Detraz's avatar Gabriel Detraz Committed by root

Modification du système de gestion des nom de machines

parent 2e515cce
......@@ -208,16 +208,19 @@ class Domain(models.Model):
cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain')
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:
elif hasattr(self,'extension'):
return self.extension
else:
return None
def clean(self):
if self.get_extension():
self.extension=self.get_extension()
""" 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:
raise ValidationError("On ne peut créer à la fois A et CNAME")
......@@ -229,15 +232,18 @@ class Domain(models.Model):
raise ValidationError("Le nom de domaine %s est trop long (maximum de 63 caractères)." % dns)
if not HOSTNAME_LABEL_PATTERN.match(dns):
raise ValidationError("Ce nom de domaine %s contient des carractères interdits." % dns)
return
self.validate_unique()
super(Domain, self).clean()
def save(self, *args, **kwargs):
# On verifie la cohérence en forçant l'extension par la méthode
self.extension = self.get_extension
if not self.get_extension():
raise ValidationError("Extension invalide")
self.full_clean()
super(Domain, self).save(*args, **kwargs)
def __str__(self):
return str(self.name) + str(self.get_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.get_extension.name
return obj.domain.get_extension().name
def get_macaddress(self, obj):
return str(obj.mac_address)
......
......@@ -91,11 +91,12 @@ 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 = 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)
new_machine.user = user
new_interface = interface.save(commit=False)
if machine.is_valid() and interface.is_valid():
new_machine = machine.save(commit=False)
new_machine.user = user
new_interface = interface.save(commit=False)
domain.instance.interface_parent = new_interface
if domain.is_valid():
new_domain = domain.save(commit=False)
with transaction.atomic(), reversion.create_revision():
new_machine.save()
......@@ -113,8 +114,6 @@ def new_machine(request, userid):
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
......@@ -134,27 +133,24 @@ def edit_interface(request, interfaceid):
machine_form = EditMachineForm(request.POST or None, instance=interface.machine)
interface_form = EditInterfaceForm(request.POST or None, instance=interface)
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)
new_machine = machine_form.save(commit=False)
new_domain = domain_form.save(commit=False)
with transaction.atomic(), reversion.create_revision():
new_machine.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in machine_form.changed_data))
with transaction.atomic(), reversion.create_revision():
new_interface.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in interface_form.changed_data))
with transaction.atomic(), reversion.create_revision():
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")
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)
with transaction.atomic(), reversion.create_revision():
new_machine.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in machine_form.changed_data))
with transaction.atomic(), reversion.create_revision():
new_interface.save()
reversion.set_user(request.user)
reversion.set_comment("Champs modifié(s) : %s" % ', '.join(field for field in interface_form.changed_data))
with transaction.atomic(), reversion.create_revision():
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))
return form({'machineform': machine_form, 'interfaceform': interface_form, 'domainform': domain_form}, 'machines/machine.html', request)
@login_required
......@@ -194,10 +190,11 @@ def new_interface(request, machineid):
return redirect("/users/profil/" + str(request.user.id))
interface_form = AddInterfaceForm(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)
new_interface.machine = machine
if interface_form.is_valid():
new_interface = interface_form.save(commit=False)
domain_form.instance.interface_parent = new_interface
new_interface.machine = machine
if domain_form.is_valid():
new_domain = domain_form.save(commit=False)
with transaction.atomic(), reversion.create_revision():
new_interface.save()
......@@ -210,8 +207,6 @@ def new_interface(request, machineid):
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)
@login_required
......
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