def calculate_username(email):
 email = email.split('@')[0]
 username = re.sub('[^\\w.@+-]', '-', email)
 username = username[:settings.USERNAME_MAX_LENGTH]
 suggested_username = username
 count = 0
 while User.objects.filter(username=suggested_username).exists():
  count += 1
  suggested_username = ('%s%d' % (username, count))
  if (len(suggested_username) > settings.USERNAME_MAX_LENGTH):
   return base64.urlsafe_b64encode(hashlib.sha1(email).digest()).rstrip('=')
 return suggested_username

def calculate_username ( email ) : email = email . split ( '@' ) [ 0 ] username = re . sub ( '[^\\w.@+-]' , '-' , email ) username = username [ : settings . USERNAME_MAX_LENGTH ] suggested_username = username count = 0 while User . objects . filter ( username = suggested_username ) . exists ( ) : count += 1 suggested_username = ( '%s%d' % ( username , count ) ) if ( len ( suggested_username ) > settings . USERNAME_MAX_LENGTH ) : return base64 . urlsafe_b64encode ( hashlib . sha1 ( email ) . digest ( ) ) . rstrip ( '=' ) return suggested_username
calculate username from email address .