management: Pass realm as arg to select_related in get_user.

This commit updates the select_related calls in queries to
get UserProfile objects in get_user function called in
management commands to pass "realm" as argument to
select_related call.

There are some management commands like deactivate_user,
change_full_name, etc. which might need fields like
"default_sending_stream" when changing full name of a bot
or something similar, but we don't think that would happen
often and we can afford to have a DB round trip to get
these fields if needed.

Also, note that "realm" is the only non-null foreign key
field in UserProfile object, so select_related() was only
fetching realm object previously as well. But we should
still pass "realm" as argument in select_related call so
that we can make sure that only required fields are
selected in case we add more foreign keys to UserProfile
in future.
This commit is contained in:
Sahil Batra
2023-07-13 20:29:30 +05:30
committed by Tim Abbott
parent 63cc18a94b
commit ce89cab667

View File

@@ -146,7 +146,7 @@ server via `ps -ef` or reading bash history. Prefer
# throw an error if they don't exist.
if realm is not None:
try:
return UserProfile.objects.select_related().get(
return UserProfile.objects.select_related("realm").get(
delivery_email__iexact=email.strip(), realm=realm
)
except UserProfile.DoesNotExist:
@@ -158,7 +158,9 @@ server via `ps -ef` or reading bash history. Prefer
# optimistically try to see if there is exactly one user with
# that email; if so, we'll return it.
try:
return UserProfile.objects.select_related().get(delivery_email__iexact=email.strip())
return UserProfile.objects.select_related("realm").get(
delivery_email__iexact=email.strip()
)
except MultipleObjectsReturned:
raise CommandError(
"This Zulip server contains multiple users with that email (in different realms);"