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
|
2017-01-04 05:30:48 +01: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',
|
2016-11-16 21:16:02 +01:00
|
|
|
dest='string_id',
|
2015-08-21 02:10:41 +02:00
|
|
|
type=str,
|
|
|
|
required=True,
|
2016-11-16 21:16:02 +01:00
|
|
|
help='The subdomain or string_id of the realm.')
|
2015-08-21 02:10:41 +02:00
|
|
|
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
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(options["string_id"])
|
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)
|
|
|
|
|
2016-11-10 01:18:29 +01:00
|
|
|
alias = options['alias'].lower()
|
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:
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "realm_alias")
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(1)
|