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.

send_custom_email: Stop lying about the type of options.

Alex Vandiver1229e38a0d9d
diff --git a/zerver/lib/send_email.py b/zerver/lib/send_email.py
index 5b39cd20daf..b20ff8e65b8 100644
--- a/zerver/lib/send_email.py
+++ b/zerver/lib/send_email.py
@@ -511,7 +511,9 @@ def send_custom_email(
users: QuerySet[UserProfile],
*,
target_emails: Sequence[str] = [],
- options: Dict[str, Any],
+ dry_run: bool,
+ options: Dict[str, str],
+ admins_only: bool = False,
add_context: Optional[Callable[[Dict[str, object], UserProfile], None]] = None,
) -> None:
"""
@@ -562,7 +564,7 @@ def send_custom_email(
# Finally, we send the actual emails.
for user_profile in users.select_related("realm").order_by("id"):
- if options.get("admins_only") and not user_profile.is_realm_admin:
+ if admins_only and not user_profile.is_realm_admin:
continue
context: Dict[str, object] = {
"realm": user_profile.realm,
@@ -582,10 +584,10 @@ def send_custom_email(
options.get("from_name"), parsed_email_template.get("from"), "from_name"
),
context=context,
- dry_run=options["dry_run"],
+ dry_run=dry_run,
)
- if options["dry_run"]:
+ if dry_run:
break
# Now send emails to any recipients without a user account.
@@ -600,10 +602,10 @@ def send_custom_email(
options.get("from_name"), parsed_email_template.get("from"), "from_name"
),
context={"remote_server_email": True},
- dry_run=options["dry_run"],
+ dry_run=dry_run,
)
- if options["dry_run"]:
+ if dry_run:
break
diff --git a/zerver/management/commands/send_custom_email.py b/zerver/management/commands/send_custom_email.py
index 0303f4569c5..a27f311d944 100644
--- a/zerver/management/commands/send_custom_email.py
+++ b/zerver/management/commands/send_custom_email.py
@@ -86,7 +86,9 @@ class Command(ZulipBaseCommand):
)
@override
- def handle(self, *args: Any, **options: str) -> None:
+ def handle(
+ self, *args: Any, dry_run: bool = False, admins_only: bool = False, **options: str
+ ) -> None:
target_emails: List[str] = []
users: QuerySet[UserProfile] = UserProfile.objects.none()
add_context: Optional[Callable[[Dict[str, object], UserProfile], None]] = None
@@ -156,10 +158,15 @@ class Command(ZulipBaseCommand):
Q(tos_version=None) | Q(tos_version=UserProfile.TOS_VERSION_BEFORE_FIRST_LOGIN)
)
send_custom_email(
- users, target_emails=target_emails, options=options, add_context=add_context
+ users,
+ target_emails=target_emails,
+ dry_run=dry_run,
+ admins_only=admins_only,
+ options=options,
+ add_context=add_context,
)
- if options["dry_run"]:
+ if dry_run:
print("Would send the above email to:")
for user in users:
print(f" {user.delivery_email} ({user.realm.string_id})")
diff --git a/zerver/tests/test_email_notifications.py b/zerver/tests/test_email_notifications.py
index bcf3ace6d6d..47fed107a74 100644
--- a/zerver/tests/test_email_notifications.py
+++ b/zerver/tests/test_email_notifications.py
@@ -34,12 +34,12 @@ class TestCustomEmails(ZulipTestCase):
markdown_template.flush()
send_custom_email(
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template.name,
"reply_to": reply_to,
"subject": email_subject,
"from_name": from_name,
- "dry_run": False,
},
)
self.assert_length(mail.outbox, 1)
@@ -66,12 +66,12 @@ class TestCustomEmails(ZulipTestCase):
send_custom_email(
UserProfile.objects.none(),
target_emails=[contact_email],
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
"reply_to": reply_to,
"subject": email_subject,
"from_name": from_name,
- "dry_run": False,
},
)
self.assert_length(mail.outbox, 1)
@@ -95,9 +95,9 @@ class TestCustomEmails(ZulipTestCase):
)
send_custom_email(
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
- "dry_run": False,
},
)
self.assert_length(mail.outbox, 1)
@@ -113,9 +113,9 @@ class TestCustomEmails(ZulipTestCase):
)
send_custom_email(
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
- "dry_run": False,
},
)
self.assert_length(mail.outbox, 1)
@@ -136,9 +136,9 @@ class TestCustomEmails(ZulipTestCase):
send_custom_email(
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
- "dry_run": False,
},
add_context=add_context,
)
@@ -162,10 +162,10 @@ class TestCustomEmails(ZulipTestCase):
NoEmailArgumentError,
send_custom_email,
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
"from_name": from_name,
- "dry_run": False,
},
)
@@ -173,10 +173,10 @@ class TestCustomEmails(ZulipTestCase):
NoEmailArgumentError,
send_custom_email,
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
"subject": email_subject,
- "dry_run": False,
},
)
@@ -194,10 +194,10 @@ class TestCustomEmails(ZulipTestCase):
DoubledEmailArgumentError,
send_custom_email,
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
"subject": email_subject,
- "dry_run": False,
},
)
@@ -205,10 +205,10 @@ class TestCustomEmails(ZulipTestCase):
DoubledEmailArgumentError,
send_custom_email,
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=False,
options={
"markdown_template_path": markdown_template_path,
"from_name": from_name,
- "dry_run": False,
},
)
@@ -223,10 +223,10 @@ class TestCustomEmails(ZulipTestCase):
)
send_custom_email(
UserProfile.objects.filter(id__in=(admin_user.id, non_admin_user.id)),
+ dry_run=False,
+ admins_only=True,
options={
"markdown_template_path": markdown_template_path,
- "admins_only": True,
- "dry_run": False,
},
)
self.assert_length(mail.outbox, 1)
@@ -241,12 +241,12 @@ class TestCustomEmails(ZulipTestCase):
with patch("builtins.print") as _:
send_custom_email(
UserProfile.objects.filter(id=hamlet.id),
+ dry_run=True,
options={
"markdown_template_path": markdown_template_path,
"reply_to": reply_to,
"subject": email_subject,
"from_name": from_name,
- "dry_run": True,
},
)
self.assert_length(mail.outbox, 0)