2017-08-04 19:10:44 +02:00
|
|
|
|
|
|
|
import sys
|
|
|
|
from argparse import ArgumentParser
|
2017-11-16 00:43:27 +01:00
|
|
|
from typing import Any
|
2017-08-04 19:10:44 +02:00
|
|
|
|
|
|
|
from zerver.lib.management import ZulipBaseCommand
|
2017-11-16 00:43:27 +01:00
|
|
|
from zerver.models import Realm
|
2017-08-04 19:10:44 +02:00
|
|
|
|
|
|
|
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"""
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def add_arguments(self, parser: ArgumentParser) -> None:
|
2017-08-04 19:10:44 +02:00
|
|
|
parser.add_argument("--all",
|
|
|
|
dest="all",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Print all the configuration settings of the realms.")
|
|
|
|
|
2017-10-26 11:35:57 +02:00
|
|
|
def handle(self, *args: Any, **options: Any) -> None:
|
2017-08-04 19:10:44 +02:00
|
|
|
realms = Realm.objects.all()
|
|
|
|
|
2018-12-28 08:18:34 +01:00
|
|
|
outer_format = "%-5s %-20s %-30s %-50s"
|
2017-08-04 19:10:44 +02:00
|
|
|
inner_format = "%-40s %s"
|
|
|
|
deactivated = False
|
|
|
|
|
|
|
|
if not options["all"]:
|
2018-12-28 08:18:34 +01:00
|
|
|
print(outer_format % ("id", "string_id", "name", "domain"))
|
|
|
|
print(outer_format % ("--", "---------", "----", "------"))
|
2017-08-04 19:10:44 +02:00
|
|
|
|
|
|
|
for realm in realms:
|
2018-12-30 19:35:39 +01:00
|
|
|
display_string_id = realm.string_id if realm.string_id != '' else "''"
|
2017-08-04 19:10:44 +02:00
|
|
|
if realm.deactivated:
|
2018-12-28 08:18:34 +01:00
|
|
|
print(self.style.ERROR(outer_format % (
|
|
|
|
realm.id,
|
2018-12-30 19:35:39 +01:00
|
|
|
display_string_id,
|
2018-12-28 08:18:34 +01:00
|
|
|
realm.name,
|
|
|
|
realm.uri)))
|
2017-08-04 19:10:44 +02:00
|
|
|
deactivated = True
|
|
|
|
else:
|
2018-12-30 19:35:39 +01:00
|
|
|
print(outer_format % (realm.id, display_string_id, realm.name, realm.uri))
|
2017-08-04 19:10:44 +02:00
|
|
|
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]))
|
|
|
|
|
2018-12-30 19:33:28 +01:00
|
|
|
for key, value in sorted(realm_dict.items()):
|
2017-08-04 19:10:44 +02:00
|
|
|
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."))
|