users: Directly access id of foreign keys instead of full object.

We used to access the complete objects for UserProfile foreign
keys like "bot_owner" and "default_sending_stream", where we only
needed ID of them.

This commit fixes some of such instances and now we directly get
the id using "bot_owner_id" and "default_sending_stream_id" so
that we can avoid the unnecessary complexity of accessing the
complete object.
This commit is contained in:
Sahil Batra
2023-07-17 21:49:01 +05:30
committed by Tim Abbott
parent d0fe378075
commit c11cf8eb54
4 changed files with 6 additions and 6 deletions

View File

@@ -379,8 +379,8 @@ def created_bot_event(user_profile: UserProfile) -> Dict[str, Any]:
# Set the owner key only when the bot has an owner.
# The default bots don't have an owner. So don't
# set the owner key while reactivating them.
if user_profile.bot_owner is not None:
bot["owner_id"] = user_profile.bot_owner.id
if user_profile.bot_owner_id is not None:
bot["owner_id"] = user_profile.bot_owner_id
return dict(type="realm_bot", op="add", bot=bot)

View File

@@ -118,9 +118,9 @@ class Addressee:
# This is a hack to deal with the fact that we still support
# default streams (and the None will be converted later in the
# call path).
if sender.default_sending_stream:
if sender.default_sending_stream_id:
# Use the user's default stream
stream_name_or_id = sender.default_sending_stream.id
stream_name_or_id = sender.default_sending_stream_id
else:
raise JsonableError(_("Missing stream"))

View File

@@ -1990,7 +1990,7 @@ class UserProfile(AbstractBaseUser, PermissionsMixin, UserBaseSettings): # type
def can_admin_user(self, target_user: "UserProfile") -> bool:
"""Returns whether this user has permission to modify target_user"""
if target_user.bot_owner == self:
if target_user.bot_owner_id == self.id:
return True
elif self.is_realm_admin and self.realm == target_user.realm:
return True

View File

@@ -140,7 +140,7 @@ def user_directly_controls_user(user_profile: UserProfile, target: UserProfile)
owned by the current user"""
if user_profile == target:
return True
if target.is_bot and target.bot_owner == user_profile:
if target.is_bot and target.bot_owner_id == user_profile.id:
return True
return False