2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-05-31 17:06:18 +02:00
|
|
|
from optparse import make_option
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2016-12-08 01:43:15 +01:00
|
|
|
from typing import Any, Text
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2013-10-25 23:24:26 +02:00
|
|
|
from django.conf import settings
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2016-10-28 21:43:47 +02:00
|
|
|
from zerver.lib.actions import Realm, do_create_realm, set_default_streams
|
2016-10-31 23:47:45 +01:00
|
|
|
from zerver.models import RealmAlias, can_add_alias, get_realm_by_string_id
|
2013-11-12 23:37:00 +01:00
|
|
|
|
2016-07-19 06:38:23 +02:00
|
|
|
if settings.ZILENCER_ENABLED:
|
2013-11-12 23:37:00 +01:00
|
|
|
from zilencer.models import Deployment
|
2012-11-21 22:31:42 +01:00
|
|
|
|
2013-10-17 17:12:15 +02:00
|
|
|
import re
|
2013-10-17 17:47:30 +02:00
|
|
|
import sys
|
2013-10-17 17:12:15 +02:00
|
|
|
|
2012-11-21 22:31:42 +01:00
|
|
|
class Command(BaseCommand):
|
2016-10-28 07:21:53 +02:00
|
|
|
help = """Create a realm.
|
2013-05-31 17:06:18 +02:00
|
|
|
|
2016-11-22 01:44:16 +01:00
|
|
|
Usage: ./manage.py create_realm --string_id=acme --name='Acme'"""
|
2013-05-31 17:06:18 +02:00
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
|
|
|
# type: (CommandParser) -> None
|
|
|
|
parser.add_argument('-d', '--domain',
|
|
|
|
dest='domain',
|
|
|
|
type=str,
|
|
|
|
help='The domain for the realm.')
|
|
|
|
|
|
|
|
parser.add_argument('-s', '--string_id',
|
|
|
|
dest='string_id',
|
|
|
|
type=str,
|
|
|
|
help="A short name for the realm. If this "
|
|
|
|
"installation uses subdomains, this will be "
|
|
|
|
"used as the realm's subdomain.")
|
|
|
|
|
|
|
|
parser.add_argument('-n', '--name',
|
|
|
|
dest='name',
|
|
|
|
type=str,
|
|
|
|
help='The user-visible name for the realm.')
|
|
|
|
|
|
|
|
parser.add_argument('--corporate',
|
|
|
|
dest='org_type',
|
|
|
|
action="store_const",
|
|
|
|
const=Realm.CORPORATE,
|
|
|
|
help='Is a corporate org_type')
|
|
|
|
|
|
|
|
parser.add_argument('--community',
|
|
|
|
dest='org_type',
|
|
|
|
action="store_const",
|
|
|
|
const=Realm.COMMUNITY,
|
|
|
|
default=None,
|
|
|
|
help='Is a community org_type. Is the default.')
|
|
|
|
|
|
|
|
parser.add_argument('--deployment',
|
|
|
|
dest='deployment_id',
|
|
|
|
type=int,
|
|
|
|
default=None,
|
|
|
|
help='Optionally, the ID of the deployment you '
|
|
|
|
'want to associate the realm with.')
|
2012-11-21 22:31:42 +01:00
|
|
|
|
2013-10-17 17:12:15 +02:00
|
|
|
def validate_domain(self, domain):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (str) -> None
|
2013-10-17 17:12:15 +02:00
|
|
|
# Domains can't contain whitespace if they are to be used in memcached
|
2016-03-31 03:39:51 +02:00
|
|
|
# keys. Seems safer to leave that as the default case regardless of
|
|
|
|
# which backing store we use.
|
2013-10-17 17:12:15 +02:00
|
|
|
if re.search("\s", domain):
|
|
|
|
raise ValueError("Domains can't contain whitespace")
|
|
|
|
|
|
|
|
# Domains must look like domains, ie have the structure of
|
|
|
|
# <subdomain(s)>.<tld>. One reason for this is that bots need
|
|
|
|
# to have valid looking emails.
|
|
|
|
if len(domain.split(".")) < 2:
|
|
|
|
raise ValueError("Domains must contain a '.'")
|
|
|
|
|
2016-10-29 04:58:44 +02:00
|
|
|
if not can_add_alias(domain):
|
|
|
|
raise ValueError("Domain already assigned to an existing realm")
|
2013-11-13 19:53:38 +01:00
|
|
|
|
2012-11-21 22:31:42 +01:00
|
|
|
def handle(self, *args, **options):
|
2016-06-04 16:52:18 +02:00
|
|
|
# type: (*Any, **Any) -> None
|
2016-11-28 23:29:01 +01:00
|
|
|
string_id = options["string_id"]
|
2016-10-28 07:21:53 +02:00
|
|
|
name = options["name"]
|
|
|
|
domain = options["domain"]
|
2016-09-16 19:05:14 +02:00
|
|
|
|
2016-10-31 23:47:45 +01:00
|
|
|
if not name or not string_id:
|
2016-10-28 07:21:53 +02:00
|
|
|
print("\033[1;31mPlease provide a name and string_id.\033[0m\n", file=sys.stderr)
|
2016-11-22 01:44:16 +01:00
|
|
|
self.print_help("./manage.py", "create_realm")
|
2013-05-31 17:06:18 +02:00
|
|
|
exit(1)
|
2012-11-21 22:31:42 +01:00
|
|
|
|
2016-07-19 06:38:23 +02:00
|
|
|
if options["deployment_id"] is not None and not settings.ZILENCER_ENABLED:
|
2015-11-01 17:11:06 +01:00
|
|
|
print("\033[1;31mExternal deployments are not supported on voyager deployments.\033[0m\n", file=sys.stderr)
|
2013-10-25 23:24:26 +02:00
|
|
|
exit(1)
|
|
|
|
|
2016-10-28 07:21:53 +02:00
|
|
|
if domain is not None:
|
|
|
|
self.validate_domain(domain)
|
2013-10-17 17:12:15 +02:00
|
|
|
|
2016-10-31 23:47:45 +01:00
|
|
|
if get_realm_by_string_id(string_id) is not None:
|
|
|
|
raise ValueError("string_id taken. Please choose another one.")
|
|
|
|
|
2016-10-29 03:48:47 +02:00
|
|
|
realm, created = do_create_realm(string_id, name, org_type=options["org_type"])
|
2013-05-31 17:06:18 +02:00
|
|
|
if created:
|
2016-10-28 07:21:53 +02:00
|
|
|
print(string_id, "created.")
|
2016-10-29 03:48:47 +02:00
|
|
|
if domain:
|
|
|
|
RealmAlias.objects.create(realm=realm, domain=domain)
|
|
|
|
print("RealmAlias %s created for realm %s" % (domain, string_id))
|
2013-10-25 23:24:26 +02:00
|
|
|
if options["deployment_id"] is not None:
|
|
|
|
deployment = Deployment.objects.get(id=options["deployment_id"])
|
|
|
|
deployment.realms.add(realm)
|
|
|
|
deployment.save()
|
2015-11-01 17:11:06 +01:00
|
|
|
print("Added to deployment", str(deployment.id))
|
2016-07-19 06:38:23 +02:00
|
|
|
elif settings.PRODUCTION and settings.ZILENCER_ENABLED:
|
2013-10-25 23:24:26 +02:00
|
|
|
deployment = Deployment.objects.get(base_site_url="https://zulip.com/")
|
|
|
|
deployment.realms.add(realm)
|
|
|
|
deployment.save()
|
2015-08-21 18:37:30 +02:00
|
|
|
# In the else case, we are not using the Deployments feature.
|
2016-12-08 01:43:15 +01:00
|
|
|
stream_dict = {
|
|
|
|
"social": {"description": "For socializing", "invite_only": False},
|
|
|
|
"engineering": {"description": "For engineering", "invite_only": False}
|
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
|
|
|
set_default_streams(realm, stream_dict)
|
2013-11-22 22:09:47 +01:00
|
|
|
|
2015-11-01 17:11:06 +01:00
|
|
|
print("\033[1;36mDefault streams set to social,engineering,zulip!\033[0m")
|
2013-05-31 17:06:18 +02:00
|
|
|
else:
|
2016-10-28 07:21:53 +02:00
|
|
|
print(string_id, "already exists.")
|