Login

Username genrator

Author:
gustavo80br
Posted:
April 24, 2008
Language:
Python
Version:
.96
Tags:
auth username name
Score:
2 (after 2 ratings)

This function generate an username based on the user's full name. First it tries to use the first name first letter plus the last name, second it tires the first name plus the last name first latter, and at last it tries to use the first name with a sequential number at the end.

The username generated are all lowercase and ASCII only characters.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import unicodedata
from django.contrib.auth.models import User


def generate_username(full_name):
    
    """
    >>> from utils import generate_username
    >>> from django.contrib.auth.models import User
    >>>
    >>> name = 'Sebastião Henrique de Almeida Gonçalves'
    >>> username = generate_username(name)
    >>> print username
    sgoncalves
    >>> u = User(username=username, password="123456", email="teste1@fundacaoaprender.org.br")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiaog
    >>> u = User(username=username, password="123456", email="teste2@fundacaoaprender.org.br")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiao1
    >>> u = User(username=username, password="123456", email="teste3@fundacaoaprender.org.br")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiao2
    >>> u = User(username=username, password="123456", email="teste4@fundacaoaprender.org.br")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiao3
    >>> u = User(username=username, password="123456", email="teste5@fundacaoaprender.org.br")
    >>> u.save()
    """
        
    name = unicodedata.normalize('NFKD', unicode(full_name.lower())).encode('ASCII', 'ignore')
    name = name.split(' ')
    lastname = name[-1]
    firstname = name[0]
    
    # tente iniciais dos primeiros nomes mais ultimo nome inteiro
    username = '%s%s' % (firstname[0], lastname)
    if User.objects.filter(username=username).count() > 0:
        # se não servir isso, tente primeiro nome inteiro mais iniciais dos ultimos nomes
        username = '%s%s' % (firstname, lastname[0])
        if User.objects.filter(username=username).count() > 0:
            # se não servir, coloque o primeiro nome, mais um número
            users = User.objects.filter(username__regex=r'^%s[1-9]{1,}$' % firstname).order_by('username').values('username')                
            if len(users) > 0:
                last_number_used = map(lambda x: int(x['username'].replace(firstname,'')), users)
                last_number_used.sort()
                last_number_used = last_number_used[-1]
                number = last_number_used + 1
                username = '%s%s' % (firstname, number)
            else:
                username = '%s%s' % (firstname, 1)
    
    return username

More like this

  1. Sort Table Headers by insin 7 years, 11 months ago
  2. very archive template by stuntgoat 5 years, 8 months ago
  3. very archive view by stuntgoat 5 years, 8 months ago
  4. Protect anti robots template tag by marinho 7 years, 1 month ago
  5. Trim the center of a string by grillermo 2 years, 7 months ago

Comments

Please login first before commenting.