2013-10-10 21:37:26 +02:00
|
|
|
from __future__ import absolute_import
|
2015-11-01 17:11:06 +01:00
|
|
|
from __future__ import print_function
|
2013-10-10 21:37:26 +02:00
|
|
|
|
2016-12-14 19:40:04 +01:00
|
|
|
from argparse import ArgumentParser, RawTextHelpFormatter
|
2017-03-03 19:01:52 +01:00
|
|
|
from typing import Any, Dict, Text
|
2016-06-04 16:52:18 +02:00
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
from django.core.management.base import BaseCommand, CommandParser
|
2012-11-27 18:26:51 +01:00
|
|
|
|
2017-01-04 05:30:48 +01:00
|
|
|
from zerver.models import get_realm
|
2013-11-22 22:09:47 +01:00
|
|
|
from zerver.lib.actions import set_default_streams
|
2012-11-27 18:26:51 +01:00
|
|
|
|
|
|
|
from optparse import make_option
|
|
|
|
import sys
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
|
|
help = """Set default streams for a realm
|
|
|
|
|
|
|
|
Users created under this realm will start out with these streams. This
|
2017-03-14 23:43:47 +01:00
|
|
|
command is not additive: if you re-run it on a realm with a different
|
2012-11-27 18:26:51 +01:00
|
|
|
set of default streams, those will be the new complete set of default
|
|
|
|
streams.
|
|
|
|
|
|
|
|
For example:
|
|
|
|
|
2016-11-16 21:16:02 +01:00
|
|
|
./manage.py set_default_streams --realm=foo --streams=foo,bar,baz
|
|
|
|
./manage.py set_default_streams --realm=foo --streams="foo,bar,baz with space"
|
|
|
|
./manage.py set_default_streams --realm=foo --streams=
|
2012-11-27 18:26:51 +01:00
|
|
|
"""
|
|
|
|
|
2016-12-14 19:40:04 +01:00
|
|
|
# Fix support for multi-line usage
|
|
|
|
def create_parser(self, *args, **kwargs):
|
|
|
|
# type: (*Any, **Any) -> ArgumentParser
|
|
|
|
parser = super(Command, self).create_parser(*args, **kwargs)
|
|
|
|
parser.formatter_class = RawTextHelpFormatter
|
|
|
|
return parser
|
|
|
|
|
2016-11-03 10:22:19 +01:00
|
|
|
def add_arguments(self, parser):
|
2016-12-14 19:40:04 +01:00
|
|
|
# type: (ArgumentParser) -> None
|
2016-11-16 21:16:02 +01:00
|
|
|
parser.add_argument('-r', '--realm',
|
|
|
|
dest='string_id',
|
2016-11-03 10:22:19 +01:00
|
|
|
type=str,
|
2016-11-16 21:16:02 +01:00
|
|
|
help='The subdomain or string_id of the existing realm to which to '
|
2016-11-03 10:22:19 +01:00
|
|
|
'attach default streams.')
|
|
|
|
|
|
|
|
parser.add_argument('-s', '--streams',
|
|
|
|
dest='streams',
|
|
|
|
type=str,
|
|
|
|
help='A comma-separated list of stream names.')
|
2012-11-27 18:26:51 +01:00
|
|
|
|
|
|
|
def handle(self, **options):
|
2017-02-19 03:39:27 +01:00
|
|
|
# type: (**str) -> None
|
2016-11-16 21:16:02 +01:00
|
|
|
if options["string_id"] is None or options["streams"] is None:
|
|
|
|
print("Please provide both a subdomain name or string_id and a default \
|
2015-11-01 17:11:06 +01:00
|
|
|
set of streams (which can be empty, with `--streams=`).", file=sys.stderr)
|
2012-11-27 18:26:51 +01:00
|
|
|
exit(1)
|
|
|
|
|
2016-12-08 01:43:15 +01:00
|
|
|
stream_dict = {
|
|
|
|
stream.strip(): {"description": stream.strip(), "invite_only": False}
|
|
|
|
for stream in options["streams"].split(",")
|
|
|
|
} # type: Dict[Text, Dict[Text, Any]]
|
2017-01-04 05:30:48 +01:00
|
|
|
realm = get_realm(options["string_id"])
|
2016-12-08 01:43:15 +01:00
|
|
|
set_default_streams(realm, stream_dict)
|