From e7f5da83ac6ebff16abfe03d20f4732f6ff602e6 Mon Sep 17 00:00:00 2001 From: Vishnu Ks Date: Fri, 4 Aug 2017 17:10:44 +0000 Subject: [PATCH] management: Create list realms command. Fixes #5917. --- zerver/lib/management.py | 3 +- zerver/management/commands/list_realms.py | 77 +++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 zerver/management/commands/list_realms.py diff --git a/zerver/lib/management.py b/zerver/lib/management.py index a4fffd3c8e..41ae951901 100644 --- a/zerver/lib/management.py +++ b/zerver/lib/management.py @@ -25,7 +25,8 @@ class ZulipBaseCommand(BaseCommand): dest='realm_id', required=required, type=str, - help='The numeric or string ID (subdomain) of the Zulip organization to modify.') + help='The numeric or string ID (subdomain) of the Zulip organization to modify. ' + 'You can use the command list_realms to find ID of the realms in this server.') def get_realm(self, options): # type: (Dict[str, Any]) -> Optional[Realm] diff --git a/zerver/management/commands/list_realms.py b/zerver/management/commands/list_realms.py new file mode 100644 index 0000000000..47ba1bd9cf --- /dev/null +++ b/zerver/management/commands/list_realms.py @@ -0,0 +1,77 @@ +from __future__ import absolute_import +from __future__ import print_function + +import sys + +from typing import Any +from argparse import ArgumentParser + +from zerver.models import Realm +from zerver.lib.management import ZulipBaseCommand + +class Command(ZulipBaseCommand): + help = """List realms in the server and it's configuration settings(optional). + +Usage examples: + +./manage.py list_realms +./manage.py list_realms --all""" + + def add_arguments(self, parser): + # type: (ArgumentParser) -> None + parser.add_argument("--all", + dest="all", + action="store_true", + default=False, + help="Print all the configuration settings of the realms.") + + def handle(self, *args, **options): + # type: (*Any, **Any) -> None + realms = Realm.objects.all() + + outer_format = "%-5s %-40s %-40s" + inner_format = "%-40s %s" + deactivated = False + + if not options["all"]: + print(outer_format % ("id", "string_id", "name")) + print(outer_format % ("--", "---------", "----")) + + for realm in realms: + if realm.deactivated: + print(self.style.ERROR(outer_format % (realm.id, realm.string_id, realm.name))) + deactivated = True + else: + print(outer_format % (realm.id, realm.string_id, realm.name)) + if deactivated: + print(self.style.WARNING("\nRed rows represent deactivated realms.")) + sys.exit(0) + + # The remaining code path is the --all case. + identifier_attributes = ["id", "name", "string_id"] + for realm in realms: + # Start with just all the fields on the object, which is + # hacky but doesn't require any work to maintain. + realm_dict = realm.__dict__ + # Remove a field that is confusingly useless + del realm_dict['_state'] + # Fix the one bitfield to display useful data + realm_dict['authentication_methods'] = str(realm.authentication_methods_dict()) + + for key in identifier_attributes: + if realm.deactivated: + print(self.style.ERROR(inner_format % (key, realm_dict[key]))) + deactivated = True + else: + print(inner_format % (key, realm_dict[key])) + + for key, value in sorted(realm_dict.iteritems()): + if key not in identifier_attributes: + if realm.deactivated: + print(self.style.ERROR(inner_format % (key, value))) + else: + print(inner_format % (key, value)) + print("-" * 80) + + if deactivated: + print(self.style.WARNING("\nRed is used to highlight deactivated realms."))