Skip to content

Commit

Permalink
fix: update autogenerated username functionality (#35052)
Browse files Browse the repository at this point in the history
  • Loading branch information
attiyaIshaque authored Jul 5, 2024
1 parent 6e9e854 commit 57dd213
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
2 changes: 2 additions & 0 deletions openedx/core/djangoapps/user_authn/views/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ def test_generate_username_from_data(self, data, expected_initials):
({}, None),
({'first_name': '', 'last_name': ''}, None),
({'name': ''}, None),
({'name': '='}, None),
({'name': '@'}, None),
({'first_name': '阿提亚', 'last_name': '阿提亚'}, "AT"),
({'first_name': 'أحمد', 'last_name': 'محمد'}, "HM"),
({'name': 'أحمد محمد'}, "HM"),
Expand Down
27 changes: 18 additions & 9 deletions openedx/core/djangoapps/user_authn/views/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,16 +128,25 @@ def _get_username_prefix(data):
- str: Name initials or None.
"""
username_regex_partial = settings.USERNAME_REGEX_PARTIAL
valid_username_regex = r'^[A-Za-z0-9_\-]+$'
full_name = ''
if data.get('first_name', '').strip() and data.get('last_name', '').strip():
full_name = f"{unidecode(data.get('first_name', ''))} {unidecode(data.get('last_name', ''))}"
elif data.get('name', '').strip():
full_name = unidecode(data['name'])

if full_name.strip():
full_name = re.findall(username_regex_partial, full_name)[0]
name_initials = "".join([name_part[0] for name_part in full_name.split()[:2]])
return name_initials.upper() if name_initials else None
try:
if data.get('first_name', '').strip() and data.get('last_name', '').strip():
full_name = f"{unidecode(data.get('first_name', ''))} {unidecode(data.get('last_name', ''))}"
elif data.get('name', '').strip():
full_name = unidecode(data['name'])

if full_name.strip():
matched_name = re.findall(username_regex_partial, full_name)
if matched_name:
full_name = " ".join(matched_name)
name_initials = "".join([name_part[0] for name_part in full_name.split()[:2]])
if re.match(valid_username_regex, name_initials):
return name_initials.upper() if name_initials else None

except Exception as e: # pylint: disable=broad-except
logging.info(f"Error in _get_username_prefix: {e}")
return None

return None

Expand Down

0 comments on commit 57dd213

Please sign in to comment.