Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
send_custom_email: Order by delivery_email if necessary.
Alex Vandiver8d9ead0f6dac
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.pyindex 35b5c3ed28b..9cfca2df27c 100644--- a/zerver/lib/send_email.py+++ b/zerver/lib/send_email.py@@ -582,6 +582,7 @@ def send_custom_email(dry_run: bool,options: Dict[str, str],add_context: Optional[Callable[[Dict[str, object], UserProfile], None]] = None,+ distinct_email: bool = False,) -> None:"""Helper for `manage.py send_custom_email`.@@ -594,7 +595,13 @@ def send_custom_email()"""email_sender = custom_email_sender(**options, dry_run=dry_run)- for user_profile in users.select_related("realm").order_by("id"):++ users = users.select_related("realm")+ if distinct_email:+ users = users.distinct("delivery_email").order_by("delivery_email", "id")+ else:+ users = users.order_by("id")+ for user_profile in users:context: Dict[str, object] = {"realm": user_profile.realm,"realm_string_id": user_profile.realm.string_id,diff --git a/zerver/management/commands/send_custom_email.py b/zerver/management/commands/send_custom_email.pyindex 6f9e163149a..2e99d994b7c 100644--- a/zerver/management/commands/send_custom_email.py+++ b/zerver/management/commands/send_custom_email.py@@ -98,6 +98,7 @@ class Command(ZulipBaseCommand):) -> None:users: QuerySet[UserProfile] = UserProfile.objects.none()add_context: Optional[Callable[[Dict[str, object], UserProfile], None]] = None+ distinct_email = Falseif options["remote_servers"]:servers = RemoteZulipServer.objects.filter(deactivated=False)@@ -136,25 +137,22 @@ class Command(ZulipBaseCommand):# Marketing email sent at most once to each email address# for users who are recently active (!long_term_idle)# users of the product, or who are admins/owners.- users = (- UserProfile.objects.filter(- is_active=True,- is_bot=False,- is_mirror_dummy=False,- realm__deactivated=False,- enable_marketing_emails=True,- )- .filter(- Q(long_term_idle=False)- | Q(- role__in=[- UserProfile.ROLE_REALM_OWNER,- UserProfile.ROLE_REALM_ADMINISTRATOR,- ]- )+ users = UserProfile.objects.filter(+ is_active=True,+ is_bot=False,+ is_mirror_dummy=False,+ realm__deactivated=False,+ enable_marketing_emails=True,+ ).filter(+ Q(long_term_idle=False)+ | Q(+ role__in=[+ UserProfile.ROLE_REALM_OWNER,+ UserProfile.ROLE_REALM_ADMINISTRATOR,+ ])- .distinct("delivery_email"))+ distinct_email = Truedef add_marketing_unsubscribe(context: Dict[str, object], user: UserProfile) -> None:context["unsubscribe_link"] = one_click_unsubscribe_link(user, "marketing")@@ -175,7 +173,8 @@ class Command(ZulipBaseCommand):role__in=admin_roles,realm__deactivated=False,realm__in=sponsored_realms,- ).distinct("delivery_email")+ )+ distinct_email = Trueelse:realm = self.get_realm(options)users = self.get_users(options, realm, is_bot=False)@@ -203,6 +202,7 @@ class Command(ZulipBaseCommand):dry_run=dry_run,options=options,add_context=add_context,+ distinct_email=distinct_email,)if dry_run: