From a9429935ea614e06fa99a82dd63ca3873408f052 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABl=20Kervella?= Date: Fri, 29 Sep 2017 08:40:41 +0000 Subject: [PATCH] Fonction pour proposer un nom non-existant pour une nouvelle interface --- machines/forms.py | 6 +++--- machines/views.py | 2 +- users/models.py | 21 +++++++++++++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/machines/forms.py b/machines/forms.py index cd4ac87d..f1a9566d 100644 --- a/machines/forms.py +++ b/machines/forms.py @@ -107,11 +107,11 @@ class DomainForm(AliasForm): fields = ['name'] def __init__(self, *args, **kwargs): - if 'name_user' in kwargs: - name_user = kwargs.pop('name_user') + if 'user' in kwargs: + user = kwargs.pop('user') nb_machine = kwargs.pop('nb_machine') initial = kwargs.get('initial', {}) - initial['name'] = name_user.lower()+str(nb_machine) + initial['name'] = user.get_next_domain_name() kwargs['initial'] = initial super(DomainForm, self).__init__(*args, **kwargs) diff --git a/machines/views.py b/machines/views.py index 4268519b..9fc64236 100644 --- a/machines/views.py +++ b/machines/views.py @@ -93,7 +93,7 @@ def new_machine(request, userid): machine = NewMachineForm(request.POST or None) 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.pseudo.replace('_','-'), nb_machine=nb_machine) + domain = DomainForm(request.POST or None, user=user, nb_machine=nb_machine) if machine.is_valid() and interface.is_valid(): new_machine = machine.save(commit=False) new_machine.user = user diff --git a/users/models.py b/users/models.py index a69493ae..61c39aad 100644 --- a/users/models.py +++ b/users/models.py @@ -50,7 +50,7 @@ from django.contrib.auth.models import AbstractBaseUser, BaseUserManager from django.core.validators import MinLengthValidator from topologie.models import Room from cotisations.models import Cotisation, Facture, Paiement, Vente -from machines.models import Domain, Interface, MachineType, Machine, Nas, MachineType, regen +from machines.models import Domain, Interface, MachineType, Machine, Nas, MachineType, Extension, regen from preferences.models import GeneralOption, AssoOption, OptionalUser, OptionalMachine, MailMessageOption now = timezone.now() @@ -473,7 +473,7 @@ class User(AbstractBaseUser): interface_cible.clean() machine_parent.clean() domain = Domain() - domain.name = self.pseudo.replace('_','-').lower() + str(all_machines.count()) + domain.name = self.get_next_domain_name() domain.interface_parent = interface_cible domain.clean() machine_parent.save() @@ -494,6 +494,23 @@ class User(AbstractBaseUser): self.pwd_ntlm = hashNT(password) return + def get_next_domain_name(self): + """Look for an available name for a new interface for + this user by trying "pseudo0", "pseudo1", "pseudo2", ... + """ + + def simple_pseudo(): + return self.pseudo.replace('_', '-').lower() + + def composed_pseudo( n ): + return simple_pseudo() + str(n) + + num = 0 + while Domain.objects.filter(name=composed_pseudo(num)) : + num += 1 + return composed_pseudo(num) + + def __str__(self): return self.pseudo