8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-25 00:13:45 +00:00

Modification du système de gestion des nom de machines

This commit is contained in:
Gabriel Detraz 2017-08-01 06:57:56 +02:00 committed by root
parent 2e515cced3
commit 85703c407f
3 changed files with 43 additions and 42 deletions

View file

@ -208,16 +208,19 @@ class Domain(models.Model):
cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain') cname = models.ForeignKey('self', null=True, blank=True, related_name='related_domain')
class Meta: class Meta:
unique_together = ("name", "extension") unique_together = (("name", "extension"),)
@cached_property
def get_extension(self): def get_extension(self):
if self.interface_parent: if self.interface_parent:
return self.interface_parent.type.ip_type.extension return self.interface_parent.type.ip_type.extension
else: elif hasattr(self,'extension'):
return self.extension return self.extension
else:
return None
def clean(self): 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 """ """ 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: if self.interface_parent and self.cname:
raise ValidationError("On ne peut créer à la fois A et 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) raise ValidationError("Le nom de domaine %s est trop long (maximum de 63 caractères)." % dns)
if not HOSTNAME_LABEL_PATTERN.match(dns): if not HOSTNAME_LABEL_PATTERN.match(dns):
raise ValidationError("Ce nom de domaine %s contient des carractères interdits." % 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): def save(self, *args, **kwargs):
# On verifie la cohérence en forçant l'extension par la méthode if not self.get_extension():
self.extension = self.get_extension raise ValidationError("Extension invalide")
self.full_clean()
super(Domain, self).save(*args, **kwargs) super(Domain, self).save(*args, **kwargs)
def __str__(self): def __str__(self):
return str(self.name) + str(self.get_extension) return str(self.name) + str(self.get_extension())
class IpList(models.Model): class IpList(models.Model):
PRETTY_NAME = "Addresses ipv4" PRETTY_NAME = "Addresses ipv4"

View file

@ -50,7 +50,7 @@ class InterfaceSerializer(serializers.ModelSerializer):
return obj.domain.name return obj.domain.name
def get_interface_extension(self, obj): def get_interface_extension(self, obj):
return obj.domain.get_extension.name return obj.domain.get_extension().name
def get_macaddress(self, obj): def get_macaddress(self, obj):
return str(obj.mac_address) return str(obj.mac_address)

View file

@ -91,11 +91,12 @@ def new_machine(request, userid):
interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',))) interface = AddInterfaceForm(request.POST or None, infra=request.user.has_perms(('infra',)))
nb_machine = Interface.objects.filter(machine__user=userid).count() nb_machine = Interface.objects.filter(machine__user=userid).count()
domain = DomainForm(request.POST or None, name_user=user.surname, nb_machine=nb_machine) domain = DomainForm(request.POST or None, name_user=user.surname, nb_machine=nb_machine)
try: if machine.is_valid() and interface.is_valid():
if machine.is_valid() and interface.is_valid() and domain.is_valid():
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
new_machine.user = user new_machine.user = user
new_interface = interface.save(commit=False) new_interface = interface.save(commit=False)
domain.instance.interface_parent = new_interface
if domain.is_valid():
new_domain = domain.save(commit=False) new_domain = domain.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_machine.save() new_machine.save()
@ -113,8 +114,6 @@ def new_machine(request, userid):
reversion.set_comment("Création") reversion.set_comment("Création")
messages.success(request, "La machine a été crée") messages.success(request, "La machine a été crée")
return redirect("/users/profil/" + str(user.id)) 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) return form({'machineform': machine, 'interfaceform': interface, 'domainform': domain}, 'machines/machine.html', request)
@login_required @login_required
@ -134,10 +133,9 @@ 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 = DomainForm(request.POST or None, instance=interface.domain) 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(): 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_machine = machine_form.save(commit=False)
new_interface = interface_form.save(commit=False)
new_domain = domain_form.save(commit=False) new_domain = domain_form.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_machine.save() new_machine.save()
@ -153,8 +151,6 @@ def edit_interface(request, interfaceid):
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 domain_form.changed_data))
messages.success(request, "La machine a été modifiée") messages.success(request, "La machine a été modifiée")
return redirect("/users/profil/" + str(interface.machine.user.id)) 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
@ -194,10 +190,11 @@ 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 = DomainForm(request.POST or None) domain_form = DomainForm(request.POST or None)
try: if interface_form.is_valid():
if interface_form.is_valid() and domain_form.is_valid():
new_interface = interface_form.save(commit=False) new_interface = interface_form.save(commit=False)
domain_form.instance.interface_parent = new_interface
new_interface.machine = machine new_interface.machine = machine
if domain_form.is_valid():
new_domain = domain_form.save(commit=False) new_domain = domain_form.save(commit=False)
with transaction.atomic(), reversion.create_revision(): with transaction.atomic(), reversion.create_revision():
new_interface.save() new_interface.save()
@ -210,8 +207,6 @@ def new_interface(request, machineid):
reversion.set_comment("Création") reversion.set_comment("Création")
messages.success(request, "L'interface a été ajoutée") messages.success(request, "L'interface a été ajoutée")
return redirect("/users/profil/" + str(machine.user.id)) 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