Skip to content

Commit 82c672c

Browse files
committed
feat: add utility function to retrieve user by username or email
1 parent a42e2a9 commit 82c672c

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

openedx_authz/rest_api/utils.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"""Utility functions for the Open edX AuthZ REST API."""
2+
3+
from django.contrib.auth import get_user_model
4+
from django.db.models import Q
5+
6+
User = get_user_model()
7+
8+
9+
def get_user_by_username_or_email(username_or_email: str) -> User:
10+
"""
11+
Retrieve a user by their username or email address.
12+
13+
Args:
14+
username_or_email (str): The username or email address to search for.
15+
16+
Returns:
17+
User: The User object if found and not retired.
18+
19+
Raises:
20+
User.DoesNotExist: If no user matches the provided username or email,
21+
or if the user has an associated retirement request.
22+
"""
23+
user = User.objects.get(Q(email=username_or_email) | Q(username=username_or_email))
24+
if hasattr(user, "userretirementrequest"):
25+
raise User.DoesNotExist
26+
return user

0 commit comments

Comments
 (0)