Ce serveur Gitlab sera éteint le 30 juin 2020, pensez à migrer vos projets vers les serveurs gitlab-research.centralesupelec.fr et gitlab-student.centralesupelec.fr !

Commit d8dbf46d authored by Maël Kervella's avatar Maël Kervella Committed by Pierre Cadart

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

parent edca49dc
......@@ -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)
......@@ -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
......@@ -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):
domain = Domain()
domain.name = self.pseudo.replace('_','-').lower() + str(all_machines.count())
domain.name = self.get_next_domain_name()
domain.interface_parent = interface_cible
......@@ -494,6 +494,23 @@ class User(AbstractBaseUser):
self.pwd_ntlm = hashNT(password)
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
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