8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-12-01 17:12:26 +00:00

Fonction pour proposer un nom non-existant pour une nouvelle interface

This commit is contained in:
Maël Kervella 2017-09-29 08:40:41 +00:00 committed by Pierre Cadart
parent edca49dcf1
commit d8dbf46d6a
3 changed files with 23 additions and 6 deletions

View file

@ -107,11 +107,11 @@ class DomainForm(AliasForm):
fields = ['name'] fields = ['name']
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
if 'name_user' in kwargs: if 'user' in kwargs:
name_user = kwargs.pop('name_user') user = kwargs.pop('user')
nb_machine = kwargs.pop('nb_machine') nb_machine = kwargs.pop('nb_machine')
initial = kwargs.get('initial', {}) initial = kwargs.get('initial', {})
initial['name'] = name_user.lower()+str(nb_machine) initial['name'] = user.get_next_domain_name()
kwargs['initial'] = initial kwargs['initial'] = initial
super(DomainForm, self).__init__(*args, **kwargs) super(DomainForm, self).__init__(*args, **kwargs)

View file

@ -93,7 +93,7 @@ def new_machine(request, userid):
machine = NewMachineForm(request.POST or None) machine = NewMachineForm(request.POST or None)
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.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(): if machine.is_valid() and interface.is_valid():
new_machine = machine.save(commit=False) new_machine = machine.save(commit=False)
new_machine.user = user new_machine.user = user

View file

@ -50,7 +50,7 @@ from django.contrib.auth.models import AbstractBaseUser, BaseUserManager
from django.core.validators import MinLengthValidator from django.core.validators import MinLengthValidator
from topologie.models import Room from topologie.models import Room
from cotisations.models import Cotisation, Facture, Paiement, Vente 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 from preferences.models import GeneralOption, AssoOption, OptionalUser, OptionalMachine, MailMessageOption
now = timezone.now() now = timezone.now()
@ -473,7 +473,7 @@ class User(AbstractBaseUser):
interface_cible.clean() interface_cible.clean()
machine_parent.clean() machine_parent.clean()
domain = Domain() 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.interface_parent = interface_cible
domain.clean() domain.clean()
machine_parent.save() machine_parent.save()
@ -494,6 +494,23 @@ class User(AbstractBaseUser):
self.pwd_ntlm = hashNT(password) self.pwd_ntlm = hashNT(password)
return 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): def __str__(self):
return self.pseudo return self.pseudo