Files
zulip/zerver/management/commands/deactivate_realm.py
Lauryn Menard 673a01ea0c realm-deactivation: Send email to owners as part of deactivation.
Creates a new "realm_deactivated" email that can be sent to realm
owners as part of `do_deactivate_realm`, via a boolean flag,
`email_owners`.

This flag is set to `False` when `do_deactivate_realm` is used for
realm exports or changing a realm's subdomain, so that the active
organization owners are not emailed in those cases.

This flag is optional for the `deactivate_realm` management command,
but as there is no active user passed in that case, then the email
is sent without referencing who deactivated the realm.

It is passed as `True` for the support analytics view, but the email
that is generated does not include information about the support
admin user who completed the request for organization deactivation.

When an active organization owner deactivates the organization, then
the flag is `True` and an email is sent to them as well as any other
active organization owners, with a slight variation in the email text
for those two cases.

Adds specific tests for when `email_owners` is passed as `True`. All
existing tests for other functionality of `do_deactivate_user` pass
the flag as `False`.

Adds `localize` from django.util.formats as a jinja env filter so
that the dates in these emails are internationlized for the owner's
default language setting in the "realm_deactivated" email templates.

Fixes #24685.
2024-06-26 16:48:18 -07:00

55 lines
1.8 KiB
Python

from argparse import ArgumentParser
from typing import Any, cast
from typing_extensions import override
from zerver.actions.realm_settings import do_add_deactivated_redirect, do_deactivate_realm
from zerver.lib.management import ZulipBaseCommand
class Command(ZulipBaseCommand):
help = """Script to deactivate a realm."""
@override
def add_arguments(self, parser: ArgumentParser) -> None:
parser.add_argument(
"--redirect_url", metavar="<redirect_url>", help="URL to which the realm has moved"
)
parser.add_argument(
"--deactivation_reason",
metavar="<deactivation_reason>",
help="Reason for deactivation",
required=True,
)
parser.add_argument(
"--email_owners",
action="store_true",
help="Whether to email organization owners about realm deactivation",
)
self.add_realm_args(parser, required=True)
@override
def handle(self, *args: Any, **options: Any) -> None:
realm = self.get_realm(options)
deactivation_reason = options["deactivation_reason"]
assert realm is not None # Should be ensured by parser
if options["redirect_url"]:
print("Setting the redirect URL to", options["redirect_url"])
do_add_deactivated_redirect(realm, options["redirect_url"])
if realm.deactivated:
print("The realm", options["realm_id"], "is already deactivated.")
return
send_realm_deactivation_email = options["email_owners"]
print("Deactivating", options["realm_id"])
do_deactivate_realm(
realm,
acting_user=None,
deactivation_reason=cast(Any, deactivation_reason),
email_owners=send_realm_deactivation_email,
)
print("Done!")