2017-11-16 00:43:27 +01:00
|
|
|
from argparse import ArgumentParser
|
2024-01-04 16:20:46 +01:00
|
|
|
from typing import Any
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2021-07-16 22:11:10 +02:00
|
|
|
from django.core.management.base import CommandError
|
2024-01-04 16:20:46 +01:00
|
|
|
from django.db.models import QuerySet
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2024-09-23 20:24:45 +02:00
|
|
|
from zerver.actions.users import do_send_password_reset_email
|
2021-07-16 22:11:10 +02:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import UserProfile
|
2017-06-06 05:53:04 +02:00
|
|
|
|
2020-01-14 21:59:46 +01:00
|
|
|
|
2017-07-10 21:42:29 +02:00
|
|
|
class Command(ZulipBaseCommand):
|
2016-08-14 07:27:40 +02:00
|
|
|
help = """Send email to specified email address."""
|
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2024-01-04 16:23:25 +01:00
|
|
|
parser.add_argument(
|
|
|
|
"--only-never-logged-in",
|
|
|
|
action="store_true",
|
|
|
|
help="Filter to only users which have not accepted the TOS.",
|
|
|
|
)
|
2021-02-12 08:19:30 +01:00
|
|
|
parser.add_argument(
|
2021-02-12 08:20:45 +01:00
|
|
|
"--entire-server", action="store_true", help="Send to every user on the server. "
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
|
|
|
self.add_user_list_args(
|
|
|
|
parser,
|
|
|
|
help="Email addresses of user(s) to send password reset emails to.",
|
|
|
|
all_users_help="Send to every user on the realm.",
|
|
|
|
)
|
2017-07-10 21:42:29 +02:00
|
|
|
self.add_realm_args(parser)
|
2016-08-14 07:27:40 +02:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
2017-08-24 23:41:45 +02:00
|
|
|
if options["entire_server"]:
|
2024-01-04 16:20:46 +01:00
|
|
|
users: QuerySet[UserProfile] = UserProfile.objects.filter(
|
2022-06-23 20:07:19 +02:00
|
|
|
is_active=True, is_bot=False, is_mirror_dummy=False
|
|
|
|
)
|
2017-08-24 23:41:45 +02:00
|
|
|
else:
|
|
|
|
realm = self.get_realm(options)
|
2017-08-25 01:26:19 +02:00
|
|
|
try:
|
2019-08-14 19:48:54 +02:00
|
|
|
users = self.get_users(options, realm, is_bot=False)
|
2017-08-25 01:26:19 +02:00
|
|
|
except CommandError as error:
|
|
|
|
if str(error) == "You have to pass either -u/--users or -a/--all-users.":
|
2021-02-12 08:19:30 +01:00
|
|
|
raise CommandError(
|
|
|
|
"You have to pass -u/--users or -a/--all-users or --entire-server."
|
|
|
|
)
|
2017-08-25 01:26:19 +02:00
|
|
|
raise error
|
2024-01-04 16:23:25 +01:00
|
|
|
if options["only_never_logged_in"]:
|
|
|
|
users = users.filter(tos_version=-1)
|
|
|
|
|
|
|
|
if not users.exists():
|
|
|
|
print("No matching users!")
|
2017-08-24 23:41:45 +02:00
|
|
|
|
2016-08-14 07:27:40 +02:00
|
|
|
self.send(users)
|
|
|
|
|
2024-01-04 16:20:46 +01:00
|
|
|
def send(self, users: QuerySet[UserProfile]) -> None:
|
2021-02-12 08:19:30 +01:00
|
|
|
"""Sends one-use only links for resetting password to target users"""
|
2016-08-14 07:27:40 +02:00
|
|
|
for user_profile in users:
|
2024-09-23 20:24:45 +02:00
|
|
|
do_send_password_reset_email(
|
|
|
|
user_profile.delivery_email, user_profile.realm, user_profile
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|