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
|
2017-01-21 08:27:36 +01:00
|
|
|
from django.core.exceptions import ValidationError
|
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
|
2017-01-17 02:31:47 +01:00
|
|
|
from zerver.lib.actions import get_realm_aliases
|
2017-01-21 08:27:36 +01:00
|
|
|
from zerver.lib.domains import validate_domain
|
2013-11-07 20:17:54 +01:00
|
|
|
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,))
|
2017-01-17 02:31:47 +01:00
|
|
|
for alias in get_realm_aliases(realm):
|
2016-12-26 19:19:02 +01:00
|
|
|
print(alias["domain"])
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(0)
|
|
|
|
|
2017-01-21 08:27:36 +01:00
|
|
|
domain = options['alias'].strip().lower()
|
|
|
|
try:
|
|
|
|
validate_domain(domain)
|
|
|
|
except ValidationError as e:
|
|
|
|
print(e.messages[0])
|
|
|
|
sys.exit(1)
|
2013-11-07 20:17:54 +01:00
|
|
|
if options["op"] == "add":
|
2016-12-26 19:19:02 +01:00
|
|
|
if not can_add_alias(domain):
|
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)
|
2016-12-26 19:19:02 +01:00
|
|
|
RealmAlias.objects.create(realm=realm, domain=domain)
|
2013-11-07 20:17:54 +01:00
|
|
|
sys.exit(0)
|
|
|
|
elif options["op"] == "remove":
|
2016-12-26 19:19:02 +01:00
|
|
|
RealmAlias.objects.get(realm=realm, domain=domain).delete()
|
2013-11-07 20:17:54 +01:00
|
|
|
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)
|