From 85703c407fe68d753e439eed77556571a7dd5138 Mon Sep 17 00:00:00 2001 From: Gabriel Detraz Date: Tue, 1 Aug 2017 06:57:56 +0200 Subject: [PATCH] =?UTF-8?q?Modification=20du=20syst=C3=A8me=20de=20gestion?= =?UTF-8?q?=20des=20nom=20de=20machines?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- machines/models.py | 20 ++++++++----- machines/serializers.py | 2 +- machines/views.py | 63 +++++++++++++++++++---------------------- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/machines/models.py b/machines/models.py index ec7563c9..241e3a14 100644 --- a/machines/models.py +++ b/machines/models.py @@ -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" diff --git a/machines/serializers.py b/machines/serializers.py index edbe46a5..4288c0d7 100644 --- a/machines/serializers.py +++ b/machines/serializers.py @@ -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) diff --git a/machines/views.py b/machines/views.py index 4ab2460c..6e0eb46b 100644 --- a/machines/views.py +++ b/machines/views.py @@ -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