2013-04-23 18:51:17 +02:00
|
|
|
from __future__ import absolute_import
|
2013-05-31 17:06:18 +02:00
|
|
|
from optparse import make_option
|
2013-04-23 18:51:17 +02:00
|
|
|
|
2012-11-21 22:31:42 +01:00
|
|
|
from django.core.management.base import BaseCommand
|
2013-07-29 23:03:31 +02:00
|
|
|
from zerver.lib.actions import do_create_realm
|
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):
|
2013-05-31 17:06:18 +02:00
|
|
|
help = """Create a realm for the specified domain.
|
|
|
|
|
2013-10-17 17:47:30 +02:00
|
|
|
Usage: python manage.py create_realm --domain=foo.com --name='Foo, Inc.'"""
|
2013-05-31 17:06:18 +02:00
|
|
|
|
|
|
|
option_list = BaseCommand.option_list + (
|
|
|
|
make_option('-o', '--open-realm',
|
|
|
|
dest='open_realm',
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help='Make this an open realm.'),
|
2013-10-17 17:47:30 +02:00
|
|
|
make_option('-d', '--domain',
|
|
|
|
dest='domain',
|
|
|
|
type='str',
|
|
|
|
help='The domain for the realm.'),
|
|
|
|
make_option('-n', '--name',
|
|
|
|
dest='name',
|
|
|
|
type='str',
|
|
|
|
help='The user-visible name for the realm.'),
|
2013-05-31 17:06:18 +02:00
|
|
|
)
|
2012-11-21 22:31:42 +01:00
|
|
|
|
2013-10-17 17:12:15 +02:00
|
|
|
def validate_domain(self, domain):
|
|
|
|
# Domains can't contain whitespace if they are to be used in memcached
|
|
|
|
# keys.
|
|
|
|
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 '.'")
|
|
|
|
|
2012-11-21 22:31:42 +01:00
|
|
|
def handle(self, *args, **options):
|
2013-10-17 17:47:30 +02:00
|
|
|
if options["domain"] is None or options["name"] is None:
|
|
|
|
print >>sys.stderr, "\033[1;31mPlease provide both a domain and name.\033[0m\n"
|
2013-05-31 17:06:18 +02:00
|
|
|
self.print_help("python manage.py", "create_realm")
|
|
|
|
exit(1)
|
2012-11-21 22:31:42 +01:00
|
|
|
|
2013-10-17 17:47:30 +02:00
|
|
|
domain = options["domain"]
|
|
|
|
name = options["name"]
|
|
|
|
|
2013-10-17 17:12:15 +02:00
|
|
|
self.validate_domain(domain)
|
|
|
|
|
2013-05-31 17:06:18 +02:00
|
|
|
realm, created = do_create_realm(
|
2013-10-17 17:47:30 +02:00
|
|
|
domain, name, restricted_to_domain=not options["open_realm"])
|
2013-05-31 17:06:18 +02:00
|
|
|
if created:
|
|
|
|
print domain, "created."
|
2013-10-17 18:13:34 +02:00
|
|
|
print "\033[1;36mDon't forget to run set_default_streams!\033[0m"
|
2013-05-31 17:06:18 +02:00
|
|
|
else:
|
|
|
|
print domain, "already exists."
|