send_custom_email: Stop turning every user query into an id-based set.

The set of objects in the `users` object can be very large (in some
cases, literally every object in the database) and making them into a
giant `id in (...)` to handle the one tiny corner case which we never
use is silly.

Switch the `--users` codepath to returning a QuerySet as well, so it
can be composed.  We pass a QuerySet into send_custom_email as well,
so it can ensure that the realm is `select_related` in as well, no
matter how the QuerySet was generated.
This commit is contained in:
Alex Vandiver
2023-08-03 20:20:37 +00:00
committed by Tim Abbott
parent 6b25fab38c
commit 5a32ea52ae
6 changed files with 63 additions and 39 deletions

View File

@@ -78,13 +78,12 @@ class Command(ZulipBaseCommand):
realm = self.get_realm(options)
user_profiles = self.get_users(options, realm, is_bot=False, include_deactivated=True)
else:
user_profile_query = UserProfile.objects.select_related("realm").filter(is_bot=False)
user_profiles = UserProfile.objects.select_related("realm").filter(is_bot=False)
if not user_profile_query.exists():
if not user_profiles.exists():
# This case provides a special error message if one
# tries setting up LDAP sync before creating a realm.
raise CommandError("Zulip server contains no users. Have you created a realm?")
user_profiles = list(user_profile_query)
if len(user_profiles) == 0:
# We emphasize that this error is purely about the