2020-10-28 00:33:40 +01:00
|
|
|
from argparse import ArgumentParser
|
|
|
|
from typing import Any
|
|
|
|
|
2021-06-03 00:25:08 +02:00
|
|
|
from django.conf import settings
|
2020-10-28 00:33:40 +01:00
|
|
|
from django.core.management.base import CommandError
|
|
|
|
|
2023-02-28 03:35:01 +01:00
|
|
|
from zerver.actions.realm_settings import do_delete_all_realm_attachments
|
2020-10-28 00:33:40 +01:00
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
|
|
|
from zerver.models import Message, UserProfile
|
|
|
|
|
|
|
|
|
|
|
|
class Command(ZulipBaseCommand):
|
|
|
|
help = """Script to permanently delete a realm. Recommended only for removing
|
|
|
|
realms used for testing; consider using deactivate_realm instead."""
|
|
|
|
|
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2021-05-10 21:29:25 +02:00
|
|
|
self.add_realm_args(parser, required=True)
|
2020-10-28 00:33:40 +01:00
|
|
|
|
|
|
|
def handle(self, *args: Any, **options: str) -> None:
|
|
|
|
realm = self.get_realm(options)
|
|
|
|
assert realm is not None # Should be ensured by parser
|
|
|
|
|
|
|
|
user_count = UserProfile.objects.filter(
|
|
|
|
realm_id=realm.id,
|
|
|
|
is_active=True,
|
|
|
|
is_bot=False,
|
|
|
|
).count()
|
|
|
|
|
2022-10-29 21:51:17 +02:00
|
|
|
message_count = Message.objects.filter(realm=realm).count()
|
2020-10-28 00:33:40 +01:00
|
|
|
|
|
|
|
print(f"This realm has {user_count} users and {message_count} messages.\n")
|
|
|
|
|
2021-06-03 00:25:08 +02:00
|
|
|
if settings.BILLING_ENABLED:
|
|
|
|
# Deleting a Realm object also deletes associating billing
|
|
|
|
# metadata in an invariant-violating way, so we should
|
docs: Add missing space to compound verbs “back up”, “log in”, etc.
Noun: backup, login, logout, lookup, setup.
Verb: back up, log in, log out, look up, set up.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-02-07 20:41:10 +01:00
|
|
|
# never use this tool for a realm with billing set up.
|
2021-08-03 02:13:47 +02:00
|
|
|
from corporate.models import CustomerPlan, get_customer_by_realm
|
|
|
|
|
|
|
|
customer = get_customer_by_realm(realm)
|
2023-01-18 02:59:37 +01:00
|
|
|
if customer and (
|
|
|
|
customer.stripe_customer_id
|
2023-09-07 18:22:41 +02:00
|
|
|
or CustomerPlan.objects.filter(customer=customer).exists()
|
2023-01-18 02:59:37 +01:00
|
|
|
):
|
|
|
|
raise CommandError("This realm has had a billing relationship associated with it!")
|
2021-06-03 00:25:08 +02:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
print(
|
|
|
|
"This command will \033[91mPERMANENTLY DELETE\033[0m all data for this realm. "
|
|
|
|
"Most use cases will be better served by scrub_realm and/or deactivate_realm."
|
|
|
|
)
|
2020-10-28 00:33:40 +01:00
|
|
|
|
|
|
|
confirmation = input("Type the name of the realm to confirm: ")
|
|
|
|
if confirmation != realm.string_id:
|
|
|
|
raise CommandError("Aborting!")
|
|
|
|
|
2023-02-28 03:35:01 +01:00
|
|
|
# Explicitly remove the attachments and their files in backend
|
|
|
|
# storage; failing to do this leaves dangling files
|
|
|
|
do_delete_all_realm_attachments(realm)
|
|
|
|
|
2020-10-28 00:33:40 +01:00
|
|
|
# TODO: This approach leaks Recipient and Huddle objects,
|
|
|
|
# because those don't have a foreign key to the Realm or any
|
|
|
|
# other model it cascades to (Realm/Stream/UserProfile/etc.).
|
|
|
|
realm.delete()
|
|
|
|
|
|
|
|
print("Realm has been successfully permanently deleted.")
|