models: Pass realm as arg to select_related in get_system_bot.

This commit updates the select_related calls in queries to
get UserProfile object in get_syste_bot function pass "realm"
as argument to select_related call.

The "get_system_bot" call function is mostly used to get cross
realm bot which are used as senders to send messages.
The fields like default_events_register_stream and recipient
are not required for these cases. The bot_owner field is used
to check access to a stream to send message but the cross-realm
bots are handled differently and the bot_owner check is not
required.

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-14 13:44:35 +05:30 committed by Tim Abbott
parent ce89cab667
commit 71c66cd75c
1 changed files with 1 additions and 1 deletions

View File

@ -3928,7 +3928,7 @@ def get_system_bot(email: str, realm_id: int) -> UserProfile:
the same realm as the one *to* which the message will be sent should be used - because the same realm as the one *to* which the message will be sent should be used - because
cross-realm messages will be eliminated as part of the migration. cross-realm messages will be eliminated as part of the migration.
""" """
return UserProfile.objects.select_related().get(email__iexact=email.strip()) return UserProfile.objects.select_related("realm").get(email__iexact=email.strip())
def get_user_by_id_in_realm_including_cross_realm( def get_user_by_id_in_realm_including_cross_realm(