Skip to content
Snippets Groups Projects
Unverified Commit 572ec10d authored by Awais Jibran's avatar Awais Jibran Committed by GitHub
Browse files

Merge pull request #26695 from edx/aj/refactor/using-q-objects

refactor: use q objects when fetching user using an identifier
parents 370e4263 950633a9
No related branches found
Tags release-2021-02-24-08.46
No related merge requests found
......@@ -3,6 +3,7 @@
from django.contrib.auth.models import User # lint-amnesty, pylint: disable=imported-auth-user
from django.core.management.base import BaseCommand
from django.db.models import Q
class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docstring
......@@ -14,19 +15,16 @@ class Command(BaseCommand): # lint-amnesty, pylint: disable=missing-class-docst
def handle(self, *args, **options):
email_or_username = options['email_or_username']
try:
if '@' in email_or_username:
user = User.objects.get(email=email_or_username)
else:
user = User.objects.get(username=email_or_username)
except User.DoesNotExist:
user = User.objects.filter(Q(username=email_or_username) | Q(email=email_or_username)).first()
if not user:
print(f'User {email_or_username} does not exist. ')
print('Available users: ')
print(User.objects.all())
return
roles = user.roles.all()
print('{} has %d roles:'.format(user, len(roles)))
print(f'{user} has {len(roles)} roles:')
for role in roles:
print(f'\t{role}')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment