Merge pull request #28844 from edx/aehsan/VAN-702/username_suggestions_format_updated

Username suggestion added based on the full name
This commit is contained in:
Adeel Ehsan
2021-10-26 12:20:24 +05:00
committed by GitHub
2 changed files with 42 additions and 18 deletions

View File

@@ -3,6 +3,7 @@ Utility functions used during user authentication.
"""
import random
import re
from urllib.parse import urlparse # pylint: disable=import-error
from uuid import uuid4 # lint-amnesty, pylint: disable=unused-import
@@ -57,28 +58,50 @@ def is_registration_api_v1(request):
return 'v1' in request.get_full_path() and 'register' not in request.get_full_path()
def generate_username_suggestions(username):
""" Generate 3 available username suggestions """
max_length = USERNAME_MAX_LENGTH
short_username = username[:max_length - 6] if max_length is not None else username
short_username = short_username.replace('_', '').replace('-', '')
def remove_special_characters_from_name(name):
return "".join(re.findall(r"[\w-]+", name))
def generate_username_suggestions(name):
""" Generate 3 available username suggestions """
username_suggestions = []
int_ranges = [
{'min': 0, 'max': 9},
{'min': 10, 'max': 99},
{'min': 100, 'max': 999},
{'min': 1000, 'max': 99999},
]
for int_range in int_ranges:
for _ in range(10):
random_int = random.randint(int_range['min'], int_range['max'])
suggestion = f'{short_username}_{random_int}'
max_length = USERNAME_MAX_LENGTH
names = name.split(' ')
if names:
first_name = remove_special_characters_from_name(names[0].lower())
last_name = remove_special_characters_from_name(names[-1].lower())
if first_name != last_name and first_name:
# username combination of first and last name
suggestion = f'{first_name}{last_name}'[:max_length]
if not username_exists_or_retired(suggestion):
username_suggestions.append(suggestion)
break
if len(username_suggestions) == 3:
break
# username is combination of first letter of first name and last name
suggestion = f'{first_name[0]}-{last_name}'[:max_length]
if not username_exists_or_retired(suggestion):
username_suggestions.append(suggestion)
if len(first_name) >= 2:
short_username = first_name[:max_length - 6] if max_length is not None else first_name
short_username = short_username.replace('_', '').replace('-', '')
int_ranges = [
{'min': 0, 'max': 9},
{'min': 10, 'max': 99},
{'min': 100, 'max': 999},
{'min': 1000, 'max': 99999},
]
for int_range in int_ranges:
for _ in range(10):
random_int = random.randint(int_range['min'], int_range['max'])
suggestion = f'{short_username}_{random_int}'
if not username_exists_or_retired(suggestion):
username_suggestions.append(suggestion)
break
if len(username_suggestions) == 3:
break
return username_suggestions

View File

@@ -788,6 +788,7 @@ class RegistrationValidationView(APIView):
def name_handler(self, request):
""" Validates whether fullname is valid """
name = request.data.get('name')
self.username_suggestions = generate_username_suggestions(name)
return get_name_validation_error(name)
def username_handler(self, request):