Login

Username genrator

Author:
gustavo80br
Posted:
April 24, 2008
Language:
Python
Version:
.96
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="[email protected]")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiaog
    >>> u = User(username=username, password="123456", email="[email protected]")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiao1
    >>> u = User(username=username, password="123456", email="[email protected]")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiao2
    >>> u = User(username=username, password="123456", email="[email protected]")
    >>> u.save()
    >>>
    >>> username = generate_username(name)
    >>> print username
    sebastiao3
    >>> u = User(username=username, password="123456", email="[email protected]")
    >>> 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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 4 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 7 months ago

Comments

Please login first before commenting.