Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

models: Pass realm and bot_owner as args to select_related.

Sahil Batra3e09a219299a
diff --git a/zerver/models.py b/zerver/models.py
index 17952769914..d93dab4037f 100644
--- a/zerver/models.py
+++ b/zerver/models.py
@@ -3810,7 +3810,7 @@ class Subscription(models.Model):
@cache_with_key(user_profile_by_id_cache_key, timeout=3600 * 24 * 7)
def get_user_profile_by_id(user_profile_id: int) -> UserProfile:
- return UserProfile.objects.select_related().get(id=user_profile_id)
+ return UserProfile.objects.select_related("realm", "bot_owner").get(id=user_profile_id)
def get_user_profile_by_email(email: str) -> UserProfile:
@@ -3826,7 +3826,7 @@ def get_user_profile_by_email(email: str) -> UserProfile:
@cache_with_key(user_profile_by_api_key_cache_key, timeout=3600 * 24 * 7)
def maybe_get_user_profile_by_api_key(api_key: str) -> Optional[UserProfile]:
try:
- return UserProfile.objects.select_related().get(api_key=api_key)
+ return UserProfile.objects.select_related("realm", "bot_owner").get(api_key=api_key)
except UserProfile.DoesNotExist:
# We will cache failed lookups with None. The
# use case here is that broken API clients may
@@ -3850,7 +3850,7 @@ def get_user_by_delivery_email(email: str, realm: Realm) -> UserProfile:
EMAIL_ADDRESS_VISIBILITY_ADMINS security model. Use get_user in
those code paths.
"""
- return UserProfile.objects.select_related().get(
+ return UserProfile.objects.select_related("realm", "bot_owner").get(
delivery_email__iexact=email.strip(), realm=realm
)
@@ -3887,7 +3887,9 @@ def get_user(email: str, realm: Realm) -> UserProfile:
EMAIL_ADDRESS_VISIBILITY_ADMINS. In those code paths, use
get_user_by_delivery_email.
"""
- return UserProfile.objects.select_related().get(email__iexact=email.strip(), realm=realm)
+ return UserProfile.objects.select_related("realm", "bot_owner").get(
+ email__iexact=email.strip(), realm=realm
+ )
def get_active_user(email: str, realm: Realm) -> UserProfile:
@@ -3900,7 +3902,7 @@ def get_active_user(email: str, realm: Realm) -> UserProfile:
def get_user_profile_by_id_in_realm(uid: int, realm: Realm) -> UserProfile:
- return UserProfile.objects.select_related().get(id=uid, realm=realm)
+ return UserProfile.objects.select_related("realm", "bot_owner").get(id=uid, realm=realm)
def get_active_user_profile_by_id_in_realm(uid: int, realm: Realm) -> UserProfile:
diff --git a/zerver/tests/test_subs.py b/zerver/tests/test_subs.py
index d08806e4d05..afd3daa0ad9 100644
--- a/zerver/tests/test_subs.py
+++ b/zerver/tests/test_subs.py
@@ -2551,7 +2551,7 @@ class StreamAdminTest(ZulipTestCase):
for name in ["cordelia", "prospero", "iago", "hamlet", "outgoing_webhook_bot"]
]
result = self.attempt_unsubscribe_of_principal(
- query_count=28,
+ query_count=27,
cache_count=8,
target_users=target_users,
is_realm_admin=True,
@@ -2660,7 +2660,7 @@ class StreamAdminTest(ZulipTestCase):
webhook_bot = self.example_user("webhook_bot")
do_change_bot_owner(webhook_bot, bot_owner=user_profile, acting_user=user_profile)
result = self.attempt_unsubscribe_of_principal(
- query_count=14,
+ query_count=13,
target_users=[webhook_bot],
is_realm_admin=False,
is_subbed=True,
@@ -2674,7 +2674,7 @@ class StreamAdminTest(ZulipTestCase):
webhook_bot = self.example_user("webhook_bot")
do_change_bot_owner(webhook_bot, bot_owner=other_user, acting_user=other_user)
result = self.attempt_unsubscribe_of_principal(
- query_count=9,
+ query_count=8,
target_users=[webhook_bot],
is_realm_admin=False,
is_subbed=True,