2013-11-07 20:17:54 +01:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-11-07 20:17:54 +01:00
|
|
|
|
2016-06-04 16:52:18 +02:00
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
from argparse import ArgumentParser
|
2013-11-07 20:17:54 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2016-10-29 04:58:44 +02:00
|
|
|
from zerver.models import Realm, RealmAlias, get_realm, can_add_alias
|
2013-11-07 20:17:54 +01:00
|
|
|
from zerver.lib.actions import realm_aliases
|
|
|
|
import sys
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
2015-08-21 02:10:41 +02:00
|
|
|
help = """Manage aliases for the specified realm"""
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (ArgumentParser) -> None
|
2015-08-21 02:10:41 +02:00
|
|
|
parser.add_argument('-r', '--realm',
|
|
|
|
dest='domain',
|
|
|
|
type=str,
|
|
|
|
required=True,
|
|
|
|
help='The name of the realm.')
|
|
|
|
parser.add_argument('--op',
|
|
|
|
dest='op',
|
|
|
|
type=str,
|
|
|
|
default="show",
|
|
|
|
help='What operation to do (add, show, remove).')
|
|
|
|
parser.add_argument('alias', metavar='<alias>', type=str, nargs='?',
|
|
|
|
help="alias to add or remove")
|
2013-11-07 20:17:54 +01:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **str) -> None
|
2015-10-13 22:54:35 +02:00
|
|
|
realm = get_realm(options["domain"])
|
2013-11-07 20:17:54 +01:00
|
|
|
if options["op"] == "show":
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Aliases for %s:" % (realm.domain,))
|
2013-11-07 20:17:54 +01:00
|
|
|
for alias in realm_aliases(realm):
|
2015-11-01 17:11:06 +01:00
|
|
|
print(alias)
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2015-08-21 02:10:41 +02:00
|
|
|
alias = options['alias']
|
2013-11-07 20:17:54 +01:00
|
|
|
if options["op"] == "add":
|
2016-10-29 04:58:44 +02:00
|
|
|
if not can_add_alias(alias):
|
2015-11-01 17:11:06 +01:00
|
|
|
print("A Realm already exists for this domain, cannot add it as an alias for another realm!")
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(1)
|
|
|
|
RealmAlias.objects.create(realm=realm, domain=alias)
|
|
|
|
sys.exit(0)
|
|
|
|
elif options["op"] == "remove":
|
|
|
|
RealmAlias.objects.get(realm=realm, domain=alias).delete()
|
|
|
|
sys.exit(0)
|
|
|
|
else:
|
|
|
|
self.print_help("python manage.py", "realm_alias")
|
|
|
|
sys.exit(1)
|